diff --git a/61 Math Dice/java/Die.java b/61 Math Dice/java/Die.java new file mode 100644 index 00000000..1a616213 --- /dev/null +++ b/61 Math Dice/java/Die.java @@ -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("| * * |"); + } +} \ No newline at end of file diff --git a/61 Math Dice/java/MathDice.java b/61 Math Dice/java/MathDice.java new file mode 100644 index 00000000..bd2ecd70 --- /dev/null +++ b/61 Math Dice/java/MathDice.java @@ -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...."); + } + } + +} \ No newline at end of file