GOALS: The
purpose of this activity is to gain exposure with using objects of the
Random class, while loops controlling flow based on user input, and
some String methods.
Be sure to add
the names of all members of
your group to the top of the Yahtzee.java file.
Getting Started: You should navigate to
G:\Lintont\introcs\chapter5,
grab a copy of the
activity5
folder, and copy it to your H:\ drive. Start BlueJ, open the activity5
project and add the names of all members of your group to the top of
the Yahtzee.java file. You will submit the Yahtzee.java file (one per
group) via Blackboard.
- In BlueJ, create a Yahtzee object and call its tester() method.
This method uses a few (new) methods from the InputReader class that
get input from the user. You can look at the interface version of the
InputReader class, to see what methods it offers, and a brief
description of them. You may then need to look at the source code of
the InputReader class as well. Look at the source code (in
Yahtzee.java) for the tester() method. Some of the input method calls
provide a question mark at the end of their parameter String, others
(the last one) do not. Pay careful attention to this, as you will want
to complete the Yahtzee.java file in a way that prints question marks
in the usual fashion (they are there when they should be, and there are
not 2 of them for a single question).
- Now call the start() method of your Yahtzee object, to get a feel
for how it works and responds to various responses you might give it.
Try answering the would you like to play again questions with things
like "maybe" or "sure thing". Can you answer with anything other that
"yes" or "no" and not get an error message? Be careful here, you might
have to look at source code to determine this answer correctly.
- We want a method that rolls 5 dice and stores the results in the
field rolls (an array of
5 ints). This is similar to the code I wrote
in class Wednesday. You can use a for loop to do this. Near the middle
of the Yahtzee.java file is a header for the rollDice() method. Write
the body of that method. Be sure to use rolls.length, instead of 5 in
this method (just in case we alter rolls later, and need to roll a
different number of dice. Your rollDice()
method can simply call the
rollIt() method to
produce the values of the dice.
- Now modify the body of the while loop in the start() method, so
that it calls the rollDice()
method (so 5 dice get rolled and saved in
the array rolls), print
out the values of these rolls (call the
printOut() method),
and then asks if the user would like to roll again.
- In the game of Yahtzee, you roll 5 dice, and then you decide for
each die, whether to keep it, or re-roll it. Eventually (after 2
chances at re-rolling), you compare your 5 dice to standard outcomes
(like 5 of a kind, a straight, or 2 pair). For us to implement this
sort of behavior, we need a method say reRoll() that will take an int
parameter, and re-roll the die with that index. This method will not
return any value, and it will simply replace rolls[i] with a new value
(from calling the rollIt()
method), whenever the input parameter i is
between 0 (inclusive) and rolls.length (exclusive). Write the reRoll()
method at the bottom of your Yahtzee.java file. Test it out by rolling
all 5 dice and then calling your reRoll()
method with various input
values (some from 0 to 4, others outside this range).
- In order to utilize this reRoll()
method, we will need to ask the
user 5 questions (do they want to re-roll die 1, die 2, die 3, die 4,
and die 5) and respond accordingly (by doing nothing if they do NOT
want to re-roll that die, and re-rolling that die when they say yes).
Modify your start()
method so that it rolls all 5 dice (as before) and
then asks these 5 questions, and re-rolls each die that the user would
like to re-roll. Ideally, you could do this with a loop, but if you
have to (for a few points less), you can simply write code that handles
die1, then die 2, and so on. Once you've asked them these questions and
re-rolled the appropriate dice, be sure to printout the results and ask
if they would like to play again. One more note, in the gane of
Yahtzee, you re-roll all of your dice at one time (those that you chose
to re-roll anyway), so while you should probably re-roll each die
immediately as you ask the user these questions, you should NOT print
out the new values, until after they've decided whether or not to
re-roll each of the dice.
- In the game of Yahtzee, you roll all 5 dice, pick any subset (all
5 is allowed) of the 5 dice to re-roll, and then pick any subset (again
all 5 can be re-rolled, and die that were left alone the first time can
be re-rolled on this second pass) to re-roll a second time. Your goal
is
to get "poker-hands" (full houses, four of a kind etc.) with the 5 dice
after all three "rolls" have been completed. Modify your start method
so that it
incorporates this second "re-rolling", therefore mimicing the game of
Yahtzee itself.
- Turn in your Yahtzee.java file on Blackboard (under assignments).