![]() |
CS 111 - Introduction to Computer Science
Homework #9 |
1. Average Pig Turns: How many turns on average does it take for the “hold at 20 or goal” player to reach 100? In file Pig5.java, using code from your simulation from the previous assignment (Pig4.java without previous output), simulate 1,000,000 games and output the average number of turns the computer takes to reach the goal score. Highlight the transcript in the space immediately below to check your result:
Average turns: 12.6349062. Two-Computer-Player Pig: In file Pig6.java, augment your previous game simulation so that two computer players are competing with the same “hold at 20 or goal” policy. At the beginning of each turn, output both scores and which player is currently playing, along with previous roll and score output. An example transcript is here. Note: Be sure to "factor out common code". There is no reason to have two separate, nearly identical sections of code for each player's turn.
Probability of first player win: 0.535006
Common error: When creating nested loops, perform loop initialization directly before the relevant loop. Note:
<loop 1 initilization>
<loop 1> {
<loop 2 initialization>
<loop 2> {
...
}
}
is not the same as
<loop 1 initilization>
<loop 2 initialization>
<loop 1> {
<loop 2> {
...
}
}
In the latter case, <loop 2> is not properly initialized each time.