V22.0101 Homework Assignment 3 Fall 2000
A Game of Craps

Assigned: Thur Oct 26
Due: Thurs, Nov 9 11:59 pm

In this assignment you will write a program in which two players play craps and bet on the outcomes of the games. They take turns rolling the dice, and the first one to reach a pre-determined goal wins the game. If during the game, one player loses all his money, the other player wins. This assignment will give you practice correctly implementing while and/or do-while loops; and using objects

Craps is a simple dice game. You roll two dice. If you get a 7 or an 11 on this first roll you win; if you get a 2 or a 12 on the first roll you lose; otherwise the result of the first roll is called your point. You then continue to roll the dice until you match the point (in which case you win) or until you roll a 7 (in which case you lose).

Since your program will eventually have two players play against each other, each player will be represented by an instance of the a class called Dice. This class asks the user to place a bet and then plays a game of craps. Have the player start with $100 dollars. This starting amount, the name of the player, and the goal of $300 should all be passed to the class through the constructor. Don't allow bets that are less than 0 or more than the player has. Don't allow the player to play if he/she runs out of money. All of the bets will be inputted at the keyboard. Use ConsoleReader to do the reading.

The computer rolls the dice by setting each of the two dice values to a random value between 1 and 6 and adding the two values together.

Here is a sample run for one player.

[Some introductory remarks....]
Grace, you have $100 dollars. How much would you like to bet? $40
Press return key to roll the dice.
You rolled a 3 and a 4 for a total of 7.

Grace, congratulations! You won the game and the bet!
You won $40. You now have $140.

where Grace is the string passed to the constructor (see below). Here is another possible scenario:
Joe, you have $100. How much would you like to bet? $150
That's more money than you have.

Joe, you have $100. How much would you like to bet? $130
That's more money than you have.

Joe, you have $100. How much would you like to bet? $40
You rolled a 3 and a 2 for a total of 5.
5 is your point.

Press return key to roll the dice.
You rolled a 3 and a 5 for a total of 8.

5 is your point.

Press return key to roll the dice.
You rolled a 1 and a 4 for a total of 5.

Joe, you won the game.
You won $40. You now have $140.

The class Dice should have the following instance variables:

In addition, use goal as a final int variable.

Hopefully you will see when you write the finished program the significance of instance variables; each instance of the class Dice (each of the players) has its own values for the instance variables. Thus each instance has its own name and its own value of amount. Also, each of the methods of the class can access these instance variables.

Of course console, the instance of ConsoleReader, should be accessible to all instances of the Dice class, that is, to both players. Thus we declare it as a static variable. (Static variables are also called class variables.) We only want this to be instantiated once, so we do this in the main method.

The main execution of your program should be done in method begin() which calls the following methods in the order that is indicated (which, yes, you need to write):

private void get_bet()
It should tell the player the current amount of money that he or she has and then prompt for the amount of the bet It should do the error checking outlined above.
private boolean play_game()
This returns a boolean value that states whether the player wins. This method does the bulk of the work. It should have some methods of its own, but the design of that is left to you.
private void disp_message(boolean win)
This method takes as a parameters the result of the game. It prints an appropriate message (``Oh too bad, you lost the game. You bet $25 so you lost $25. You now have $75").

After public void begin() calls the above methods, if appropriate it prints whether the player has reached the goal or gone bust. At this point of the program development only one player should throw the dice, i.e., your main method should just instantiate an object which calls begin().

Once your preliminary program is running, the next step is to have two players play against each other. The players take turns playing each other until one looses all of his/her money or reaches or exceeds the goal. The main method will now look very much like that of the coin tossing program we wrote in class. To help you write this, write the following methods:

public boolean terminate()
which indicates whether a player has reached the goal or gone bust,
public boolean reached_goal()
which determines whether the goal has been reached or exceded (it is used in terminate),
public boolean busted()
which determines if a player has gone bust (it is also used in terminate), and
public String get_name()
which gets the name of a player.

Your program should print the name of the player whose object is being executed. For instance,

Joe, you have $100. How much is your bet?

When the first player has either won or lost his roll, the next player goes. So the program would print, for instance,

Grace, you have $100. How much is your bet?

And when a player wins, the program should print the name of that player.

Design your program so that it fits the description given above, though you may (and should, in some cases) add extra procedures. Your assignment will be graded partially on whether it executes correctly, but programming style, comments, good identifier names, and generally readable code will also count.

Sam Marateck
WED Oct 25 13:15:39 EDT 2000