mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
Merge pull request #125 from olliehcrook/ollies-branch
Added java version of mathdice game
This commit is contained in:
73
61 Math Dice/java/Die.java
Normal file
73
61 Math Dice/java/Die.java
Normal file
@@ -0,0 +1,73 @@
|
||||
import java.util.Random;
|
||||
|
||||
public class Die {
|
||||
private static final int DEFAULT_SIDES = 6;
|
||||
private int faceValue;
|
||||
private int sides;
|
||||
private Random generator = new Random();
|
||||
|
||||
/**
|
||||
* Construct a new Die with default sides
|
||||
*/
|
||||
public Die() {
|
||||
this.sides = DEFAULT_SIDES;
|
||||
this.faceValue = 1 + generator.nextInt(sides);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a new random number between 1 and sides to be stored in faceValue
|
||||
*/
|
||||
private void throwDie() {
|
||||
this.faceValue = 1 + generator.nextInt(sides);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return the faceValue
|
||||
*/
|
||||
public int getFaceValue() {
|
||||
return faceValue;
|
||||
}
|
||||
|
||||
|
||||
public void printDie() {
|
||||
throwDie();
|
||||
int x = this.getFaceValue();
|
||||
|
||||
System.out.println(" ----- ");
|
||||
|
||||
if(x==4||x==5||x==6) {
|
||||
printTwo();
|
||||
} else if(x==2||x==3) {
|
||||
System.out.println("| * |");
|
||||
} else {
|
||||
printZero();
|
||||
}
|
||||
|
||||
if(x==1||x==3||x==5) {
|
||||
System.out.println("| * |");
|
||||
} else if(x==2||x==4) {
|
||||
printZero();
|
||||
} else {
|
||||
printTwo();
|
||||
}
|
||||
|
||||
if(x==4||x==5||x==6) {
|
||||
printTwo();
|
||||
} else if(x==2||x==3) {
|
||||
System.out.println("| * |");
|
||||
} else {
|
||||
printZero();
|
||||
}
|
||||
|
||||
System.out.println(" ----- ");
|
||||
}
|
||||
|
||||
private void printZero() {
|
||||
System.out.println("| |");
|
||||
}
|
||||
|
||||
private void printTwo() {
|
||||
System.out.println("| * * |");
|
||||
}
|
||||
}
|
||||
53
61 Math Dice/java/MathDice.java
Normal file
53
61 Math Dice/java/MathDice.java
Normal file
@@ -0,0 +1,53 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
public class MathDice {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner in = new Scanner(System.in);
|
||||
Die dieOne = new Die();
|
||||
Die dieTwo = new Die();
|
||||
int guess = 1;
|
||||
int answer;
|
||||
|
||||
System.out.println("Math Dice");
|
||||
System.out.println("https://github.com/coding-horror/basic-computer-games");
|
||||
System.out.println();
|
||||
System.out.print("This program generates images of two dice.\n"
|
||||
+ "When two dice and an equals sign followed by a question\n"
|
||||
+ "mark have been printed, type your answer, and hit the ENTER\n" + "key.\n"
|
||||
+ "To conclude the program, type 0.\n");
|
||||
|
||||
while (true) {
|
||||
dieOne.printDie();
|
||||
System.out.println(" +");
|
||||
dieTwo.printDie();
|
||||
System.out.println(" =");
|
||||
int tries = 0;
|
||||
answer = dieOne.getFaceValue() + dieTwo.getFaceValue();
|
||||
|
||||
while (guess!=answer && tries < 2) {
|
||||
if(tries == 1)
|
||||
System.out.println("No, count the spots and give another answer.");
|
||||
try{
|
||||
guess = in.nextInt();
|
||||
} catch(Exception e) {
|
||||
System.out.println("Thats not a number!");
|
||||
in.nextLine();
|
||||
}
|
||||
|
||||
if(guess == 0)
|
||||
System.exit(0);
|
||||
|
||||
tries++;
|
||||
}
|
||||
|
||||
if(guess != answer){
|
||||
System.out.println("No, the answer is " + answer + "!");
|
||||
} else {
|
||||
System.out.println("Correct");
|
||||
}
|
||||
System.out.println("The dice roll again....");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user