repl.it
@oliverosz/

KaijiGame2Strategies

Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from math import pow

pk = 0.8  # probability of King player winning
ps = 0.2  # probability of Slave player winning

wk = 100  # win multiplier of King player
ws = 500  # win multiplier of Slave player

start = 29  # starting assets
rounds = 12

roles = ['King', 'Slave']
p = [pk, ps]
w = [wk, ws]


def estimatedWinnings(startingRoleIdx: int, betMaxRoleIdx: int):
    won = 0.0
    for i in range(0, rounds):
        role = (startingRoleIdx + i) % 2
        left = rounds - i - 1  # remaining rounds
        prevBigBets = i / 2  # number of big bets made before
        prevSmallBets = i - prevBigBets
        if role == betMaxRoleIdx or i == rounds - 1:
            # bet as high as possible: current assets - remaining rounds
            # case 1: we won all previous big bets
            pcase1 = pow(p[betMaxRoleIdx], prevBigBets)
            # each small and big bet we had won increases the current bid
            bigBet = start - left + prevSmallBets * p[
                1 - betMaxRoleIdx] + prevBigBets
            won += pcase1 * bigBet * p[role] * w[role]
            # case 2: we had lost a big bet so bet minimum
            won += (1 - pcase1) * p[role] * w[role]
        else:
            won += p[role] * w[role]
    return won


for maxRole in [0, 1]:
    print('Strategy is to maximize bet as ' + roles[maxRole])
    for startRole in [0, 1]:
        value = estimatedWinnings(startRole, maxRole)
        print('When starting role is {}, estimated winnings: {}'.format(
            roles[startRole], value))