CS 111 - Introduction to Computer Science
Homework #10


Due: Friday 4/7 at the beginning of class

1.  Dice Histogram: Call your class DiceHistogram.java. In this exercise, you will simulate the throwing of 2 standard six-sided dice a given number of times and display the resulting distribution as a text-based histogram. First, ask the user how many time to roll the dice. Then simulate the dice rolling and print the maximum number of times a sum was rolled. Next print lines for each possible dice roll showing a number of asterisks proportional to the number of times the sum was rolled. Scale the number of asterisks such that the sum rolled the most will have 60 asterisks.  Example transcript (user input underlined):

How many rolls? 1000000
Max count: 166039
 2 **********
 3 ********************
 4 ******************************
 5 ****************************************
 6 **************************************************
 7 ************************************************************
 8 **************************************************
 9 ****************************************
10 ******************************
11 ********************
12 *********

Possible "stepping stones" towards the goal: (1) Print the given number of roll sums. Remember: The sum of two 6-sided dice is different than a single roll of an 11-sided die numbered 2 through 12.  (Why?)  (2) Use an array to count the number of rolls for each sum and print a table (i.e. numbers instead of histogram asterisks above).  (3) Compute and print the maximum count.  (4) Round (60.0 * count / maximum) for each count to compute the number of asterisks, and print the histogram as shown.

2.  ChompGame: Chomp is a Nim-type game in which two players take bites of a gridded cookie in turn.  Each bite at a given row and column consumes the cookie at, below, and to the right of that position.  The player taking the last bite in the first row and first column loses.

Implement ChompGame.java according to this specification.  This class will model the game, and could serve as a basis for a text-based game interface (next exercise), a graphical user interface (GUI), game simulation for analysis/machine learning, etc.  No scanning of input or printing of output will take place in this class, so you'll need another class (see next exercise) to effectively test this code.

Chomp Resources:

3.  Chomp: For this exercise, design and implement Chomp.java according to this specification such that two players can play a text-based version of chomp for a given board size up to at least size 10-by-10.  Use class ChompGame for modeling the game state itself. All input/output for text-based play should take place in the main method of this class.

First, print instructions with the following lines: {"CHOMP", "In the game of Chomp, players bite a rectangular cookie in turn.", "The player chomping the last bite (0,0) loses.", "Each chomp, a row and column, bites off all below and to the right."}. Then prompt the user for the number of rows and columns with prompts "Rows? " and "Columns? ". Read these in as integers presumed to be in the range 1-10.  Create a ChompGame object using this information.

Then, while the game is not over, do the following: Print the game board produced by the object toString() and preceded and followed with blank lines. Prompt the user for a row and column for chomping with "Player # chomp? ", where "#" is the games current player number.  Attempt the chomp move, making the move and changing the player if the chomp move is legal, and just printing the message "That is not a legal chomp position." otherwise.

After the game is over, print a blank line and the message "Player # wins!", where "#" is the winning player.

No input/output should take place in class <code>Chomp</code>. Whenever possible, use the computation of the Chomp class to perform the modeling (i.e. play, logic) of the game.

Example transcript (input underlined):

CHOMP
In the game of Chomp, players bite a rectangular cookie in turn.
The player chomping the last bite (0,0) loses.
Each chomp, a row and column, bites off all below and to the right.
Rows? 3
Columns? 5

 01234
0*****
1*****
2*****

Player 1 chomp? 1 4

 01234
0*****
1**** 
2**** 

Player 2 chomp? 2 1

 01234
0*****
1**** 
2*    

Player 1 chomp? 0 1

 01234
0*    
1*    
2*    

Player 2 chomp? 1 0

 01234
0*    
1     
2     

Player 1 chomp? -1 0
That is not a legal chomp position.

 01234
0*    
1     
2     
Player 1 chomp? 0 5
That is not a legal chomp position.

 01234
0*    
1     
2     

Player 1 chomp? 1 1
That is not a legal chomp position.

 01234
0*    
1     
2     

Player 1 chomp? 0 0

Player 2 wins!