Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rom random import randrange
- class Poke:
- #constructs the Poke which cotains a number of die with sides 4, 6, 8, 12, 20
- def __init__(self):
- self.List = []
- def add_die(self,die):
- #adds a die to the Poke,Input must be tetrahedron,hexahedron,octahedron,
- #dodecahedron, or icosahedron.Must be all lowercase, anything else will
- #error. Must also be a string.
- #Maximum Poke Capacity is 100, will not add after that.
- die = die
- q = len(self.List)
- if q <=99:
- if die == 'tetrahedron':
- self.List.append(4)
- elif die == 'hexahedron':
- self.List.append(6)
- elif die == 'octahedron':
- self.List.append(8)
- elif die == 'dodecahedron':
- self.List.append(12)
- elif die == 'icosahedron':
- self.List.append(20)
- else:
- print "Error! Please follow input rules described in comments!"
- else:
- print "Exceeded Poke maximum capacity, Please create a new Poke"
- return self.List
- def pick_die(self):
- #method picks a random die
- y = randrange(0,len(self.List))
- return self.List[y]
- def sample(self,die):
- # generates a random dice roll from pic_die, then displaces contents
- #of the Poke
- x = self.List
- roll = randrange(1,die+1)
- C4 = 0
- C6 = 0
- C8 = 0
- C12 = 0
- C20 = 0
- total = 0
- y = len(self.List)
- for i in range(0,y):
- if x[i] == 4:
- C4 = C4 + 1
- elif x[i] == 6:
- C6 = C6 + 1
- elif x[i] == 8:
- C8 = C8 + 1
- elif x[i] == 12:
- C12 = C12 + 1
- else:
- C20 = C20 + 1
- print "Total 4-sided die:", C4
- print "Total 6-sided die:", C6
- print "Total 8-sided die:", C8
- print "Total 12-sided die:", C12
- print "Total 20-sided die:", C20
- return roll
- def roll(self,die):
- #Allows a single die to be created and then rolled. This not stored in
- # the main Poke list. Input is the same as "add_die" method. Same rules
- # apply.
- if die == 'tetrahedron':
- x = randrange(0,5)
- print '4-sided die'
- return x
- elif die == 'hexahedron':
- x = randrange(0,7)
- print '6-sided die'
- return x
- elif die == 'octahedron':
- x = randrange(0,9)
- print '8-sided die'
- return x
- elif die == 'dodecahedron':
- x = randrange(0,13)
- print '12-sided die'
- return x
- elif die == 'icosahedron':
- x = randrange(0,21)
- print '20-sided die'
- return x
- else:
- print "Error! Please follow input rules described in comments!"
- def main():
- #use main function ot operate the program by creating instances and using
- #class methods.
- x = Poke() #creates poke instance
- four = x.add_die('tetrahedron') #adds all types of dice, and shows error
- six = x.add_die('hexahedron') #lines 30-36
- eight = x.add_die('octahedron')
- twelve = x.add_die('dodecahedron')
- twenty = x.add_die('icosahedron')
- error = x.add_die(4)
- print error
- for i in range (0,150): #shows that the max die the poke can hold is 100
- a = randrange(0,5)
- if a == 0:
- x.add_die('tetrahedron')
- elif a == 1:
- x.add_die('hexahedron')
- elif a == 2:
- x.add_die('octahedron')
- elif a == 3:
- x.add_die('dodecahedron')
- elif a == 4:
- x.add_die('icosahedron')
- y = x.pick_die() # calls pick_die, which picks a random die.
- print(y)
- print x.sample(y) #outputs total number of each dice created in poke and
- #shows that the returned value of the roll is correct
- #the sum of all die is less than or equal to 100.
- print x.roll('tetrahedron') # shows all possible outputs/returns for roll
- print x.roll('hexahedron')
- print x.roll('octahedron')
- print x.roll('dodecahedron')
- print x.roll('icosahedron')
- print x.roll(5)
- main()
Add Comment
Please, Sign In to add comment