Guest User

Untitled

a guest
Jun 20th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.42 KB | None | 0 0
  1. rom random import randrange
  2.  
  3. class Poke:
  4.     #constructs the Poke which cotains a number of die with sides 4, 6, 8, 12, 20
  5.     def __init__(self):
  6.         self.List = []
  7.        
  8.     def add_die(self,die):
  9.         #adds a die to the Poke,Input must be tetrahedron,hexahedron,octahedron,
  10.         #dodecahedron, or icosahedron.Must be all lowercase, anything else will
  11.         #error. Must also be a string.
  12.         #Maximum Poke Capacity is 100, will not add after that.
  13.         die = die
  14.         q = len(self.List)
  15.         if q <=99:
  16.             if die == 'tetrahedron':
  17.                 self.List.append(4)
  18.             elif die == 'hexahedron':
  19.                 self.List.append(6)
  20.             elif die == 'octahedron':
  21.                 self.List.append(8)
  22.             elif die == 'dodecahedron':
  23.                 self.List.append(12)
  24.             elif die == 'icosahedron':
  25.                 self.List.append(20)
  26.             else:
  27.                 print "Error! Please follow input rules described in comments!"
  28.         else:
  29.             print "Exceeded Poke maximum capacity, Please create a new Poke"
  30.         return self.List
  31.    
  32.     def pick_die(self):
  33.         #method picks a random die
  34.         y = randrange(0,len(self.List))
  35.         return self.List[y]
  36.    
  37.     def sample(self,die):
  38.         # generates a random dice roll from pic_die, then displaces contents
  39.         #of the Poke
  40.         x = self.List
  41.         roll = randrange(1,die+1)
  42.         C4 = 0
  43.         C6 = 0
  44.         C8 = 0
  45.         C12 = 0
  46.         C20 = 0
  47.         total = 0
  48.         y = len(self.List)
  49.         for i in range(0,y):
  50.             if x[i] == 4:
  51.                 C4 = C4 + 1
  52.             elif x[i] == 6:
  53.                 C6 = C6 + 1
  54.             elif x[i] == 8:
  55.                 C8 = C8 + 1
  56.             elif x[i] == 12:
  57.                 C12 = C12 + 1
  58.             else:
  59.                 C20 = C20 + 1
  60.         print "Total 4-sided die:", C4
  61.         print "Total 6-sided die:", C6
  62.         print "Total 8-sided die:", C8
  63.         print "Total 12-sided die:", C12
  64.         print "Total 20-sided die:", C20
  65.         return roll
  66.    
  67.     def roll(self,die):
  68.         #Allows a single die to be created and then rolled. This not stored in
  69.         # the main Poke list. Input is the same as "add_die" method. Same rules
  70.         # apply.
  71.         if die == 'tetrahedron':
  72.             x = randrange(0,5)
  73.             print '4-sided die'
  74.             return x
  75.         elif die == 'hexahedron':
  76.             x = randrange(0,7)
  77.             print '6-sided die'
  78.             return x
  79.         elif die == 'octahedron':
  80.             x = randrange(0,9)
  81.             print '8-sided die'
  82.             return x
  83.         elif die == 'dodecahedron':
  84.             x = randrange(0,13)
  85.             print '12-sided die'
  86.             return x
  87.         elif die == 'icosahedron':
  88.             x = randrange(0,21)
  89.             print '20-sided die'
  90.             return x
  91.         else:
  92.             print "Error! Please follow input rules described in comments!"
  93.            
  94. def main():
  95.     #use main function ot operate the program by creating instances and using
  96.     #class methods.
  97.    
  98.     x = Poke() #creates poke instance
  99.    
  100.     four = x.add_die('tetrahedron') #adds all types of dice, and shows error
  101.     six  = x.add_die('hexahedron')  #lines 30-36
  102.     eight = x.add_die('octahedron')
  103.     twelve = x.add_die('dodecahedron')
  104.     twenty = x.add_die('icosahedron')
  105.     error = x.add_die(4)
  106.     print error
  107.    
  108.     for i in range (0,150): #shows that the max die the poke can hold is 100
  109.         a = randrange(0,5)
  110.         if a == 0:
  111.             x.add_die('tetrahedron')
  112.         elif a == 1:
  113.             x.add_die('hexahedron')
  114.         elif a == 2:
  115.             x.add_die('octahedron')
  116.         elif a == 3:
  117.             x.add_die('dodecahedron')
  118.         elif a == 4:
  119.             x.add_die('icosahedron')
  120.    
  121.            
  122.     y = x.pick_die() # calls pick_die, which picks a random die.
  123.     print(y)
  124.    
  125.     print x.sample(y) #outputs total number  of each dice created in poke and
  126.                       #shows that the returned value of the roll is correct
  127.                       #the sum of all die is less than or equal to 100.
  128.    
  129.     print x.roll('tetrahedron') # shows all possible outputs/returns for roll
  130.     print x.roll('hexahedron')
  131.     print x.roll('octahedron')
  132.     print x.roll('dodecahedron')
  133.     print x.roll('icosahedron')
  134.     print x.roll(5)
  135. main()
Add Comment
Please, Sign In to add comment