CSC 211: Program 5

Due: Seventh Week

Objective of this Program: (1) To learn more about repetition in programs, (2) to use more than one class in a project, and (3) to pass Objects as parameters.

The program is to play the game of dice called craps. The term comes from the French term crab, which means to "lose the throw". (A dice is a six-sided cube with a number of spots on it. The number of spots range from 1 to 6, inclusive.) The rules are as follows: two die are thrown and the sum of their two faces are added up, giving a number from 2 to 12. If the number is 7 or 11, the thrower wins. If the number is either 2, 3, or 12, the thrower loses. If it is neither of these numbers (i.e., it is either 4, 5, 6, 8, 9, or 10), this number becomes the point. The thrower continues to throw the dice until either his point comes up or the number 7 comes up. If the point comes up, the thrower wins. If a 7 comes up, the thrower loses. (One says he "crapped out"). If the thrower wins, he plays again. If he loses, he gives up the dice to the next player. (Hence, the term "crapped out" or lose the throw.)

In this program, the computer will give the outcome of the throw of the die and decide if the thrower won or lost, and the user will be the thrower. The program should work by first putting out a welcoming statement which gives a salutation and asks if the person wants to play. E.g.,


Hi there!  Do you want to play some craps(Y/N)?  

If the user responds 'N', then the program terminates. If the response is 'Y', ask the user does he want to know the rules of the game. If the response is 'Y', print out the rules, else start the game.

The game is played as above. If the user wins, (s)he is informed of the win and another game is played. One continues to play until the player loses. At this point, the user is told how many times (s)he won. E.g.,

You won 3 consecutive times.  That is very good.  Thanks for playing craps.

The program then terminates.

Implementing Program 4

There will be two classes for this assignment. One is the Craps class, which will play the game and the other will be the Die class, which will represent the dice to be thrown.

Die Class

The Die class has been created for you by me and you can download it here. You just need to include the class in your project. How to do this will be demonstrated in class. Note that the UML of the class is:

		 _______________________________
		|     Die			|
		|_______________________________|
		|   - final int MAX_NUMBER	|
		|   - final int MIN_NUMBER      |
		|   - final int NO_NUMBER	|
		|   - int number		|
		|_______________________________|
		|   + Die()			|
		|   + roll(): void		|
		|   + getNumber(): int		|
		|_______________________________|

The UML (Universal Modeling Language) is a means to describe to the user the interface of a class. An example of a clock is given in Chapter 8 on page 472 of the text.

There are 4 members (or variables) of the class. Three of them are fixed and represent the largest number the die can have (6), the smallest number that the die can have (1), and a numerical value to represent that a number has not yet been assigned to the die. The three methods are (1) the constructor Die which initializes the number to be now yet assigned (NO_NUMBER), (2) a method roll, which rolles the die (i.e., randomly assigns it a number from 1 to 6, and (3) getNumber, which returns the value of the Die.

Craps Class

As said earlier, the term craps has nothing to do with Thomas Crapper, who popularized the flush toilet and has several patents for its improvement.

The Craps class should have 4 methods, main, rules, plays, and rollDie. This is similar to the last programming assignment where we had 2 methods, main and calculateBill.

The protocols for the four methods are

public static void main(String [] args)

public static void rules()

public static boolean play()

private static int rollDie(Die one,Die two)

The method main

The main method will do the following:

(1) ask the user if they want to play a game? If the user says no, then terminate the program.

(2) ask the user if they want the rules. If the user replies yes, then call the method of rules.

(3) play the game by calling the method play of the object. If the user wins, call the method play again. Keep calling the method until the user loses the game. One knows if the user won or lost by the returned value of play , which returns a true if the user won, false if the user lost.

(4) thank the user for playing (via a System.out.println statement) and tell the user how many times they won the game before losing.

The method rules

The rules method will just be a collection of System.out.println statements, which just print out the rules. The rules are given in the first paragraph of this documentation. You do not have to retype them, just copy and paste them into rules and add the System.out.println statements.

The method play

To play the game of craps, the two die have to be created. You do this via the constructor of the class. The constructor is called whenever a new object of the class is created, i.e., when one has a statement of the form:

Die dieOne = new Die();	
Die dieTwo = new Die();
In the above statement, what comes after the 'new' is a call to the constructor. Note that if you only will have the variable defined:

Die dieOne;

Die dieTwo;

This will not create an actual Die. This just tells the compiler that you want to have two Die, but you have not yet created them. The call to the constructor will actually create them.

So the first two lines of your method play will create the two die: dieOne and dieTwo as above:

Die dieOne = new Die();	
Die dieTwo = new Die();
The method play will call the private method rollDie which will return a value between 2 and 12. The code for calling rollDie is:

result = rollDie(dieOne,twoDie);

where result is an int. What is this is saying is that one is passing two arguments to rollDie, each of which is a Die. It returns a variable of type int. It will roll the dice and return the value. (See below for a discussion of this method.)

If the game is won or lost on the first throw, a boolean value is returned by the method (true if the user is won, and false if lost). The user is notified of what the result of the die was and whether (s)he won or lost via System.out.println .

If the game is not won or lost on the first throw, the value of point is set to the result and the user is notified of the point they are trying to win with (again via a System.out.println). (point is a local variable inside of play.) Then a while loop is entered and one continues to roll the die until the game is won or lost. The user is notified of the outcome of each roll of the die (again via a System.out.println). This must be inside the loop. Before returning, the user is notified if they won or lost. It will then return true if the user won and false if the user lost.

So a typical output would look like:

The die came to 7 and you won.

or

The die came to 3 and you lost.

or

Your point is 6.
The die came to 8.
The die came to 5.
The die came to 9.
The die came to 7 and you lost.

Note that the user will see the output of all the games at the console window. I.e., they will not see each throw of the die as it occurs. This is because there are no "pauses" in the game.

The method rollDie

Since we have to roll both die more than once in the playing of a game, and since the rolling of the die logically stands by itself, we make a separate method out of it.

The procedure's protocol will be:

private int rollDie(Die one,Die two)

This means that it is passed two parameters, which are both an object of the class Die. The procedure will roll each of the die via the roll methods for the die. This is the same as calling methods of classes in the Java API. That is to say, you have called methods from String and Math classes. Now you are just using a method in a class that a user (me) has supplied you. So, you would write:

one.roll();

to roll the first die. Similarly for the second die.

Note: We know how to write the a call for roll from its UML (see above).

It will use the getNumber methods to get the outcome of the roll of the die and add them up to get the result. It will return the result. The returning of the result is handled just as you did in Program4.

This appears to be a very difficult assignment. However, one can solve it by doing it incrementally. I have given you a incremental outline on how to do this. This is called top down developement.

Extra-Credit:

(i) If we just put the output to the window console, then we would just see the output of all the games. To make it more interesting to the user, we could send to the user after each throw.

To send the information to the user, we will use the JOptionPane.showMessageDialog. An example of this can be found here. So, one would have statements like

JOptionPane.showMessageDialog(null,"Your point is 8","Result of Throw", JOptionPane.INFORMATION_MESSAGE);

Then the user will see the "game" as it progresses with each throw. However, when we are done, there will be no "record" of the game as we had with using the console window. So we should incorporate both of these.

What this means is that you will have "pairs" of messages for each output. I.e., you will have two lines of code for each output, one of them a System.out.print and one of them a JOptionPane.ShowMessageDialog. Each of the pair will output the exact same message.

(ii) Modify the program to ask the user how many times (s)he wants to play the game. Then play the game the number of times the user asked, and at the end tell the user how many times they won, and the percentage of times that they won. This should be done in the main method.

(iii) First ask the user if they want the output of each game to be shown to the user. I.e., whether they want the System.out.println statements to be sent to the console window or not. Then when the method of plays is called, send a boolean value, with true if one wants the outputs and false if one wants the outputs suppressed.

So one would have

boolean result, notSuppress;

if user wants results
  notSuppress = true;
else
  notSuppress = false;

result = plays(notSuppress);

Then in plays check the value of notSuppress to see if the output is to sent to the console window or not.

(iv) Combine Extra-Credit (ii) and (iii) so that one can run 1000 games and see what the percentage of winning is without completely filling up the console window.

As with all of the extra-credits, there should be two projects to hand-in. The regular assignment and the extra-credit assignment.

What to Hand-in

You should post your project as a 'zip' file on the COL website. Also indicate how many hours you worked on the program in the Readme.txt file.

Again, as usual, if you do the extra-credit, you should post two projects in the appropriate places.


Homepage of CTI School back to 211 homepage