Point based game

 avatar
unknown
python
17 days ago
1.0 kB
8
Indexable
def game2(p=6/10, n=10):
    #state = (A wins, B wins, winner of prev)
    states = [(a,b,c) for a in range(n) for b in range(n)
              for c in ([''] if a==0 and b==0 else (['A'] if b==0 else ['A', 'B']) ) ]
    sToI = {s: i for i,s in enumerate(states)}
    matN = len(states)
    qMat = matrix(QQ, matN)
    rArr = [0]*matN
    for i1, (a,b,prevWinner) in enumerate(states):
        #A wins the round
        score2 = a+(3 if prevWinner=='A' else 2)
        if score2>=n:
            rArr[i1] += p
        else:
            s2 = (score2, b, 'A')
            qMat.add_to_entry(i1, sToI[s2], p)
        #B wins the round
        score2 = b+(3 if prevWinner=='B' else 2)
        if score2>=n:
            pass #only recording the A-win column
        else:
            s2 = (a, score2, 'B')
            qMat.add_to_entry(i1, sToI[s2], 1-p)
    fundMat = (matrix.identity(matN) - qMat)^(-1)
    return (fundMat*vector(rArr))[sToI[(0,0,'')]]
    
ans = game2(p=6/10, n=10)
print ("%s = %f" %(ans, ans) )
Leave a Comment