## d4 Proportional Board Dice Game

import math
import random



## define variables

outcomes = 4
roll = 0

twoEightWin = 0  ## Variables for counting game wins
threeSevenWin = 0
fourSixWin = 0
fiveWin = 0


trials = int(input("Number of Trials: "))         ## Number of Trials

scale = int(input("Proportionality scale: "))     ## Scale of the board proportionality

k = 0

for k in range(0, trials):

    twoEight = 0
    threeSeven = 0
    fourSix = 0
    five = 0

    end = 0

    while (end == 0):  ##  Optional output commands are marked as comment lines

        roll = (random.randint(1, 4) + random.randint(1, 4))
        ## print('Roll ', k, ' = ', roll)
        if (roll == 2) or (roll == 8):
            twoEight = twoEight + 1
        elif (roll == 3) or (roll == 7):
            threeSeven = threeSeven + 1
        elif (roll == 4) or (roll == 6):
            fourSix = fourSix + 1
        else:
            five = five + 1

        if (twoEight == 2*scale):
            twoEightWin = twoEightWin + 1
            ## print('Trial #', k, ', Two/Twelve wins.')
            end = 1
        elif (threeSeven == 4*scale):
            threeSevenWin = threeSevenWin + 1
            ## print('Trial #', k, ', Three/Eleven wins.')
            end = 1
        elif (fourSix == 6*scale):
            fourSixWin = fourSixWin + 1
            ## print('Trial #', k, ', Four/Ten wins.')
            end = 1
        elif (five == 4*scale):
            fiveWin = fiveWin + 1
            ## print('Trial #', k, ', Seven wins.')
            end = 1


print('Proportional Board, d4, scale of ',scale,' and ',trials,' trials:')
print(' # # # # # # # ')
print('Two/Eight won ', twoEightWin, 'times for ', (100 * twoEightWin) / trials, '%.')
print('Three/Seven won ', threeSevenWin, 'times for ', (100 * threeSevenWin) / trials, '%.')
print('Four/Six won ', fourSixWin, 'times for ', (100 * fourSixWin) / trials, '%.')
print('Five won ', fiveWin, 'times for ', (100 * fiveWin) / trials, '%.')


fairShare = trials/outcomes
print('Equal outcomes is', fairShare)
chiSquare_balanced = (((twoEightWin-fairShare)**2)/fairShare)+(((threeSevenWin-fairShare)**2)/fairShare)+(((fourSixWin-fairShare)**2)/fairShare)+(((fiveWin-fairShare)**2)/fairShare)
# chiSquare_theoretical = chisquare([(100 * twoEightWin) / trials, (100 * threeSevenWin) / trials, (100 * fourSixWin) / trials, (100 * fiveWin) / trials], f_exp=[35.2181, 23.2719, 18.2381, 23.2719])

variance = (((twoEightWin-fairShare)**2) + ((threeSevenWin-fairShare)**2) + ((fourSixWin-fairShare)**2) + ((fiveWin - fairShare) ** 2)) / (outcomes - 1)
print('Game variance is ', variance)
print('Game StDev is', math.sqrt(variance))
print('Balanced chi square value is', chiSquare_balanced)
# print('Theoretical chi square value is', chiSquare_theoretical)