From 27ef9994e03ba31ded3165512025c38b670a7244 Mon Sep 17 00:00:00 2001 From: Steve MacDonald Date: Thu, 29 Jan 2015 15:46:33 -0500 Subject: [PATCH] Answers to Lab 2 and a bug fix --- Lab 2 Answers.md | 38 ++++++++++++++++++++++++++++++++++++++ src/Die.java | 2 +- src/Main.java | 12 +++++++----- 3 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 Lab 2 Answers.md mode change 100755 => 100644 src/Die.java mode change 100755 => 100644 src/Main.java diff --git a/Lab 2 Answers.md b/Lab 2 Answers.md new file mode 100644 index 0000000..50a09f2 --- /dev/null +++ b/Lab 2 Answers.md @@ -0,0 +1,38 @@ +#Answers to lab 2 + +**Question 1:** The parameter cutoffValue is part of the constructor method PigGame. + +**Question 2:** 0.0 + +**Question 3:** In method playTurn, + + while(!rolledOne && score < cutoff && score + s.getScore() < 100) + { + d.roll(); + if (d.getUpValue() == 1) + { + score = 0; + rolledOne = true; + numBusts += 1; + } + else + score = score + d.getUpValue(); + } + + return score; + } +} + +**Question 4:** I think the problem(s) might be located in either PigGame or ScoreSheet. I think it is unlikely that the issue is with Die or Main. + +**Question 5:** The problem was in the file Die.java, with the method roll, value upValue. The program was casting random numbers between 0 and 1 into int values, which is always 0, then adding 1 which leaves us with 1 as the upValue every time. I moved the cast outside of the parentheses so that the random value is multiplied by 6 before it is cast into an int and added to 1. + +**Question 6:** + +10: 14 + +15: 13 + +20: 12 + +25: 12 \ No newline at end of file diff --git a/src/Die.java b/src/Die.java old mode 100755 new mode 100644 index 6f3a8e4..2ca26c3 --- a/src/Die.java +++ b/src/Die.java @@ -22,7 +22,7 @@ public Die() */ public void roll() { - upValue = ((int)Math.random() * 6) + 1; + upValue = (int)(Math.random() * 6) + 1; } /** diff --git a/src/Main.java b/src/Main.java old mode 100755 new mode 100644 index ff2edf8..ca852d0 --- a/src/Main.java +++ b/src/Main.java @@ -8,15 +8,17 @@ public class Main { public static void main(String[] args) { + int totalTurns = 0; + for (int i=0 ; i<1000 ; i++) + { // Create a new game with a cutoff of 18 - PigGame g = new PigGame(18); + PigGame g = new PigGame(25); // Run one game g.playGame(); - // output the results - System.out.println(g.getScore()); - System.out.println(g.getNumTurns()); - System.out.println(g.getTurnAverage()); + totalTurns += g.getNumTurns(); + } + System.out.println(totalTurns / 1000); } } \ No newline at end of file