# d6 Probability Calculator for 2/12 to win

import math

print('d6 calculator for 2/12 to win.')
prop = int(input('Proportionality constant: '))
##  prop = 1

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  ## a is the winning number(s) that we are calculating.
print (amax)
bmax = (4*prop)-1
print(bmax)
cmax = (6*prop)-1
print(cmax)
dmax = (8*prop)-1
print(dmax)
emax = (10*prop)-1
print(emax)
fmax = (6*prop)-1
print(fmax)

totalcases = bmax+cmax+dmax+emax+fmax+1


# Probability of each roll

aprob = (2/36)
bprob = (4/36)
cprob = (6/36)
dprob = (8/36)
eprob = (10/36)
fprob = (6/36)

for i in range(0, (totalcases)):

# for i in range(0, 32):

    print('     Starting case i=', i)

    aend = 0
    bend = 0
    cend = 0
    dend = 0
    eend = 0
    fend = 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
    e = 0
    f = 0

    #### Set Initial State

    print('** Begin finding initial state! **')

    temp = i
    initialize = 0

    while(initialize == 0):
        if (temp > fmax):
            f = fmax
            # print('f overflow.  f set to fmax.')
            temp = temp - fmax
        else:
            f = temp    # Put everything in f
            temp = 0    # Empty temp
            # print('Everything assigned to f.  Loop breaks.')
            initialize = 1

        if (temp > emax):
            e = emax
            temp = temp - emax
            # print('e overflow.  e set to emax.')
        else:
            e = temp    # Put everything in e
            temp = 0    # Empty temp
            # print('Everything assigned to e.  Loop breaks.')
            initialize = 1

        if (temp > dmax):
            d = dmax
            temp = temp - dmax
            # print('d overflow.  d set to 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    # Put everything in c
            temp = 0    # Empty temp
            # print('Everything assigned to c.  Loop breaks.')
            initialize = 1


    print('Initial state is ', b, c, d, e, f)

    print('Solving initial state now.')
    statecoeff = (math.factorial(a+b+c+d+e+f-1)/(math.factorial(a-1)*math.factorial(b)*math.factorial(c)*math.factorial(d)
                                                 *math.factorial(e)*math.factorial(f)))
    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)*(eprob**e)*(fprob**f)
    print('Initial state of ', b, c, d, e, f, ' 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
            # print('c overflow.  c ending set to cmax.')
            temp = temp - cmax
        else:
            cend = temp  # Put everything in c
            temp = 0  # Empty temp
            # print('Everything assigned to c.  Loop breaks.')
            endstate = 1

        if (temp > dmax):
            dend = dmax
            # print('d overflow.  d ending set to dmax.')
            temp = temp - dmax
        else:
            dend = temp  # Put everything in d
            temp = 0  # Empty temp
            # print('Everything assigned to d.  Loop breaks.')
            endstate = 1

        if (temp > emax):
            eend = emax
            temp = temp - emax
            # print('e overflow.  e ending set to emax.')
            fend = temp
            # print('Balance assigned to f')
            endstate = 1
        else:
            eend = temp
            # print('Balance assigned to e.  Loop breaks.')
            endstate = 1

    print('End condition state is ', bend, cend, dend, eend, fend)

    #### Begin to iterate

    complete = 0

    print('** Begin iterating! **')

    while (b != bend) or (c != cend) or (d != dend) or (e != eend) or (f != fend):

        if (c == cmax) and (d == dmax) and (e == emax) and (f == fmax):
            b = b + 1
            c = c - 1
        elif (d == dmax) and (e == emax) and (f == fmax):
            c = c + 1
            d = d - 1
        elif (e == emax) and (f == fmax):
            d = d + 1
            e = e - 1

        elif ((d == 0) and (e == 0) and (f == 0)):
            b = b + 1
            c = 0
            if (i - b - c - d - e > fmax):  # f overflow
                f = fmax
                e = i - b - c - d - fmax
                if (e > emax):
                    e = emax
                    d = i - b - c - emax - fmax
                    if (d > dmax):
                        d = dmax
                        c = i - b - dmax - emax - fmax
                        if (c > cmax):
                            c = cmax
                            b = i - cmax - dmax - emax - fmax
            else:
                f = i - b - c - d - e
        elif (e == 0) and (f == 0):
            c = c + 1
            d = 0
            if (i - b - c - d - e > fmax):  # f overflow
                f = fmax
                e = i - b - c - d - fmax
                if (e > emax):
                    e = emax
                    d = i - b - c - emax - fmax
                    if (d > dmax):
                        d = dmax
                        c = i - b - dmax - emax - fmax
                        if (c > cmax):
                            c = cmax
                            b = i - cmax - dmax - emax - fmax
            else:
                f = i - b - c - d - e
        elif (f == 0):
            d = d + 1
            e = 0
            if (i - b - c - d - e > fmax):  # f overflow
                f = fmax
                e = i - b - c - d - fmax
                if (e > emax):
                    e = emax
                    d = i - b - c - emax - fmax
                    if (d > dmax):
                        d = dmax
                        c = i - b - dmax - emax - fmax
                        if (c > cmax):
                            c = cmax
                            b = i - cmax - dmax - emax - fmax
            else:
                f = i - b - c - d - e
        else:
            f = f - 1
            e = e + 1

        if (e > emax):
            d = d + 1
            e = 0
            if (i - b - c - d - e > fmax):  # f overflow
                f = fmax
                e = i - b - c - d - fmax
                if (e > emax):
                    e = emax
                    d = i - b - c - emax - fmax
                    if (d > dmax):
                        d = dmax
                        c = i - b - dmax - emax - fmax
                        if (c > cmax):
                            c = cmax
                            b = i - cmax - dmax - emax - fmax
            else:
                f = i - b - c - d - e
        if (d > dmax):
            c = c + 1
            d = 0
            if (i - b - c - d - e > fmax):  # f overflow
                f = fmax
                e = i - b - c - d - fmax
                if (e > emax):
                    e = emax
                    d = i - b - c - emax - fmax
                    if (d > dmax):
                        d = dmax
                        c = i - b - dmax - emax - fmax
                        if (c > cmax):
                            c = cmax
                            b = i - cmax - dmax - emax - fmax
            else:
                f = i - b - c - d - e

        if (c > cmax):
            b = b + 1
            c = 0
            if (i - b - c - d - e > fmax):  # f overflow
                f = fmax
                e = i - b - c - d - fmax
                if (e > emax):
                    e = emax
                    d = i - b - c - emax - fmax
                    if (d > dmax):
                        d = dmax
                        c = i - b - dmax - emax - fmax
                        if (c > cmax):
                            c = cmax
                            b = i - cmax - dmax - emax - fmax
            else:
                f = i - b - c - d - e







        # print('Calculating values for state ', b, c, d)
        statecoeff = (math.factorial(a + b + c + d + e + f - 1) / (
                math.factorial(a - 1) * math.factorial(b) * math.factorial(c)
                * math.factorial(d)* math.factorial(e)* math.factorial(f)))
        # 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) * (eprob ** e)* (fprob ** f)
        print('State probability for', b, c, d, e, f, ' is', stateprob)
        totalprob = totalprob + stateprob
        print('Cumulative probability is', totalprob * 100, '%')

        if (b == bend) and (c == cend) and (d == dend) and (e == eend) and (f == fend):
            complete = 1


