CPSC 120 Assignment #4
Due Wednesday November 7, 2007 by 4:00 p.m.
Craps is a popular casino game played by betting against the outcome
of rolling a pair of dice. While there are many different variations
to the game, in this assignment you will create the classes to implement
a relatively simple version.
For our purposes, the rules of craps are defined as follows:
- If you roll a 7 or 11 on the first try, that is
called a 'Natural': you win.
- If your first roll is not a Natural, the value of your roll is
considered the 'Point'
- Subsequent rolls attempt to repeat the Point before rolling a Natural.
You should continue to roll the dice until either a Point or a Natural is
rolled. If you roll the Point, you win. If you roll a Natural, you lose.
To complete this assignment, you are responsible for creating three java
files that are part of the assign4 package.
The DicePanel is a special JPanel that is responsible for representing
your dice. The DicePanel should meet the following specifications:
- Two Die objects
You need to save the class Die.java from your
textbook (it is on pages 164-165 - an example program that uses
Die objects is on page 163). You will not change Die.java - you will just use
it. You should declare the two instance variables die1 and die2
to be of type Die.
- Two JLabels
These labels will be used to show an image displaying the standard pipped
representation of a six-sided die. You can find these images in a file
called DiePips.tgz . You should save this file to
your Assignments folder and untar it. This will create a directory called
"pics" that contains the six image files needed to represent the six sides
of the die.
- A constructor with no parameters is responsible for:
- Instantiating the instance data.
- Setting the labels to the image corresponding to
the face value of the dice
- Adding the labels to the panel
- Setting the background color of the panel.
- A rollDice method that rolls the dice and updates the labels
to reflect the outcome. The method should not have any parameters.
- A getTotalRoll method that returns the total of the
face values on the two dice (this method DOES NOT roll the dice
again - it just returns the total of the current face values).
The craps panel is a special JPanel that displays the dice, buttons and labels
needed to play a simple game of craps. Your CrapsPanel should adhere to the
- A Dice Panel
You can't play Craps without a pair of dice.
- Two JButtons
One button is used to roll the dice, the other is used to start a new game.
Both of these buttons should be appropriately labeled so that the user
understands what happens if they are pushed.
- A Button Listener
A single button listener object should be assigned to both buttons.
- Two Labels
One label clearly identifies the current "Point" value (when appropriate),
the other displays the outcome of the game (when appropriate).
- An integer to track the current "Point"
- A boolean to track if the game has ended.
A constructor with no parameters is responsible for:
- Instantiating the GUI Components, and arranging them on the panel.
Note: you may need to introduce additional nested panels (Similar to
the ATM GUI).
- Establishing the listener for the Buttons.
- Ensuring that the background color is consistent for the panel and any
A ButtonListener class should be defined to respond to button presses.
- Pressing the "New Game" button should result in resetting all of
the necessary instance data.
- Pressing the "Roll" button should roll the dice and then use the
outcome to determine the meaning according to the rules of Craps. The current
state of the game (i.e. the point and/or the outcome) should be conveyed by
adjusting the appropriate labels.
- If the game has already been won or lost and "Roll" button is pushed,
a message should be displayed prompting the user to Start a new game.
This is an application that puts the CrapsPanel
into a Frame and displays it. It should be modeled after Splat.java (p186) or
one of the applications from lab5.
Suggestions for Doing the Assignment
Like any programming task, you should begin by decomposing the problem
into a collection of smaller, more managable tasks and working incrementally.
For this assignment, you
- Create DicePanel for modeling and displaying a pair of dice.
- Create CrapsPanel - adding only the DicePanel
- Create CrapsApp
- Run your program to verify that you can get dice to appear on your GUI.
(It they don't, check the location of the image files and the path you are
using in your program!)
- Add the button that rolls the dice. Verify that this works.
- Add code to the program that allows you to play a single game of Craps.
- Add the second button and the logic to allow multiple games.
- Thoroughly test your program!! Be sure it meets all specifications.
Your project will be graded on style, documentation and correctness.
Some stylistic aspects you should pay attention to include the use of
constants (where appropriate), identifier names, and the use of whitespace
and proper formatting.
You should also make sure that your submission does not contain uneccessary
code, e.g. extra assignments or lines of code
that have been commented out. Fifteen percent of the
final assignment grade will be for style and
documentation. Documentation is a critical portion of any
computer program. Each method is expected to have an overall
description as well as
a description of each parameter and the return value - use javadoc
style documentation as we did in lab (use Eclipse to help you!).
Larger methods should
be internally documented with descriptions of the major steps.
The balance of the grade will come from correctness, which
includes meeting all of the specifications of the assignment.
A printed copy of your program files
(the Java files - DicePanel.java, CrapsPanel.java CrapsApp.java).
Tar all the files in your assign4 subdirectory and email the .tgz
file to your instructor with a subject of cpsc120 assign4.
Academic Integrity Reminder: Assignments are to be done on
your own. You may get help on the specifices of the assignment
from no one but the instructor. You may not show your program to
anyone else or look at anyone else's program or share ideas about
how to write the program.
After you have submitted your assignment,
you are invited to enhance your program to earn some extra credit.
I know that you will have lots of fun playing with your GUI craps as designed,
but to really make it fun, we need to add some money into the equation
(After all,this is a gambling game).
Implement the following features to earn extra points.
- Add an Account object to your CrapsPanel. Initialize it with $500.
- Starting a new game will use some money from the account to place
a bet. You can either use a fixed amount or you can use a textbox
to allow the user to specify an amount.
- Winning a game results in the following payouts:
- Winning with a Natural: 3x the bet amount
- Getting the point in less than 4 rolls: 1.5x bet amount
- Getting the point in 5 to 8 rolls: 2x the bet amount
- Getting the point after 8 rolls : 5x the bet amount
improvements to the game are welcomed and will be rewarded.
If you are submitting bonus work you should create a text file (Bonus.txt)
explaining the enhancements that you made. This file should be tar'ed
together with your updated code and sent as an independent e-mail. Bonus
material will not be accepted if the original submission does
not meet at least 90% of the original specifications.