Computer Sciences 302: Project 1
A dicey proposition
Latest news
Check back often for updates!
Introduction
In this project, you will write a program that plays a simple gambling dice game. As part of this undertaking, you will implement classes that model the state and rules of the game, a class that models a player’s assets, and an application class that instantiates and manipulates these objects, takes input from the player, and creates a game.
In this project, you will rely on your big-picture knowledge of implementing classes as well as your detailed understanding of conditionals, comparison operators, and loops.
This project will be worth 5% of your final grade and must be handed by 11:59 PM on Thursday, 7/19. You must work in pairs for this assignment; please get started early.
Rules of the game
Basics
There are two players: one is controlled by a human, and one is controlled by the computer. Each player starts with $100. The game consists of multiple turns, which involve betting and rolling dice.
At each turn, the human player will decide some amount to bet. This amount must be an integral number of dollars and must be less than or equal to the amount in the player’s current purse and less than or equal to the amount in the computer’s current purse. The players will then each roll three dice. Each player may roll all three dice up to five times until a scoring combination (see below) is reached. A player may not, however, redo a roll containing a scoring combination in an effort to get a better score.
Once both players have rolled scoring combinations, the scores of their rolls are compared, with the winner taking the amount of the bet from the loser’s purse. If a player cannot roll a scoring combination in five tries, she automatically loses the turn and forfeits the bet amount to the other player. In the event of a tie, or if neither player can roll a scoring combination in five tries, the turn is cancelled and a new turn begins with the human player placing another bet.
The game ends when one player loses by running out of money.
Scoring
The following are considered scoring combinations:
- A one, a two, and a three. This roll is called “ground” and has the lowest possible point value; any other scoring combination will beat this roll.
- A four, a five, and a six. This roll is called “top” has the highest possible point value; any other scoring combination will lose to this roll.
- A “pair” of dice with the same number (e.g. two fours) and a third die. The point value of this roll is the value of the pair, or the value of the third die (that is, the one not involved in the pair), whichever is less. A roll of 2,2,3 would be worth three points (since 2 + 2 == 4 and 3 > 4); a roll of 1,1,6 would be worth 2.
- A “three-of-a-kind.” A roll with three like dice will beat any roll except a top (which beats anything except another top) or a three-of-a-kind with an equal or larger die.
Note that you will roll three dice and that you must be able to recognize scoring combinations wherever they appear. (One way to say this is that it is the combinations that matter, no matter what permutation the dice appear in.) Your program will probably consider the three dice in some order, but the combinations are independent of the order you consider the dice in. Therefore, 1,1,2 is a pair worth two points; so are 1,2,1 and 2,1,1. Likewise, 5,4,6 is considered a “top” roll — as are 6,5,4; 4,5,6; and 6,4,5. Hint: You will probably want to sort your dice from lowest to highest before searching for combinations.
Implementing your game
We have decided on the design of the game for you; it will be up to you to implement the classes and methods we have described. You must implement all of the methods that we list in the class descriptions below. This means that you must provide method declarations for methods with the exact same names, signatures, and advertised behaviors as the ones we list for each class below. However, you may add additional instance or static methods as well if necessary. (You may also implement additional helper classes, but only if you discuss your design decisions with your instructor or one of the consultants first.)
You will implement the following classes:
- Player. This class models a player in the game (whether human or computer), and keeps track of how much money is currently in that player’s purse; it provides methods to inspect the balance, inspect the player’s name, and transfer money between two players.
- Combination. This class models a roll of three dice. It provides a method to check whether or not a roll corresponds to a scoring combination and a method to calculate the score.
- DiceGame. This class models the dice game; it has as data members the two players. It provides methods to set up the game, check for winning conditions (i.e. one player is busted), print messages about the state of the game, and play a single turn.
- DiceApp. This class is an application class (i.e. one that provides a main method); it will ask the user for names of the human and computer players, create Player objects, create a DiceGame object, and manage the state of the game. When a game is over, it will ask the user if she wants to play again, and repeat the preceding steps if so.
We have provided the following class for you:
- Die. This class models a six-sided die.
You can download the Die class here; to use it in your programs, add the JAR file to your project in Eclipse and import the Die class into your namespace with import threeohtwo.Die.
What to hand in
Hand in copies of your Player.java, Combination.java, DiceGame.java, and DiceApp.java files. Each file should have a header comment indicating your name. You should submit a README.txt file with your name and the name of your partner. Finally, you should submit a plain text file (not a PDF, RTF, or DOC file) with your answers to the questions from the “Questions” section of this assignment.
This project must be handed in by 11:59 PM (CSL time) on Tuesday, 7/18.
Hints
- You'll want to use a Scanner (constructed with System.in) to get input from the user and the parseInt() method of class Integer to convert from a String into an int.
Questions
- How would your program have to change if you allowed players to re-roll to try and get a better score? Which parts of your program would become more complex?
- One downside to the game is that the human player can always eliminate the CPU player in one lucky turn. Propose a rule change that would increase the length of games.
- List all of the accessor and mutator methods in each class you implemented.