HW #6: Yahtzee
Part 2 -- due 11:59pm Tuesday, March 6
Remember to run update21 to get the files needed for this
assignment. The program handin21 will only submit
files in the cs21/homework/6 directory.
Your programs are graded on both correctness and style. Please
review the comments regarding programming style on the main page
Some of the problems will have optional components that allow you to
further practice your skills in Python. Optional portions will not be
graded, but may be interesting for those wanting some extra challenges.
Warmup Problems
If you would like some more practice with
while loops, functions,
and
if statements, we have posted some smaller
warmup problems that you can try. Working on
these problems will help you practice the core concepts before working on
the larger assignment. They are entirely optional.
Yahtzee Part II
In the second part of this two part assignment, you will extend your
Yahtzee program to play a full game of Yahtzee. In addition to the code
you wrote last week, you will make use of the
ScoreCard class installed on the CS system. You can use
import ScoreCard and help(ScoreCard) to learn more about
this class and its methods. You will also need a small function called
scoreList that we put in the update21 directory.
Before working on this assignment, copy your yahtzee.py file from your
homework/5/ to your homework/6/ directory. Modify only the
copy in homework/6/. You may also want to import the
ScoreCard class and play with the member functions before using
the class in your code. Some sample code is shown below.
from ScoreCard import *
s = ScoreCard()
s.setScore(0, 100)
res = s.getScore(0)
if s.isFree(0):
print "Category is free"
else:
print "Category is taken"
If you want to work on the assignment on your home computer, you will
need the graphics and ScoreCard library files. You can
Download these files, but remember that only files that
are in your homework/6/ folder in computer science will be graded. You
do not need to upload the ScoreCard and graphics file when you upload
your assignment.
Modify your program to implement the following.
P0: Modify reroll()
Modify the reroll function to ensure valid input. If the user enters
invalid dice positions, warn the user that some choices will be ignored,
or prompt the user to enter only valid input. The program should not
crash if the user enters invalid choices.
P1: Implement choosing a category
Implement the function
chooseCat(s, dice) that takes as input two
parameters: a
ScoreCard s and a list of dice values
dice.
The
chooseCat function should prompt the user to choose a
category from those listed on the scorecard and then update the
score card once an acceptable category has been chosen.
Your function must check that the user entered a valid category. A
valid category must be a letter value between 'N' and 'Z' and must be a
category that has not already been used. Certain member functions in the
ScoreCard class can be used to check if a category has already
been selected. In each of the member functions, the categories are
indexed by a numerical value between 0 and 15 inclusive. The mapping of
categories to numbers can be found using help(ScoreCard). If a
user selects an invalid category, inform the user of the mistake and
prompt the user again until they enter a valid answer.
P2: Modify main()
Once you have implemented
chooseCat modify your
main() function to play a full game (13 turns) of
Yahtzee. You will need to create an instance of the
ScoreCard
class. After every roll or re-roll, you should call the method
displayPossibles in the
ScoreCard class to update the
scorecard with the appropriate values, at the end of the final re-roll,
call your
chooseCat function so the user can select a category in
which to score his/her roll.
P3: Bonuses and Total Score
In addition to the standard scoring for each category defined in
last week's assignment, Yahtzee has two bonus
scoring category. The first bonus is called the
Upper Bonus and
is awarded to a player who scores a total of 63 or more points in the
first six categories (Ones, Twos, Threes, Fours, Fives, and Sixes). This
upper Bonus is worth 35 points. Implement a function that takes a
ScoreCard as input and sets the
Upper Bonus category (number 13
in the ScoreCard class) to 35 points if the user has 63 or more total points
in the first six categories.
A Yahtzee Bonus is awarded to player who has already
scored 50 points in the Yahtzee category and rolls another Yahtzee. On
any second or subsequent Yahtzee, the player is awarded a 100 point
Yahtzee bonus and the player must still select a category other than
Yahtzee (which is already taken) to play his/her score. This Yahtzee
Bonus can be awarded multiple times. For example, if a player rolls
[2,2,2,2,2] an places this roll in the Yahtzee category, he or she
receives 50 points for this Yahtzee. If later in the game the player
rolls [3,3,3,3,3], he/she is awarded 100 points and must place this roll
in another free category other than Yahtzee. If the player places the
roll [3,3,3,3,3] in the Threes category, he/she receives an additional 15
points, or 115 total for that turn. Implement a function that takes a
ScoreCard and a list of dice roll values as input and awards a Yahtzee
bonus if the roll is a second or subsequent Yahtzee. To update the
Yahtzee Bonus slot on the score card, use category number 14 in the
ScoreCard class. Make sure you do not assign a Yahtzee Bonus to a player
on the first Yahtzee.
After implementing both bonuses, modify chooseCat or
main to check for both bonuses and update the ScoreCard
accordingly. Also compute the total value for the entire ScoreCard. The
total value is stored as category number 15 in the ScoreCard class.
P4: Test
Test your program to make sure it works. Play a few games of Yahtzee, or
have your friends play. Make sure the program does not crash even if the
user enters bad input values. If you want to quit the game or start over
in the middle of playing a game, you can press
Ctrl-C to exit.
Note that you will need to use
except IndexError:,
except
NameError:, etc. instead of just
except: so that you do not
catch the
Ctrl-C keypress.
Optional Components
As noted above, these questions are
NOT required to receive full credit.
There are many extensions you could add to this program. Here are a few
we thought might be interesting.
Two player mode
Modify your program to support two or more players. Each player should
have their own score card. Adding two player support is a straightforward
extension if designed properly. You main only need to modify the
main function.
Early category selection
Currently, a user is asked to reroll the dice twice on each turn. A user
can choose to reroll no dice on each reroll, but a better option might be
to allow the user to pick a category after the first roll if the user
decides not to reroll any dice. Implement this feature.
Cheat mode
Testing Yahtzee can be difficult as each roll is random. Implement a cheat
mode where you can select the dice value on each roll. How high of a score
can you get including bonuses in this cheat mode? You can either switch
cheat mode on or off by changing the code and running the program again,
or hiding the cheat mode as an "Easter egg" — an undocumented
feature in the program. For example "cheat" is an unlikely input for which
dice you want to reroll, but perhaps this secret code could toggle the
cheat mode in the reroll function. Be creative.
Graphical dice
Add a graphical window that displays actual dice. You may want to refer to
Chapter 10.6 in the text for an example of drawing dice (The example uses
classes, but you should be able to glean enough information from the
example to draw your own dice without using classes.) If you want to
extend the graphical display further, allow the user to select the dice
with mouse clicks instead of the keyboard.
High Score
Add an option to save your highest score. For this extension, you will
need to know how to read and write files. See Chapter 4.6 in
the text for an example of how to do this.