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

Assigned:TUE NOV 6
Due: THURS NOV 15, 11:59 pm

In the version of this assignment you hand in, 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. To warm up write a preliminary draft which we call Draft #1. Draft #1 envolves only one player. Don't hand in Draft #1.

Draft #1

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 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.

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
Hit 1 to continue.
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 obtained by getName (see below). The game should continue until the player loses all his or her money or rreaches the goal set through the constructor. See the next link "A sample program shell". That link is not a working program but should give you ideas about writing the program.

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.

Hit 1 to continue.
You rolled a 3 and a 5 for a total of 8.

5 is your point.

Hit 1 to continue.
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 the Scanner class to read, for instance, the digit 1.
The class Dice should have the following instance variables:

In addition, use GOAL as final static int variable, where as soon as you reach or exceed the goal, you have won the game.

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.
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"). It also calculates the amount of money left
public String getName()
which gets the name of a player from the instance variable name.

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(). Your constructor should look like:

public Dice(int amount, goal, name)
     this.amount = amount;
     this.name = name;
     GOAL = goal;

this refers to the instance variable. Include the following methods in your program:

public boolean busted()
which indicates whether a player has gone bust,
public boolean reachedGoal()
which determines whether the goal has been reached or exceded (it is used in terminate),

Draft 2

Once your draft 1 program is running, write draft 2 which has two players play against each other. You may use Coin5.java of SEPT 27 as a guide. 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.

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 method will now look very much like that of the coin tossing program we wrote in class.

Thus all you have to do is rewrite the main method of draft #1 and include methods terminate() and reachedGoal() in the instantiated class.

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 MAR 4:15:39 EDT 2012