# d4 Probability Calculator for 3/7 or 5 to win

import math

print('d4 calculator for 2/8 to win.')
prop = int(input('Proportionality constant: '))

totalprob = 0.0
total_games = 0

# What's the most number of possible rolls before the final winning roll?
# Winning roll happens full set of times; losing number happen at most (max - 1) times.

amax = (2*prop)-1
print (amax)
bmax = (4*prop)-1
print(bmax)
cmax = (6*prop)-1
print(cmax)
dmax = (4*prop)-1
print(dmax)
print(bmax+cmax+dmax+1)


# Probability of each roll

aprob = (2/16)
bprob = (4/16)
cprob = (6/16)
dprob = (4/16)

for i in range(0, (bmax+cmax+dmax+1)):

    print('     Starting case i=', i)

    aend = 0
    bend = 0
    cend = 0
    dend = 0

# How many rolls for this state?  (a, b, c, d) is the case being considered.

    a = 2*prop
    b = 0
    c = 0
    d = 0

    #### Set Initial State

    print('** Begin finding initial state! **')

    temp = i
    initialize = 0

    while(initialize == 0):
        if (temp > dmax):
            d = dmax
            # print('d overflow.  d set to dmax.')
            temp = temp - dmax
        else:
            d = temp    # Put everything in d
            temp = 0    # Empty temp
            # print('Everything assigned to d.  Loop breaks.')
            initialize = 1

        if (temp > cmax):
            c = cmax
            temp = temp - cmax
            # print('c overflow.  c set to cmax.')
            b = temp
            # print('Balance assigned to b')
            initialize = 1
        else:
            c = temp
            # print('Balance assigned to c.  Loop breaks.')
            initialize = 1

    # print('Initial state is ', b, c, d)

    # print('Solving initial state now.')
    statecoeff = (math.factorial(a+b+c+d-1)/(math.factorial(a-1)*math.factorial(b)*math.factorial(c)*math.factorial(d)))
    # print('State coeff is', statecoeff)
    total_games = total_games + statecoeff
    print('Total games so far:', total_games)
    stateprob = statecoeff*(aprob**a)*(bprob**b)*(cprob**c)*(dprob**d)
    print('Initial state of ', b, c, d, ' has probability of', stateprob)
    totalprob = totalprob + stateprob
    print('Cumulative probability is', totalprob*100, '%')


    #### Find end condition for value of i.
    #### When will we need to iterate i?

    print('** Begin finding end condition! **')

    temp = i
    endstate = 0

    while(endstate == 0):
        if (temp > bmax):
            bend = bmax
            # print('b overflow.  b ending set to bmax.')
            temp = temp - bmax
        else:
            bend = temp    # Put everything in b
            temp = 0    # Empty temp
            # print('Everything assigned to b.  Loop breaks.')
            endstate = 1

        if (temp > cmax):
            cend = cmax
            temp = temp - cmax
            # print('c overflow.  c ending set to cmax.')
            dend = temp
            # print('Balance assigned to d')
            endstate = 1
        else:
            cend = temp
            # print('Balance assigned to c.  Loop breaks.')
            endstate = 1

    print('End condition state is ', bend, cend, dend)

    #### Begin to iterate

    complete = 0

    print('** Begin iterating! **')

    while (b != bend) or (c != cend) or (d != dend):

        d = d - 1
        c = c + 1

        if (c > cmax) or (d < 0):
            b = b + 1
            print('b iteration condition met.')
            if (i - b > dmax):
                # print('d overflow on b iteration.  d set to max, balance to c.')
                d = dmax
                c = i - b - d
            else:
                d = i - b
                c = 0
                # print('Everything goes back into d. c set to zero.')

        # print('Calculating values for state ', b, c, d)
        statecoeff = (math.factorial(a + b + c + d - 1) / (
                    math.factorial(a - 1) * math.factorial(b) * math.factorial(c) * math.factorial(d)))
        # print('State coeff is', statecoeff)
        total_games = total_games + statecoeff
        print('Total games so far:', total_games)
        stateprob = statecoeff * (aprob ** a) * (bprob ** b) * (cprob ** c) * (dprob ** d)
        print('State probability for', b, c, d, ' is', stateprob)
        totalprob = totalprob + stateprob
        print('Cumulative probability is', totalprob*100, '%')

        if (b == bend) and (c == cend) and (d == dend):
            complete = 1


