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

Assigned:MON Oct 21
Due: Draft #1 TUE, OCT 30 11:59 pm
Draft #2 TUE, NOV 6 11:59 pm
The revised draft will only be for draft #2. It is due one week after you receive your reply from the eTutor

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 the Scanner class. It was explained in class on OCT 15 in Prog2.

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.

In order to pause the program, use something like: JOptionPane.showMessageDialog(null, "hit continue"); or use the Scanner class to read, for instance, the digit 1.
The class Dice should have the following instance variables:

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.

The main execution of your program should be done in method begin() that calls the necessary private methods. You should use the following methods (which, yes, you need to write):

private void getBet()
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 playGame()
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 dispMessage(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").
public boolean busted()
which determines if a player has gone bust (it is also used in terminate, see below), and
public String getName()
which gets the name of a player from the instance variable name.

After public void begin() may call the above methods. At this point of the program development only one player should throw the dice, i.e., your main method should just instantiate only one object which calls begin(). You may use Prog0.java of OCT 27 as a guide. Your constructor should look like:

public Dice(int amount, String name)
{
     this.amount = amount;
     this.name = name;
}

this refers to the instance variable.

What we just explained constitutes draft 1. Once your draft 1 program is running, write draft 2 which has 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. In addition, use goal as a final int variable, where as soon as you reach or exceed the goal, you have won the game.

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 reachedGoal()
which determines whether the goal has been reached or exceded (it is used in terminate),

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
SUN Oct 21 10:15:39 EDT 2007