Creating a Dice Class¶
Have you played a game where you used dice? Let’s create a Die
class so that if we lose our physical dice we can still play. A die is one of a pair of dice.
Q-1: What attributes (data) would an object of the Dice class need to know? What questions should you be able to ask about it?
Q-2: What behaviors (methods) would we expect a dice class to have?
We would expect to be able to roll a die and get the value from the last roll. When we roll a dice we pick a random number from 1 to 6.
Let’s store all the values that were rolled in a list, roll_history
. We will add each value
rolled to the end of the roll_history
list and return the value
from the roll
method.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.