Created
November 1, 2021 16:33
-
-
Save altilunium/2442184953ed425a50ad213aa890513d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fractions import Fraction | |
nodes = dict() | |
class Node: | |
def __init__(self,id,p1,p2,l,r,par,gl,gr): | |
self.id = id | |
self.p1 = p1 | |
self.p2 = p2 | |
self.l = l | |
self.r = r | |
self.par = par | |
self.gl = gl | |
self.gr = gr | |
def groot(self): | |
if(self.id == 0): | |
return 1 | |
else: | |
return (self.p1/self.p2) * nodes[self.par].groot() | |
def groot_top(self): | |
if(self.id == 0): | |
return 1 | |
else: | |
return (self.p1) * nodes[self.par].groot_top() | |
def groot_bottom(self): | |
if(self.id == 0): | |
return 1 | |
else: | |
return (self.p1) * nodes[self.par].groot_bottom() | |
def __str__(self): | |
return "%s/%s [%s-%s]" % (self.p1,self.p2,self.l,self.r) | |
p1_foe = Node(1,1,3,2,2,0,1,0) | |
p1_me = Node(2,2,3,1,3,0,0,1) | |
tid = 2 | |
nodes[0] = Node(0,0,0,0,0,0,0,0) | |
nodes[1] = p1_foe | |
nodes[2] = p1_me | |
nodequeue = [] | |
nodequeue.append(nodes[1]) | |
nodequeue.append(nodes[2]) | |
n = 4 | |
for x in range (50): | |
nodetemp = [] | |
while len(nodequeue) > 0: | |
c = nodequeue.pop() | |
#choose foe | |
tid = tid + 1 | |
tp1 = c.l | |
tp2 = c.l + c.r | |
tl = c.l + 1 | |
tgl = c.gl + 1 | |
tr = c.r | |
tgr = c.gr | |
nnode = Node(tid,tp1,tp2,tl,tr,c.id,tgl,tgr) | |
nodes[tid] = nnode | |
nodetemp.append(nnode) | |
#choose me | |
tid = tid + 1 | |
tp1 = c.r | |
tp2 = c.l + c.r | |
tr = c.r + 1 | |
tgr = c.gr + 1 | |
tl = c.l | |
tgl = c.gl | |
nnode = Node(tid,tp1,tp2,tl,tr,c.id,tgl,tgr) | |
nodes[tid] = nnode | |
nodetemp.append(nnode) | |
nodequeue = nodetemp | |
losesum = 0 | |
nodetemp2 = [] | |
while len(nodequeue) > 0: | |
c = nodequeue.pop() | |
#lose condition, kalah 2 suara atau lebih | |
if (c.gl >= (c.gr + 2) ): | |
#print("lose!") | |
losesum = losesum + c.groot() | |
#print(c.groot()) | |
nodetemp2.append(c) | |
n = n +1 | |
winproba = 1 - losesum | |
winproba = Fraction(winproba).limit_denominator() | |
print("result [" + str(n) +"] : "+ str(winproba)) | |
nodequeue = nodetemp2 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment