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:
theDice represents the pair of dice.
thePoint is the integer-valued point obtained
on the come-out roll.
CrapsPlayer. They should do the following:
theDice), and it needs
to roll the dice to perform the come-out roll. The come-out
roll assigns the value on the dice to thePoint.
After the constructor creates the dice and performs the
come-out roll, it announces the result of the come-out roll
using a line of code that I have provided.
isAutomaticLoser returns a boolean
value that is true if the come-out roll is an
automatic loser (a 2, 3, or 12), and false
otherwise. (Remember where you stored the result of the come-out
roll in the constructor.)
isAutomaticWinner returns a boolean
value that is true if the come-out roll is an
automatic winner (a 7 or 11), and false otherwise.
roll performs some roll that occurs after the
come-out roll. It just returns the value rolled without changing
the point.
isWinner returns a boolean that
is true if the value of the roll supplied as
its parameter equals the point, and false otherwise.
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:
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:
Dice class, to return the value
of a roll of the dice.
CrapsPlayer class, to return the
value of a roll that occurs after the come-out roll.
main method of the
PlayCraps class, to store the result of a roll
that occurs after the come-out roll.
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.