![]() |
CS 111 - Introduction to Computer Science
Homework #8 |
0. Familiarize yourself with the Game of Pig.
1. Pig - Hold-at-20 Turn: In file Pig1.java, simulate a single turn of a computer Pig player that holds when the turn total reaches 20. Output each die roll and the final score gain for the computer player on separate lines. Three example transcripts:
Roll: 4 Roll: 5 Roll: 6 Roll: 5 Score gain: 20 Roll: 3 Roll: 1 Score gain: 0 Roll: 5 Roll: 2 Roll: 3 Roll: 6 Roll: 5 Score gain: 21
2. Pig - Hold-at-20 Outcomes: What are the probable outcomes of the "hold at 20" policy? In file Pig2.java, run this simulation 1,000,000 times (without dice roll and final score output), counting occurrences of the possible score gains in an array. Output each possible gain on a separate line along with the estimated probability of achieving it when holding at 20. Highlight the transcript in the space immediately below to check your result:
Gain Probability 0 0.624974 20 0.099667 21 0.095048 22 0.074164 23 0.053966 24 0.034919 25 0.017262
3. Pig - Hold-at-20-or-Goal Turn: It does not make sense to continue rolling when holding would win the game. In file Pig3.java, modify your simulation from Exercise 1 so that the computer player is given a non-negative score < 100 from standard input, and holds when the turn total reaches 20 or the goal score of 100 would be achieved. Output each dice roll, the final score gain, and the resulting score for the computer player on separate lines. Example transcript with user input underlined:
Score? 90 Roll: 2 Roll: 3 Roll: 4 Roll: 4 Turn total: 13 New Score: 103
4. Pig - Hold-at-20-or-Goal Game: In this exercise, you will use your turn simulation of exercise 3 to simulate an entire single-player Pig game in file Pig4.java. Using a variable score initialized to 0, simulate a game played to 100 points by a computer player with this policy. Example transcript:
Roll: 4 Roll: 6 Roll: 1 Turn total: 0 New Score: 0 Roll: 6 Roll: 5 Roll: 5 Roll: 6 Turn total: 22 New Score: 22 Roll: 6 Roll: 2 Roll: 2 Roll: 3 Roll: 3 Roll: 2 Roll: 5 Turn total: 23 New Score: 45 Roll: 3 Roll: 4 Roll: 6 Roll: 2 Roll: 6 Turn total: 21 New Score: 66 Roll: 3 Roll: 3 Roll: 4 Roll: 4 Roll: 6 Turn total: 20 New Score: 86 Roll: 3 Roll: 5 Roll: 5 Roll: 6 Turn total: 19 New Score: 105
5. Multiplication Table: Using nested loops, write a program MultTable.java that prints the multiplication table (without row/column labels). Your table should appear in the following right-justified format:
1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72 81