Kestable

Vermintide_dice_prob

Oct 28th, 2015
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.14 KB | None | 0 0
  1. #!/usr/bin/env
  2. # -*- coding: utf-8 -*-
  3. # © Konstantin Soin <[email protected]>
  4.  
  5. import pylab
  6. import random
  7. from decimal import Decimal
  8.  
  9. class Cube(object):
  10.  
  11.     def __init__(self, type='default'):
  12.         self.side_one = True
  13.         self.side_two = True
  14.         if type == 'default' or type == 2:
  15.             self.type = 'default'
  16.             self.side_three = False
  17.             self.side_four = False
  18.             self.side_five = False
  19.             self.side_six = False
  20.         elif type == 'fair' or type == 3:
  21.             self.type = 'fair'
  22.             self.side_three = True
  23.             self.side_four = False
  24.             self.side_five = False
  25.             self.side_six = False
  26.         elif type == 'four' or type == 4:
  27.             self.type = 'four'
  28.             self.side_three = True
  29.             self.side_four = True
  30.             self.side_five = False
  31.             self.side_six = False
  32.         elif type == 'win' or type == 6:
  33.             self.type = 'win'
  34.             self.side_three = True
  35.             self.side_four = True
  36.             self.side_five = True
  37.             self.side_six = True
  38.        
  39.     def change_type(self, type):
  40.         """Changes default cube type to selected: 3 or 4 or 6"""
  41.         if type == 'default' or type == 2:
  42.             self.type = 'default'
  43.             self.side_three = False
  44.             self.side_four = False
  45.             self.side_five = False
  46.             self.side_six = False
  47.         elif type == 'fair' or type == 3:
  48.             self.type = 'fair'
  49.             self.side_three = True
  50.             self.side_four = False
  51.             self.side_five = False
  52.             self.side_six = False
  53.         elif type == 'four' or type == 4:
  54.             self.type = 'four'
  55.             self.side_three = True
  56.             self.side_four = True
  57.             self.side_five = False
  58.             self.side_six = False
  59.         elif type == 'win' or type == 6:
  60.             self.type = 'win'
  61.             self.side_three = True
  62.             self.side_four = True
  63.             self.side_five = True
  64.             self.side_six = True
  65.    
  66.     def roll(self):
  67.         """Randomly roll the dice"""
  68.         roll = random.ranint(1, 6)
  69.         if roll == 1 or roll == 2:
  70.             return True
  71.         elif roll == 3:
  72.             return self.side_three
  73.         elif roll == 4:
  74.             return self.side_four
  75.         elif roll == 5:
  76.             return self.side_five
  77.         elif roll == 6:
  78.             return self.side_six
  79.    
  80.     def get_type(self):
  81.         print self.type
  82.    
  83.     def get_sides(self):
  84.         return [self.side_one, self.side_two, self.side_three, self.side_four, self.side_five, self.side_six]
  85.        
  86. class Cube_pull(object):
  87.     def __init__(self, loot_dices=0, books=0, grimoires=0):
  88.         self.cubes = []
  89.         self.cc = 7
  90.         for i in xrange(loot_dices):
  91.             self.cubes.append(Cube('fair'))
  92.             self.cc -= 1
  93.         for i in xrange(books):
  94.             self.cubes.append(Cube('four'))
  95.             self.cc -= 1
  96.         for i in xrange(grimoires):
  97.             self.cubes.append(Cube('win'))
  98.         for i in xrange(self.cc):
  99.             self.cubes.append(Cube())
  100.    
  101.     def get_vars(self):
  102.         vars = []
  103.         for side_0 in self.cubes[0].get_sides():
  104.             for side_1 in self.cubes[1].get_sides():
  105.                 for side_2 in self.cubes[2].get_sides():
  106.                     for side_3 in self.cubes[3].get_sides():
  107.                         for side_4 in self.cubes[4].get_sides():
  108.                             for side_5 in self.cubes[5].get_sides():
  109.                                 for side_6 in self.cubes[6].get_sides():
  110.                                     vars.append([side_0, side_1, side_2, side_3, side_4, side_5, side_6])
  111.         return vars
  112.    
  113.     def get_prob(self):
  114.         count_full = 0
  115.         count_zero = 0
  116.         count_one = 0
  117.         count_two = 0
  118.         count_three = 0
  119.         count_four = 0
  120.         count_five = 0
  121.         count_six = 0
  122.         count_seven = 0
  123.        
  124.         for var in self.get_vars():
  125.             count = var.count(True)
  126.             count_full += 1
  127.             if count == 0:
  128.                 count_zero += 1
  129.             elif count == 1:
  130.                 count_one += 1
  131.             elif count == 2:
  132.                 count_two += 1
  133.             elif count == 3:
  134.                 count_three += 1
  135.             elif count == 4:
  136.                 count_four += 1
  137.             elif count == 5:
  138.                 count_five += 1
  139.             elif count == 6:
  140.                 count_six += 1
  141.             elif count == 7:
  142.                 count_seven += 1
  143.         count_full = Decimal(count_full)
  144.         return [count_zero/count_full, count_one/count_full, count_two/count_full, count_three/count_full, count_four/count_full, count_five/count_full, count_six/count_full, count_seven/count_full]
  145.  
  146.  
  147. if __name__ == "__main__":
  148.     pylab.figure(1)
  149.     my_pull = Cube_pull(0, 2, 1) # loot dices, books, grimoireses
  150.     my_probs = my_pull.get_prob()
  151.     for p in my_probs:
  152.         print '%d win dices = %05r %%' % (my_probs.index(p), round(p*100, 2))
  153.     pylab.plot(range(8), my_probs)
  154.     pylab.show()
Advertisement
Add Comment
Please, Sign In to add comment