From 3f65873e2168e21fdb687398b51fa7d1585b38a8 Mon Sep 17 00:00:00 2001 From: Anna Lamoureux Date: Wed, 4 Feb 2015 17:33:49 -0500 Subject: [PATCH 1/2] Answers.md are the answers to the lab questions and Die.java has the fixed bug. Before I found the bug, I tried a couple of fixes on Main.java that, of course, didn't solve anything. --- Answers.md | 39 +++++++++++++++++++++++++++++++++++++++ src/Die.java | 2 +- src/Main.java | 2 +- 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 Answers.md mode change 100755 => 100644 src/Die.java mode change 100755 => 100644 src/Main.java diff --git a/Answers.md b/Answers.md new file mode 100644 index 0000000..1cd3e15 --- /dev/null +++ b/Answers.md @@ -0,0 +1,39 @@ +Anna Lamoureux + +Lab 2 + +Pig Game Answers + + + +1. `cutoff` isn't a parameter to the method `playturn` because `cutoff` has already been initialized and does not need to be passed a variable. There is no need to change `cutoff`. +2. The code would print out `0.0` because by coding `ScoreSheet s = new ScoreSheet()` you are referencing a new, and therefore blank ScoreSheet. In ScoreSheet.java, in `getTurnAverage()` it states that if the number of turns is 0, which it would be on a blank score sheet, then return 0.0 . +3. `numBusts` where it is located is simply assuming (correctly) that if the score of a turn was equal to 0, that they had a bust. Instead of its current location, `numBusts` could be moved to the playTurn class after the program detects a rolled 1 and therefore a bust. You could rewrite the code as the code in (Figure a). +4. The program is not printing out anything, so my thought is that the program is stuck in an infinite loop, probably as a result of a point where a variable isn't being incremented. The scoresheet doesn't seem to have any problems by just looking at the code. +5. By watching the values of upValue, score, numTurns, and numBusts, it appears that every single time the computer rolls, it rolls a 1, which only increments numBusts, and numTurns while the score stays at 0 and therefore will continue in the infinite loop of the while the score is less than 100 and turn score is less than 18. The way to correct this mistake is to move the open parenthese from before `(int)` to after to make the line of code looks like that in (Figure b). +6. The average number of turns for cutoff values: 10 = 6.4375, 15 = 3.8889, 20 = 8.4167, 25 = 7.8462 + + +Figure a + +``` +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(); + } +``` + +Figure b + +``` +upValue = (int)(Math.random() * 6) + 1; +``` + 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..306a767 --- a/src/Main.java +++ b/src/Main.java @@ -9,7 +9,7 @@ public class Main public static void main(String[] args) { // Create a new game with a cutoff of 18 - PigGame g = new PigGame(18); + PigGame g = new PigGame(25); // Run one game g.playGame(); From ca343735d8421a999c2cb83f1d8492e981042f91 Mon Sep 17 00:00:00 2001 From: anna-lamoureux Date: Tue, 24 Feb 2015 19:14:00 -0500 Subject: [PATCH 2/2] Update Main.java --- src/Main.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Main.java b/src/Main.java index 306a767..4a8ad72 100644 --- a/src/Main.java +++ b/src/Main.java @@ -9,7 +9,7 @@ public class Main public static void main(String[] args) { // Create a new game with a cutoff of 18 - PigGame g = new PigGame(25); + PigGame g = new PigGame(18); // Run one game g.playGame(); @@ -19,4 +19,4 @@ public static void main(String[] args) System.out.println(g.getNumTurns()); System.out.println(g.getTurnAverage()); } -} \ No newline at end of file +}