CS 111 - Introduction to Computer Science
Homework #7


Due: Friday 3/9 at the beginning of class

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 AveragePigTurns.java, using code from your simulation from the previous assignment (PigSolitaireGame.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.634906
2. Two-Computer-Player Pig: In file PigComputerGame.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 hereNote: 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.

3. Pig First Player Advantage: There is an advantage to going first in Pig, but how big an advantage is it for “hold at 20 or goal” play? In file PigAdvantage.java, using code from PigComputerGame.java (without previous output), simulate 1,000,000 games and output the fraction of games won by the first player.  Highlight the transcript in the space immediately below to check your result:

Probability of first player win: 0.535006

Common error: When creating nested loops, perform loop initialization directly before the relevant loop.  Note:

<loop 1 initialization>
<loop 1> {
        <loop 2 initialization>
        <loop 2> {
                    ...
        }
}

is not the same as

<loop 1 initialization>
<loop 2 initialization>
<loop 1> {       
        <loop 2> {
                    ...
        }
}

In the latter case, <loop 2> is not properly initialized each time.

4. Text Diamond: Exercise 5.24.  Call your program Diamond.java.  If you need an optional warm-up exercise, consider 5.15.   Hint: One possible solution uses two nested for repetitions, a single if decision, and three print statements (one each for '*', ' ', and newline).

Extra credit (10%): Generalize your program to prompt the user for an odd positive integer n and produce an n-by-n diamond shape.