猫史档案馆


【Python作品分享】24点查询器

用户:.................20.................20查看:3 回复:11 评论:3 创建时间:2021-07-29T15:26:32


【作品展示】

center_image

 

【作品介绍】

因为玩24点游戏的时候想不到答案,所以就做了一个这样的查询器。。。

 

【作品源代码】

pa = ['+', '-', '*', '/']
pa_s = []
for i in range(len(pa)):
        a = pa[i]
        for j in range(len(pa)):
            b = pa[j]
            for k in range(len(pa)):
                c = pa[k]
                pa_s.append([a, b, c])


def plus(na, p, nb):
    if p=='+':
        return na+nb
    if p=='-':
        return na-nb
    if p=='*':
        return na*nb
    if p=='/':
        if nb == 0:
            return 0
        else:
            return na/nb


def cr(plus_l, num_l, ty):
    t = ty.split("x")
    an = t[0]
    for i in range(1, 4):
        an += str(num_l[i-1])
        an += t[i*2-1]
        an += plus_l[i-1]
        an += t[i*2]
    an += str(num_l[-1])
    an += t[-1]

    return an

def check(plus_l, num_l):
    A = plus(num_l[0], plus_l[0], num_l[1])
    A = plus(A, plus_l[1], num_l[2])
    A = plus(A, plus_l[2], num_l[3])
    if A == 24:
        if plus_l[2] == '*' or plus_l[2] == '/' and plus_l[1] == '+' or plus_l[1] == '-':
            return cr(plus_l, num_l, '(x x x x x)x x ')
        elif plus_l[1] == '*' or plus_l[1] == '/' and plus_l[0] == '+' or plus_l[0] == '-':
            return cr(plus_l, num_l, '(x x x)x x x x ')
        else:
            return cr(plus_l, num_l, ' x x x x x x x ')

    A = plus(num_l[1], plus_l[1], num_l[2])
    A = plus(num_l[0], plus_l[0], A)
    A = plus(A, plus_l[2], num_l[3])
    if A == 24:
        if plus_l[2] == '*' or plus_l[2] == '/' and plus_l[0] == '+' or plus_l[0] == '-':
            return cr(plus_l, num_l, '[x x(x x x)]x x ')
        else:
            return cr(plus_l, num_l, ' x x(x x x)x x ')

    # A = plus(num_l[2], plus_l[2], num_l[3])
    # B = plus(num_l[0], plus_l[0], num_l[1])
    # A = plus(B, plus_l[1], A)
    # if A == 24:
    #     if plus_l[2] == '*' or plus_l[2] == '/' and plus_l[1] == '+' or plus_l[1] == '-':
    #         return cr(plus_l, num_l, '(x x x x x)x x ')
    #     return cr(plus_l, num_l, 'x x x x(x x x)')

    A = plus(num_l[1], plus_l[1], num_l[2])
    A = plus(A, plus_l[2], num_l[3])
    A = plus(num_l[0], plus_l[0], A)
    if A == 24:
        if plus_l[2] == '*' or plus_l[2] == '/' and plus_l[1] == '+' or plus_l[1] == '-':
            return cr(plus_l, num_l, ' x x[(x x x)x x]')
        return cr(plus_l, num_l, 'x x(x x x x x)')

    A = plus(num_l[2], plus_l[2], num_l[3])
    A = plus(num_l[1], plus_l[1], A)
    A = plus(num_l[0], plus_l[0], A)
    if A == 24:
        return cr(plus_l, num_l, ' x x[x x(x x x)]')

    A = plus(num_l[0], plus_l[0], num_l[1])
    B = plus(num_l[2], plus_l[2], num_l[3])
    A = plus(A, plus_l[1], B)
    if A == 24:
        return cr(plus_l, num_l, '(x x x)x(x x x)')

    return None

def twenty_four(putin):
    global pa_s
    putin = int(putin)
    w = putin // 1000
    x = putin // 100 % 10
    y = putin // 10 % 10
    z = putin % 10
    if w == 0:
        w = 10
    if x == 0:
        x = 10
    if y == 0:
        y = 10
    if z == 0:
        z = 10
    nums=[]
    num=[w, x, y, z]
    for i in range(len(num)):
        a = num.pop(i)
        for j in range(len(num)):
            b = num.pop(j)
            for k in range(len(num)):
                c = num.pop(k)
                for l in range(len(num)):
                    d = num.pop(l)
                    nums.append([a, b, c, d])
                num = [w, x, y, z]
                num.pop(i)
                num.pop(j)
            num = [w, x, y, z]
            num.pop(i)
        num = [w, x, y, z]
    for k in pa_s:
        for i in nums:
            A = check(k, i)
            if A:
                return A
    return False

print("输入四个1-10的数字,如:1234(10写做0)")
for i in range(10):
    x = input("")
    print(twenty_four(x))
# nums = []
# num = [喵]
# for i in range(len(num)):
#     num = [喵]
#     a = num.pop(i)
#     num = [喵]
#     for j in range(len(num)):
#         num = [喵]
#         b = num.pop(j)
#         num = [喵]
#         for k in range(len(num)):
#             num = [喵]
#             c = num.pop(k)
#             num = [喵]
#             for l in range(len(num)):
#                 num = [喵]
#                 d = num.pop(l)
#                 z = twenty_four(a, b, c, d)
#                 if z == False:
#                     pass
#                 else:
#                     nums.append(z)
#                 num = [喵]
#             num = [喵]
#         num = [喵]
#     num = [喵]
# print(len(nums))
# print(nums)
# with open("24点.txt", 'w')as f:
#     f.write(str(nums))

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
.................20.................20

有时候会出现5-7,2-9等小数减大数的情况,把它看成7-5,9-2就好了

点赞0


评论


quuuquuu

emotion_编程猫_点赞

点赞0


评论


伴雪纷飞伴雪纷飞

有点喵破解

点赞0


评论


该账号已被封禁该账号已被封禁

eval:???????

点赞1


评论


Meow_sauceMeow_sauce

可以啊

 

点赞0


评论


天马神坑_真天马神坑_真

用python做的话可能还有更简单的方法?

点赞0


评论


hate_and_painhate_and_pain

挺有用,我妈出了11道她说无解的二十四点,结果,有解

点赞0


评论


一个STUB用户_6923702一个STUB用户_6923702

真的肝(指程序文件

点赞0


评论


离开的十五分之六老咸鱼离开的十五分之六老咸鱼

强 

点赞0


评论


KIHIOKIHIO

五子棋程序:

def jd(x):
    try:
        x = float(x)
    except ValueError:
        return 0
    if x-(float(int(x))) > 0:
        return 0
    x = int(x)
    if x < 1 or x > 10:
        return 0
    return x


def sy(a, b, c, a2, b2, c2, s):
    x = 0
    while x <= a2:
        y = 0
        while y <= b2:
            n = 0
            lxh = 0
            lxb = 0
            while n <= c2:
                if s[x+y+n] == '黑':
                    lxh += 1
                else:
                    lxh = 0
                if s[x+y+n] == '白':
                    lxb += 1
                else:
                    lxb = 0
                n += c
            if lxb == 5:
                return 0
            if lxh == 5:
                return 2
            y += b
        x += a
    return 1


def xsy(xx, xs, yx, ys, js, js2, s):
    x = xx
    while x <= xs:
        y = yx
        while y <= yx:
            lxb = 0
            lxh = 0
            n = 0
            while n <= 4:
                if s[(y+n*js2)*10+x+n*js] == '黑':
                    lxh += 1
                else:
                    lxh = 0
                if s[(y+n*js2)*10+x+n*js] == '白':
                    lxb += 1
                else:
                    lxb = 0
                n += 1
            if lxb == 5:
                return 0
            if lxh == 5:
                return 2
            y += 1
        x += 1
    return 1


print("棋盘为10*10。")
s = []
a = 0
while a < 100:
    s.append('无')
    a += 1
print("你告诉我黑棋还是白棋先下?(输入A代表黑,输入B代表白):")
while True:
    hb = input()
    if hb != 'A' and hb != 'B':
        print("输入错误,请重试:")
    else:
        break
b = 0
while True:
    f = 0
    fh = 0
    hqs = 0
    bqs = 0
    if sy(10, 1, 1, 90, 5, 4, s) == 2:
        hqs = 1
    if sy(1, 10, 10, 9, 50, 40, s) == 2:
        hqs = 1
    if sy(10, 1, 1, 90, 5, 4, s) == 0:
        bqs = 1
    if sy(1, 10, 10, 9, 50, 40, s) == 0:
        bqs = 1
    if xsy(0, 5, 0, 5, 1, 1, s) == 2:
        hqs = 1
    if xsy(4, 9, 4, 9, -1, -1, s) == 2:
        hqs = 1
    if xsy(0, 5, 0, 5, 1, 1, s) == 0:
        bqs = 1
    if xsy(4, 9, 4, 9, -1, -1, s) == 0:
        bqs = 1
    if xsy(0, 5, 4, 9, 1, -1, s) == 2:
        hqs = 1
    if xsy(0, 5, 4, 9, 1, -1, s) == 0:
        bqs = 1
    if xsy(4, 9, 0, 5, -1, 1, s) == 2:
        hqs = 1
    if xsy(4, 9, 0, 5, -1, 1, s) == 0:
        bqs = 1
    while f < 100:
        k = 0
        bs = ' '
        while k < 10:
            if k == 0:
                bs = s[f+k]
            else:
                bs = bs+' '+s[f+k]
            k += 1
        print(bs)
        f += 10
    if hqs == 1:
        print("黑棋赢!")
        break
    if bqs == 1:
        print("白棋赢!")
        break
    if hb == 'A':
        if b % 2 == 0:
            jhb = 'A'
        else:
            jhb = 'B'
    else:
        if b % 2 == 0:
            jhb = 'B'
        else:
            jhb = 'A'
    if jhb == 'A':
        print("黑棋的x坐标(从左到右,1~10):")
        while True:
            x = input()
            if jd(x) == 0:
                print("输入错误,请重试:")
            else:
                x = jd(x)
                break
        print("黑棋的y坐标(从上到下,1~10):")
        while True:
            y = input()
            if jd(y) == 0:
                print("输入错误,请重试:")
            else:
                y = jd(y)
                break
        if (s[(y-1)*10+x-1] == '黑') or (s[(y-1)*10+x-1] == '白'):
            print("禁止重叠,下次做决策看棋盘现状。")
            fh = 1
        else:
            s[(y-1)*10+x-1] = '黑'
    else:
        print("白棋的x坐标(从左到右,1~10):")
        while True:
            x = input()
            if jd(x) == 0:
                print("输入错误,请重试:")
            else:
                x = jd(x)
                break
        print("白棋的y坐标(从上到下,1~10):")
        while True:
            y = input()
            if jd(y) == 0:
                print("输入错误,请重试:")
            else:
                y = jd(y)
                break
        if (s[(y-1)*10+x-1] == '黑') or (s[(y-1)*10+x-1] == '白'):
            print("禁止重叠,下次做决策看棋盘现状。")
            fh = 1
        else:
            s[(y-1)*10+x-1] = '白'
    if fh == 0:
        b += 1

点赞0


评论


AlcalaAlcala

11、12、13没有

点赞0


评论