1 """YootNoriSimulation
2 """
3
4 import random
5
6 class YootNori:
7 pass
8
9 class Mo(YootNori):
10 def getUpCount(self):
11 return 4
12 def getName(self):
13 return "MO"
14 def getPossibility(self):
15 return 1
16
17 class Do(YootNori):
18 def getUpCount(self):
19 return 3
20 def getName(self):
21 return "DO"
22 def getPossibility(self):
23 return 4
24
25 class Ge(YootNori):
26 def getUpCount(self):
27 return 2
28 def getName(self):
29 return "GE"
30 def getPossibility(self):
31 return 6
32
33 class Gul(YootNori):
34 def getUpCount(self):
35 return 1
36 def getName(self):
37 return "GUL"
38 def getPossibility(self):
39 return 4
40
41 class Yoot(YootNori):
42 def getUpCount(self):
43 return 0
44 def getName(self):
45 return "YOOT"
46 def getPossibility(self):
47 return 1
48
49 mapOfUpCount = {4:Mo, 3:Do, 2:Ge, 1:Gul, 0:Yoot}
50 mapOfYootName = {'MO':Mo, 'DO':Do, 'GE':Ge, 'GUL':Gul, 'YOOT':Yoot}
51
52 class DoingYootNori:
53 def __init__(self, aProbabilityUp):
54 self.probUp = aProbabilityUp
55 def throwOneYoot(self):
56 rand = random.random()
57 if rand > self.probUp:
58 return 0
59 else : return 1
60
61 def throwFourYoot(self):
62 yootlist = []
63 for i in range(4):
64 yootlist.append(self.throwOneYoot())
65 return yootlist
66
67 def judgeYoot(self, aYootList):
68 upcount = aYootList.count(1)
69 return mapOfUpCount[upcount]().getName()
70
71 def throw(self):
72 yootlist = self.throwFourYoot()
73 return self.judgeYoot(yootlist)
74
75 def getProbability(self, judge):
76 theYoot = mapOfYootName[judge]()
77 upProb = self.probUp
78 downProb = 1-upProb
79 upCount = theYoot.getUpCount()
80 downCount = 4-upCount
81
82 if upCount == 0:
83 theProb = downProb**downCount
84 elif downCount == 0:
85 theProb = upProb**upCount
86 else:
87 theProb = (downProb**downCount) * (upProb**upCount)
88 return theProb * theYoot.getPossibility()
89
90 def __repr__(self):
91 aString = ''
92 for aYoot in mapOfYootName.keys():
93 aString += ( aYoot + "'s probability is "
94 + str(self.getProbability(aYoot)) + '\n')
95 return aString
96
97 def returnMoAndGe(aProb):
98 thisGame = DoingYootNori(aProb)
99 probMo = thisGame.getProbability('MO')
100 probGe = thisGame.getProbability('GE')
101 return probMo, probGe
102
103 def statEqualMoAndGe(aOneProb, aStep):
104 probMo, probGe = returnMoAndGe(aOneProb)
105 probDiff = probGe - probMo
106 if probDiff < 0:
107 return aOneProb
108 else:
109 newProb = aOneProb + aStep
110 return statEqualMoAndGe(newProb, aStep)
111
112 if __name__ == '__main__':
113 myGame = DoingYootNori(0.71)
114 theYoot = myGame.throw()
115 print "You throw ", theYoot, '\n'
116 print myGame
117 print "The probability that equal Mo and Do is ",
118 print statEqualMoAndGe(0.5, 0.001)
119