Due: Monday 10/28 10:00am
Submission name: w18_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:
- Takes 3 arguments representing the (x, y) position and size of the Die.
- Initially set the
value
to0
.
Methods:
void display()
- If the
value
is0
, do not display the value at all.
- If the
int roll()
- Return the
value
after it is set to a random number.
- Return the
void reset()
- Set
value
back to0
.
- Set
Driver Modifications
Global variables:
- Add variables to keep track of the
target
,score
and if the player has not lost yet (is still playing).
New Methods:
void restart()
- Initialize the
target
,score
and playing variables. - Call
reset
on both dice.
- Initialize the
void updateScore(int roll)
- This is the main logic of the game.
roll
will be the result of a roll of two dice.- Modify
score
,target
and 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
updateScore
with the total. - If the player types
r
, call therestart()
method to start the game over.
void setup()
- Call
restart
instead 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 the images below for an example.