Programa en Python utilizado para explorar el truco de los ases y los dados

Como tenía curiosidad, escribí un programa en Python para enumerar todos los lanzamientos buenos y malos de cuatro dados para el truco. Ases y dados.

Here is the program:

import sys
from itertools import permutations
from itertools import combinations_with_replacement

good = set()
bad =  set()
uniquerolls = set()


def looptest(o, i, j, k , l):
    # firstdice are the only possible solutions for the first two dice
    firstdice = ["43", "34", "52", "25", "61", "16", "51", "15", "62", "26", "53", "35", "44"]
    flag = 0
    # see if the first two die add to 5, 6, 7 or 8
    # if they do, test the other two dice
    case = o[i] + o[j]

    if case in firstdice:
        flag = check(o, int(o[k]), int(o[l]), int(o[i]) + int(o[j]))
    return(flag)



def check(o, i,j,pair):
    # these are the rules for determining if you can deal to the aces depending on the sum of the first two dice
    if pair == 6:
        # if the first two dice add to 6, then one of the other two must be six to work
        if i == 6 or j == 6:
            return(1)
    elif pair == 7:
        # if the first two dice add to 7 then then one of the other two must be a 5 or a 6 to work
        if i == 5 or i == 6 or j == 5 or j == 6:
            return (1)
    elif pair == 8:
        if (i + j == 6) or (i + j) == 7 or (i + j == 8):
            return (1)

    return(0)

def main():

# uniquerolls equals all possible combinations with replacemenst of four die (126)
    uniquerolls = list(p for p in  combinations_with_replacement("123456",4))

# there are permutations of the dice to test
    for i in uniquerolls:
        roll = [i[0],i[1],i[2],i[3]]  # convert the string into a list of charactes
        perms = (p for p in permutations("0123")) # set up all permutations of the four dice so we can look at each pair
        for j in perms:
            # check the permutations of this roll looking for a solution
            flag = looptest(roll, int(j[0]), int(j[1]), int(j[2]), int(j[3]))
            if (flag == 1):
                roll.sort() # sort the roll and add it to the good set.  Sorting makes sure we only add one roll
                good.add(str(roll[0]) + str(roll[1]) + str(roll[2]) + str(roll[3]))
                break  # found a good roll so don't check any more permutations
        if (flag == 0):
            roll.sort()
            bad.add(str(roll[0]) + str(roll[1]) + str(roll[2]) + str(roll[3]))


    print("bad", len(bad))
    print("good", len(good))
    print("unique rolls", len(list(uniquerolls)))
    print(sorted(good))
    print(sorted(bad))

    over, under = 0,0
    for i in bad:
        if int(i[0])+int(i[1])+int(i[2])+int(i[3]) > 13:
            over = over + 1
        else:
            under = under + 1

    print("over",over,"under",under)
main()

The output of the program tells us several things.  There are 126 possible rolls, of which 80 are bad and 46 are good. Looking at only the bad rolls, 54 of them are rolls where the total of the four dice do not add up to at least 14.  26 of the bad rolls do add up to over 13, but still do not provide a way of dealing to the aces.

Good rolls:

['1156', '1166', '1255', '1256', '1266', '1345', '1346', '1355', '1356', '1366', '1445', '1446', '1456', '1466', '1556', '1566', '1666', '2246', '2255', '2256', '2266', '2336', '2345', '2346', '2355', '2356', '2444', '2445', '2446', '2455', '2456', '2555', '2556', '2566', '3335', '3344', '3345', '3346', '3355', '3444', '3445', '3446', '3455', '3456', '3466', '4444']

Bad rolls:

['1111', '1112', '1113', '1114', '1115', '1116', '1122', '1123', '1124', '1125', '1126', '1133', '1134', '1135', '1136', '1144', '1145', '1146', '1155', '1222', '1223', '1224', '1225', '1226', '1233', '1234', '1235', '1236', '1244', '1245', '1246', '1333', '1334', '1335', '1336', '1344', '1444', '1455', '1555', '2222', '2223', '2224', '2225', '2226', '2233', '2234', '2235', '2236', '2244', '2245', '2333', '2334', '2335', '2344', '2366', '2466', '2666', '3333', '3334', '3336', '3356', '3366', '3555', '3556', '3566', '3666', '4445', '4446', '4455', '4456', '4466', '4555', '4556', '4566', '4666', '5555', '5556', '5566', '5666', '6666']

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *