CSC 224: Program 3

Due: Fourth Week

Objective of this Program: (1) to use several classes in a project and (2) to start using GUI.

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 game is played as above. If the user wins, (s)he is informed of the win. The program will simulate the tossing of the dice by using a random number generator to generate a number from 1 to 6.

The user will decide if they want to play another game by clicking on a button, or quit, by clicking on a different button.

Implementing Program 3

There should be four classes for this assignment. One is the driver class, which will create the frame object, the second is the frame class InterfaceCraps, which creates the interface, the third will be the class Die, and the the fourth Craps, which will be the game itself.

Die Class

The Die class will have the UML of:

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

The value of MAX_NUMBER is 6 (the largest value one can get by throwing a Die) and the value of MIN_NUMBER is 1 (the smallest value one can get by throwing a Die). The value of NO_NUMBER is any value which is not between 1 and 6.

The constructor Die() will initialize the number to be NO_NUMBER.

The method roll will randomly choose a number between 1 and 6 using one of the pseudo-random number generators provided by Java. The method getNumber returns the current value of number.

So to toss a die, we must call two methods, one to roll the die, and one to get the results.

One can see the ADT of Die.

One can download the class of Die by clicking here.

Craps Class

The class will look be as follows:

	 _________________________________
	|      Craps			  |
	|_________________________________|
	|  - Die dieOne 		  |
	|  - Die dieTwo 		  |
	|  - boolean resultOfGame	  |
	|_________________________________|
	|  + Craps()			  |
	|  + void rules()		  |
	|  + void play()	   	  |
	|  + boolean getResultOfGame      |
	|  - int rollDice()		  |
	|_________________________________|

A '+' means it is public and a '-' means it is private. The three (data) members of the class are (1) the dieOne first die thrown, (2) the dieTwo second die thrown, and (3) resultOfGame which tells whether the user has won or lost the game. A win is stored as true and a loss as false.

This class will use a constructor, which was introduced in the second assignment. The constructor is called whenever a new object of the class is created, i.e., when one has a statement of the form:

Craps game = new Craps();	
In the above statement, what comes after the 'new' is a call to the constructor. The constructor which will create the dieOne and dieTwo. I.e., in the class declaration of Craps, you will have the member defined:

Die dieOne;

Die dieTwo;

but this will not create an actual Die. In the constructor, you will have

dieOne = new Die();

dieTwo = new Die();

This technique of defining the member, but only creating it in the constructor is standard in most Java programs. In a class, you just create the prototype of the members (data elements) that you need, and you either create or initialize them in the constructor. Since the other member (resultOfGame) values will be defined while playing the game, it will not be initialized inside the constructor.

There will be four other methods in the class, three public methods and one private.

The public methods are: (1) A method called rules which will output the rules of the game to the terminal window and (2) play which will actual play the game, and (3) a method getResultOfGame which will return true if the game was won and false otherwise.

The rules method will just be a collection of System.out.println statements.

One can see the ADT of Craps.

The method play

The method play will call the private method rollDice which will return a value between 2 and 12. (See below for a discussion of this method.) If the game is won or lost on the first throw, the resultOfGame is set (to be true if it 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 . The user is also notified of the result via a JOptionPane.ShowMessageDialog. (See below for a discussion of this.) The the procedure terminates (since resultOfGame has been assigned). A procedure terminates by returning to the procedure which called it. I.e., you will have a

return;

statement.

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 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. Once the outcome of the game is known, the value of resultOfGame is set and the procedure returns. (Before returning, the user is notified if they won or 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.

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 will send two outputs: (1) to the console window and (2)to the user after each throw.

We send the output to the console window via a System.out.println as we have done in the last 3 programs. So the above outputs would be just given by the System.out.print or System.out.println statments.

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 statments like

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

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.

The method rollDice

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. But the user who is playing the game does not need to know about the rolling of the die. (I.e., how it is implemented.) So we make it a private procedure known only to the class Craps. The procedure will roll each of the roll methods for the two die. 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 using the getNumber method, which returns the value.

InterfaceCraps Class

The UML of the InterfaceCraps will be:

		 _______________________________________
		|     InterfaceCraps			|
		|_______________________________________|
		|   - JButton rules			|
		|   - JButton play			|
		|   - JButton exit			|
		|   - Craps game			|
		|   - int numberOfGames			|
		|   - int numberOfWins 			|
		|_______________________________________|
		|   + InterfaceCraps()			|
		|   + void actionPerformed(ActionEvent)	|
		|_______________________________________|



The constructor of the interface class should:

(1) set the size and location of the JFrame, as well as set its layout manager to be FlowLayout.

(2) create the interface of the three buttons and add them to the frame.

(3) have the ActionListener listen to the three buttons.

(4) create the object game of the craps game and initialize the numberOfGames and numberOfWins to be zero.

The actionPerformed method should do the following:

(1) If the rules button is clicked, it should call the method of rules, i.e.,

game.rules();

(2) If the play button is clicked, is should call the method of plays and see if the player won or lost. I.e.,

game.plays(); boolean result = game.getResultOfGame();

and result being true means that the player won, while false means that the player lost. It should keep track of the total number of wins numberOfWins and the total number of games numberOfGames as they are played.

(3) If the exit button is clicked, it should notify the user how many games they won out of all of the games played, and terminate the program, i.e.,

System.exit(0);

One can see the ADT of InterfaceCraps.

The Driver Class

This class will have one method main which will be static. The method will consists of two lines of code:

(1) Create an object (say window) of the class InterfaceCraps

(2) Set the object to be visible:

window.setVisible(true);

I have given you an example of TestInterfaceCraps.java to download.

Extra-Credit:

(i) Modify the program to ask the user how many times (s)he wants to play the game. This will occur when the user clicks on the plays button. 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. Each time the plays is clicked, it will reset the number of games and wins to be zero before playing.

(ii) Have a JButton on the GUI interface which will ask the user if the output of each game will be shown to the user. I.e., whether the System.out.print and the JOptionPane.showMessageDialog will be given or not. If the user chooses to surpress the output, then only the result of the game is shown.

One will need a new member in Craps to record whether output is to be suppressed or not and a method to set its value.

(iii) Combine Extra-Credit (i) and (ii) so that one can run 1000 games and see what the percentage of winning is without completely filling up the console window. For this, you should also keep track of the total number of games played. I.e., in part (i), one resets the number of games played and wins for each click of the play JButton to zero. Upon clicking exit, one should show the user the total number of games played (for all of the times play was clicked) and the total number of wins for all the times and the percentage.

(iv) Add a JTextArea object to the JFrame, and have all of the output which was going to the console window be sent to this JTextArea.

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. It should be posted under the appropriate assignment. Also indicate how many hours you worked on the program in the Readme.txt file.


Homepage of CTI School back to 211 homepage