CS 5 Fall 2009
Short Assignment #7
Due Friday, October 9

You have no idea how many times a CS 5 student has said to me, "Gee willikers, CS 5 is fun, but it would be so much funner if it included some vice!" I hear that all the time.

You want vice? You've got vice. We'll go for gambling. Craps, to be specific.

There are a few variations of craps, but let's focus on the simplest one, in which the player is what's known as a "right bettor." The game starts with what's called a "come-out roll," in which the player rolls a pair of dice. There are three possible results of the come-out roll:

Your job is to help me write some Java code to play a game of craps. Download the files PlayCraps.java, CrapsPlayer.java, and Dice.java. (You can option-click on these links to download the files.) The PlayCraps and Dice classes are complete; you should make no changes to these. Your job is to write all the methods of the CrapsPlayer class.

The Dice class, which represents a pair of dice, has just two methods. The constructor creates a random number generator. The roll method returns the value obtained by generating two random numbers (thereby simulating rolling the two dice) and summing their results (after correcting for random numbers starting from 0).

The PlayCraps class has just a main method, which plays a game of craps. Other than System.out.println, the only methods that main calls are methods of the CrapsPlayer class. Once you understand the purpose of each of the CrapsPlayer methods, you should be able to see that the operation of main matches the rules of craps given above.

Let's look at the CrapsPlayer class. It has two instance variables:

Your job is to write the bodies of the five methods in CrapsPlayer. They should do the following: You should be able to write each of the bodies of isAutomaticLoser, isAutomaticWinner, roll, and isWinner with just one fairly simple line of code. You will probably need to write two lines of code in the constructor. Therefore, I am asking you to write a whopping total of six lines of code! But you will have to understand a bit of code.

None of the lines of code you write should call System.out.println or System.out.print. Notice also that this program requires no console input; all input comes from rolling the dice, which in turn comes from the random number generator.

Hand in a listing of your CrapsPlayer.java file, along with output from eight different runs of the program.

For your edification, here are five runs of my version:

  1. The come-out roll is 7. You win!

  2. The come-out roll is 9. You rolled a 8. You rolled a 2. You rolled a 8. You rolled a 5. You rolled a 2. You rolled a 6. You rolled a 8. You rolled a 4. You rolled a 9. You win!

  3. The come-out roll is 8. You rolled a 6. You rolled a 2. You rolled a 7. You lose!

  4. The come-out roll is 6. You rolled a 3. You rolled a 3. You rolled a 10. You rolled a 5. You rolled a 2. You rolled a 9. You rolled a 3. You rolled a 7. You lose!

  5. The come-out roll is 3. You lose!

When you are done, please take a moment to look over how the three classes interact. Think about how I designed this program and why I might have made some of the design decisions that I made. You will have to make several design decisions in Lab Assignment 1, and understanding the design of this craps program can help you become a better designer.

You might be confused because the identifier roll appears in three different contexts:

To distinguish the first two contexts, you can see that each of the Dice and CrapsPlayer classes defines a roll method. You can always tell which class's method is being called by looking at the reference to the left of the dot in a call. For example, in the do-while loop within the main method of PlayCraps, we see the call bettor.roll(). The variable bettor is declared as a reference to CrapsPlayer, and so the call must be to the roll method of CrapsPlayer, not Dice.

Looking at that line in full, we see roll = bettor.roll(), in which the identifier roll appears in two different contexts. As we've just seen, the rightmost appearance denotes the roll method of CrapsPlayer. What about the appearance of roll on the left-hand side? It's clearly not denoting a method: there are no parentheses (so it cannot be a call), and besides, you know that you have to have a variable on the left-hand side of an assignment. So it must denote the local variable roll in the main method.


Back to Short Assignments
Thomas H. Cormen <thc@cs.dartmouth.edu>
Last modified: Tue Oct 6 10:19:26 2009