Craps - a Java Applet

The documentation for the "ideal" programming project:

Internal links:

This documentation was generated with the following command:

Description

Chapter 6, figure 6.9
First program to play Craps
Deitel and Deitel with additional comments by Nicholas Duchon
September 21, 2000
CMSC 305, section 7531
Instructor: Nicholas Duchon

This program will roll a pair of pair of dice and simulate a craps game.

Craps is a dice rolling game using a pair of six-sided dice. On each roll of a pair of dice, the player (the user of this program), may either win the game, lose the game or have an inconclusive roll. A game could possibly go one forever, but that is unlikely. The outcome of the game is determined according to the following rules: Only the sum of the two dice matters, the particular pattern is not relevant. Thus, (3,6), (4,5), (5,4) and (6,3) all have the same value, 9.

  1. On the first roll, a sum of 7 or 11 wins the game.
  2. On the first roll, a sum of 2, 3 or 12 loses the game.
  3. Any other sum on the first roll becomes the "point".
  4. On subsequent rolls, a roll of point before a roll of 7 wins the game.
  5. On subsequent rolls, a roll of 7 before a roll of point loses the game.


Design

(Mar 20, 2006) I have increased the documentation in this section.

This program runs as a JApplet, requires JDK 1.2 to compile and run.

The basic design is to use the init method to create the layout of the GUI interface and configure the action listener in this class to listen for button press events. Each event simulates the roll of a pair of dice. The rest of the program keeps track of the status of the current game, and when the current game is resolved, starts a new game. Since this program is run as an applet, exiting the program is accomplished by leaving the supporting HTML web page.

Uses one class, Craps, with a few methods (see the on-line documentation). It runs as a JApplet and implements ActionListener to react to the "roll" button presses.

The instance method init creates the display, and the instance method action listener causes the class to play by calling the instance method play(). The method play() plays the game by calling the instance method rollDice(), then updating the state of the dice and, if appropriate, noting wins, loses and points. The method rollDice uses a random number generator to get values for the two dice, change the GUI display fields, then return the sum of the values on the dice to the caller (play()). The current status of the game is shown using the Applet method showStatus (String), which changes the display in the bottom-left text field of the applet window.

The class instance variables describe the state of the screen and the state of the game. A game status indicator is used to save the information about whether the game has been won or lost is still continuing. If the game is won or lost, pressing "roll" will start a new game.


User's Guide

The program is located in the following directory:
http://nova.umuc.edu/%7Educhon/ideal/showPrivate/
which can be accessed through the web from my home page:
http://nova.umuc.edu/%7Educhon/

To run the program, click the "run applet" link on the main documentation page. Apparently some browsers do not update the status window on the bottom left for the game, so you may need to open the applet in a new tab or new window.

Click "roll" to roll the dice. The values in the various fields correspond to a game of craps.

If the game is won or lost, a new game is automatically started.

To exit the program, hit "back" in your browser.

This program has been test with Netscape Navigator 4.7, Java 1.3 plugin, in Win95, and seems to work fine.


Test Plan

Start the application, press the "roll" button and play often enough to check to see:

Lessons Learned

1. How to run a java swing applet using Netscape and the latest java plug-in.

2. How to use javadoc to create links to the source code and to get the applet running.



HTML Converter:

This is an extra section, but I leave it for historical interest.

If your project is, say, p4, then put all of the relevant files into /class/cs305b/98/www/p4 directory. Note that your default directory is /class/cs305b/98 if you are student number 98.
     See the p4 example for inserting links to documentation, the html file that will run your applet and the author flag.

     Run the following command:

          > javadoc -author *.java

     This will produce the documentation set, starting with index.html

     To make swing applets work with the latest version of the java plugin, you need to modify the test.html file, the file that you use to start the java applet. To do this, enter the following command:

          > htmlc test.html

     (or whatever you have called the appropriate html file.) Note that D&D seem to have chosen to use lower case html files to test their classes, as in craps.html will test the craps program, while
     Craps.html is the file produced by the javadoc command. This may be a reasonable approach. The actual command is:

     > java -classpath /users/faculty/duchon/converter/classes HTMLConverter
     I suggest that you make an entry in your .cshrc file aliasing the first part of the command as, say, "htmlc".

     Next, make sure that the files are readable through the net using:

          > chmod a+rx *

     or something similar.
 

     Finally, test the whole affair using netscape and/or internet explorer from some PC.