Electronic handin will be available after 5PM on Thursday.
ANITROC! is a dice game played with five standard 6-sided dice. The game consists of a sequence of rounds. In each round, the player has up to 4 rolls of the dice to get the best combination possible. After each roll, the player decides which dice he/she wants to roll again. The player does not have to reroll all five dice, but the player can do so. The player can choose not to reroll any dice. The round ends when the player rolls 4 times or chooses not to reroll any dice. Once the round ends, the final set of five dice are scored according to the table below.
COMBINATION | EXPLANATION | SCORE |
ANITROC! (FIVE OF A KIND) | All five dice match each other. | 200 |
FOUR OF A KIND | Four of the five dice match each other. | 100 |
BIG STRAIGHT | The five dice form the numerical sequence 1-2-3-4-5 or 2-3-4-5-6 in any order. | 50 |
FULL HOUSE | Three of the five dice match each other and the other two dice match each other. | 40 |
THREE OF A KIND | Three of the five dice match each other. | 30 |
SMALL STRAIGHT | Four of the five dice form the numerical sequence 1-2-3-4, 2-3-4-5 or 3-4-5-6 in any order. | 20 |
TWO PAIRS | Two of the five dice match each other and two of the remaining dice match each other. | 10 |
The player gets the score for the best combination possible. For example, if the player's final dice roll is 2-2-2-6-2, then the player gets 100 points for a Four Of A Kind, even though the roll qualifies for Three Of A Kind also.
If the player does not match any combination in the table, the player receives one "strike" and gets no points for that round. The game ends once the player has a total of three strikes.
SET-UP: Download a copy of the Program5.zip file. Unzip the file and move the folder DiceGame into your workspace for Eclipse. Then, using Eclipse, start a new Java project. Select "Create a new project in workspace" and enter the project name DiceGame. You should see a warning at the bottom of the dialog box that says "The wizard will automatically configure the JRE and the project layout based on the existing source." This is ok. This means that Eclipse found a folder with source code in it already so it will start the project with that code.
In this project, open the DiceGame class and you will see that some code is provided for you as a starting point. The game includes two arrays. The die array is an int array that holds the value rolled for each of the 5 die. The reroll array is a boolean array that holds whether the player wants to reroll the corresponding die (true) or not (false). In addition, there is a variable game which is a Grid object that displays the dice in a graphical window for you as you play:
In order to display the current set of dice, you can call the supplied displayDice method as follows:
displayDice(game, die);
You need to complete the main method so it plays the game according to the rules above.
public static int getScore(int[] die)
In this method, you will need to determine what score to return for the given set of dice. To make this easier, you should sort the values in the array. You can do this easily by calling
Arrays.sort(die);
NOTE: To use Arrays.sort, you need to import java.util.*
final int MAX_ROLLS_PER_ROUND = 4; final int MAX_STRIKES_PER_GAME = 3;
Your code should use these constants rather than the values 4 and 3. This way, if you want to change the game, you can just change the numbers and recompile and your game should still work.
This program has more complex logic than we've had before, and it combines a number of concepts from our class all together (if statements, loops, user input, booleans, arrays). If you try to write the entire program and then test it, you will probably have a hard time completing the assignment. Instead, work in stages and get each feature to work before moving on. This is what programmers do when they write complex software.
Start by writing the code to roll and reroll the dice. Don't worry about the scoring or strikes yet. Make sure the logic is correct so that the player can roll up to 4 times per round and the player can reroll any combination of dice.
Then write the method to compute and return the score for a set of five dice and update your main method to call this method and update the player's score and/or number of strikes appropriately. Test this for correctness a number of times.
Then embed the code to play one round into a loop so the player can play round after round until the player receives 3 strikes.
Compile and test that your program runs correctly. If your program does not compile and run, you will not receive full credit for your assignment.
Click HERE for a sample run of the game to show you what the input and output should look like. (The dice images are not shown here, but they should appear when your program is run. The sample output shows the corresponding dice values whenever the dice images are shown.) Try to format your output as closely as possible to the sample output for maximum credit.
Your program must contain a comment at the beginning of your program with your name, andrew id, and section.
You should also use standard Java indentation and naming style for identifiers (Examples: ClassName, variableName, methodName()). You should comment your code to explain what a set of statements are doing. For example:
// Computing if player has five-of-a-kind
See the course website for instructions on how to hand in your program. Please zip your project folder that is created in Eclipse. This makes it easier for us to grade your work.
Remember that the work you submit must be your own. Also, late hand-ins are not accepted. Please plan ahead and submit early to avoid server overload at the deadline. The deadline is based on the server's clock, not your clock. Please do not email your code to your instructor or course assistant as your official hand-in; these will not be graded.