-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDieRollerExample.txt
More file actions
51 lines (39 loc) · 1.27 KB
/
Copy pathDieRollerExample.txt
File metadata and controls
51 lines (39 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import random
#I roll dice. I don't care if it is for the console
#or for a fancy smancy GUI.
#I only roll a six sided die.
class DiceRoller:
def registerDiceViewer(self, dv):
self.dv = dv
def roll(self):
return random.randrange(6)+1
#I too roll dice. I too don't care if it is for
#the console or for a fancy smancy GUI..
#But, I need input from the DiceViewer.
class FlexibleDiceRoller(DiceRoller):
def roll(self):
n = self.dv.queryUser("What size die should I role? : ")
n = int(n)
result = random.randrange(n)+1
return result
#I present dice rolls. I don't care how the die are rolled.
#I am just responsible for displaying the results...
#...and I might need to ask the user for some info if the
#dr needs me to.
#I print to the console, but another viewer could 'extend' me
#into a fancy smany GUI.
class DiceViewer:
def registerDiceRoller(self, dr):
self.dr = dr
self.dr.registerDiceViewer(self)
def queryUser(self, str):
return input(str)
def roll(self):
print ( self.dr.roll() )
dv = DiceViewer()
print ("Attempting: DiceRoller")
dv.registerDiceRoller( DiceRoller() )
dv.roll()
print ("Attempting: FlexibleDiceRoller")
dv.registerDiceRoller( FlexibleDiceRoller() )
dv.roll()