Skip to content

Answers to Lab 2 and a bug fix #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Lab 2 Answers.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/Die.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Die()
*/
public void roll()
{
upValue = ((int)Math.random() * 6) + 1;
upValue = (int)(Math.random() * 6) + 1;
}

/**
Expand Down
12 changes: 7 additions & 5 deletions src/Main.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}