From 3be271cae0f989426fd1d33a9902bf404e6811da Mon Sep 17 00:00:00 2001 From: Jacob Fritz Date: Thu, 29 Jan 2015 15:26:22 -0500 Subject: [PATCH 1/2] Changed code to run properly. Added answers under FritzAnswers.md. --- FritzAnswers.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 FritzAnswers.md diff --git a/FritzAnswers.md b/FritzAnswers.md new file mode 100644 index 0000000..95a22b6 --- /dev/null +++ b/FritzAnswers.md @@ -0,0 +1,42 @@ +## Jacob Fritz Debugger Lab + +**Question #1:** Why is `cutoff` not a parameter to the method `playTurn` in the `PigGame` class? + + This is because `cutoff` is a variable within the `PigGame` class, and since `playTurn` is a method in that class, it can access that variable freely. + +**Question #2:** What would the following code print? +``` + ScoreSheet s = new ScoreSheet(); + System.out.println(s.getTurnAverage()); +``` + + It would print "0.0". + +**Question #3:** In the `PigGame` class, `numBusts` is incremented in the `playGame` method. Describe how this statement could be moved to another method in the class without affecting the results. + + It could be moved from the `playGame` method to the `playTurn` method. It could be placed under the if statement, `if (d.getUpValue() == 1)`, after `rolledOne = true;`. This would not affect the results, as currently it checks if `score` equals 0 in the `playGame` method after a turn has been played. This change would simply make the change while the `playTurn` method is running, rather than after it finishes. + +**Question #4:** Based on your current understanding of the code, where do you think the problem(s) might be located? Are there portions of the code where you are fairly certain the problem(s) could not possibly be? + + I would estimate that the logical assumption is that the problem lies in one of the loops running thoughout the program. I do not know if any area can currently be dismissed, although I would say the declaration section and the output sections are unlikely to be incorrect. + +**Question #5:** Describe the problem(s) with the program and the way(s) you made the program execute correctly. + + The problem is that `score` never becomes greater than 0. The score keeps being reset to 0, causing the loop to be run infinitely. This is because when the dice is rolled, the value that is determined, the `upValue`, is always returned as a 1, which then caused the score to be reset, ends the turn, and increases the number of busts. + To fix this problem, the code used to convert the random number (`(int)`) needed to be moved outside of the paranthesis, allowing the random number (0.0-0.99) to be multiplied by 6, making it 0.0-5.94, then increased by 1, making it 1.0-6.94. Then, when it is converted to an integer with `(int)`, it will be between 1-6. + + The code now reads `upValue = (int) ((Math.random() * 6) + 1);`. + +**Question #6:** Using the correct program, what are the average number of turns for cutoff values 10, 15, 20, and 25? + + Cutoff Value: 10 + Average Number of Turns: 15 + + Cutoff Value: 15 + Average Number of Turns: 13 + + Cutoff Value: 20 + Average Number of Turns: 12 + + Cutoff Value: 25 + Average Number of Turns: 12 \ No newline at end of file From 47eb026459fed6feba8424389528c98096679224 Mon Sep 17 00:00:00 2001 From: Jacob Fritz Date: Thu, 29 Jan 2015 15:29:04 -0500 Subject: [PATCH 2/2] Fixed commit --- src/Die.java | 2 +- src/Main.java | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) mode change 100755 => 100644 src/Die.java mode change 100755 => 100644 src/Main.java diff --git a/src/Die.java b/src/Die.java old mode 100755 new mode 100644 index 6f3a8e4..b3be862 --- 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..c901b5c --- a/src/Main.java +++ b/src/Main.java @@ -9,14 +9,26 @@ 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(); + //g.playGame(); + //Find Turn Average + int Average = 0; + int games = 0; + while (games < 1000000) + { + g.playGame(); + Average += g.getNumTurns(); + games ++; + } + + Average /= 1000000; + System.out.println(Average); // output the results - System.out.println(g.getScore()); - System.out.println(g.getNumTurns()); - System.out.println(g.getTurnAverage()); + //System.out.println(g.getScore()); + //System.out.println(g.getNumTurns()); + //System.out.println(g.getTurnAverage()); } } \ No newline at end of file