Posted: Wed Nov 4
Due: Thursday 11/5 10:00am
Submission name: w21_drats
The Game
You will use your work from yesterday to program the totally-made-up, not-based-on-a-real-gambling-game game of Drats. In Drats a player rolls 2 dice, attempting to roll as many times until they roll a 7 or 11, at which point they loose. Here are the rules:
- Initially, a target value is not set. A player cannot increase their score until the target is set.
- The first time a player rolls, they will set the target unless:
- Their roll (both dice) is 2, which is an immediate loss.
- Their roll is a 7 or 11, in which case they roll again.
- Once a target has been set, the player rolls and follows these rules:
- If they roll a 7 or 11, they lose.
- If they roll the target, the target gets erased and they go back to the target setting phase. Their score does not change.
- If they roll any other number, it gets added to their score.
In order to make this game, add the following to your program from yesterday:
Die Class Modifications:
Constructor:
- Initially set the
valueto0.
Methods:
void display()- If the
valueis0, do not display the value at all.
- If the
int roll()- Return the
valueafter it is set to a random number.
- Return the
void reset()new- Set
valueback to0.
- Set
Driver Modifications
Global variables:
- Add variables to keep track of the
target,scoreand if the player has not lost yet (is still playing).
New Functions:
void restart()- Initialize the
target,scoreand playing variables. - Call
reseton both dice.
- Initialize the
void updateScore(int roll)- This is the main logic of the game.
rollwill be the result of a roll of two dice.- Modify
score,targetand playing variables based on the rules of the game.
Modified methods:
void keyPressed()- Only allow the player to roll (spacebar) if they have not lost yet.
- Keep track of the result of the roll, and call
updateScorewith the total. - If the player types
r, call therestart()method to start the game over.
void setup()- Call
restartinstead of setting the new global variables. - Leave the Die creation code in.
- Call
void draw()- Add text to describe the current state of the game.
- If there is no target, display “no target”.
- If the player has lost, display, “you lose”.
- If the game is running, display the target and the score.
- See below for a playable example.