猫史档案馆


【Python作品分享】2048【作品秀】

用户:KIHIOKIHIO查看:3 回复:3 评论:3 创建时间:2021-08-03T11:56:32


【作品展示】

center_image

 

【作品介绍】

文字版2048

 

【作品源代码】

import random

h=[]
i=0
while i<16:
    h.append(0)
    i+=1
l=random.randint(0,15)
l2=random.randint(0,15)
while l2==l:
    l2=random.randint(0,15)
h[l]=2
h[l2]=2
win=0
while True:
    i=0
    pzf=' '
    while i<16:
        if (i+1)%4==1:
            pzf=str(h[i])
        else:
            pzf=pzf+str(h[i])
        if (i+1)%4==0:
            print(pzf)
        else:
            j=0
            while j<5-(len(str(h[i]))):
                pzf=pzf+' '
                j+=1
        i+=1
    i=0
    while i<16:
        if h[i]==2048:
            print("你组合出了2048!!!你赢了!!!")
            win=1
            break
        i+=1
    if win==1:
        break
    i=0
    xh=1
    while i<16:
        if h[i]==0:
            xh=0
            break
        i+=1
    if xh==1:
        js=0
        i=0
        while i<16:
            if i%4!=3:
                if h[i]==h[i+1]:
                    js+=1
            i+=1
        i=0
        while i<4:
            j=0
            while j<=8:
                if h[i+j]==h[i+j+4]:
                    js+=1
                j+=4
            i=i+1
        if js==0:
            print("没法拼接了,你输了……")
            break
    try:
        x=float(input("你要做什么决策?(1上2下3左4右):"))
    except ValueError:
        print("ERROR:你的输入为字符串")
        break
    if x-(float(int(x)))>0:
        print("ERROR:你的输入为小数")
        break
    x=int(x)
    if x==1:
        i=4
        while i<16:
            f=i
            if h[i]!=0:
                while f>=0:
                    if h[f]!=0 and f!=i:
                        if h[f]==h[i]:
                            h[f]+=h[i]
                            h[i]=0
                        else:
                            if f!=i-4:
                                h[f+4]=h[i]
                                h[i]=0
                        break
                    f-=4
                if h[f+4]==0 and f<0:
                    h[f+4]=h[i]
                    h[i]=0
            i+=1
        i=0
        g=0
        while i<16:
            if h[i]==0:
                g=1
                break
            i+=1
        if g==1:
            l=random.randint(0,15)
            while h[l]!=0:
                l=random.randint(0,15)
            js=random.randint(1,2)
            h[l]=js*2
    elif x==2:
        i=11
        while i>=0:
            f=i
            if h[i]!=0:
                while f<=15:
                    if h[f]!=0 and f!=i:
                        if h[f]==h[i]:
                            h[f]+=h[i]
                            h[i]=0
                        else:
                            if f!=i+4:
                                h[f-4]=h[i]
                                h[i]=0
                        break
                    f+=4
                if h[f-4]==0 and f>15:
                    h[f-4]=h[i]
                    h[i]=0
            i-=1
        i=0
        g=0
        while i<16:
            if h[i]==0:
                g=1
                break
            i+=1
        if g==1:
            l=random.randint(0,15)
            while h[l]!=0:
                l=random.randint(0,15)
            js=random.randint(1,2)
            h[l]=js*2
    elif x==4:
        i=15
        while i>=0:
            if i%4!=3:
                f=i
                if h[i]!=0:
                    while f<=((int(i/4))*4+3):
                        if h[f]!=0 and f!=i:
                            if h[f]==h[i]:
                                h[f]+=h[i]
                                h[i]=0
                            else:
                                if f!=i+1:
                                    h[f-1]=h[i]
                                    h[i]=0
                            break
                        f+=1
                    if h[f-1]==0 and f>((int(i/4))*4+3):
                        h[f-1]=h[i]
                        h[i]=0
            i-=1
        i=0
        g=0
        while i<16:
            if h[i]==0:
                g=1
                break
            i-=1
        if g==1:
            l=random.randint(0,15)
            while h[l]!=0:
                l=random.randint(0,15)
            js=random.randint(1,2)
            h[l]=js*2
    elif x==3:
        i=1
        while i<16:
            if i%4!=0:
                f=i
                if h[i]!=0:
                    while f>=((int(i/4))*4):
                        if h[f]!=0 and f!=i:
                            if h[f]==h[i]:
                                h[f]+=h[i]
                                h[i]=0
                            else:
                                if f!=i-1:
                                    h[f+1]=h[i]
                                    h[i]=0
                            break
                        f-=1
                    if h[f+1]==0 and f<((int(i/4))*4):
                        h[f+1]=h[i]
                        h[i]=0
            i+=1
        i=0
        g=0
        while i<16:
            if h[i]==0:
                g=1
                break
            i+=1
        if g==1:
            l=random.randint(0,15)
            while h[l]!=0:
                l=random.randint(0,15)
            js=random.randint(1,2)
            h[l]=js*2
    else:
        print("ERROR:你输入的整数不合理")
        break

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
倔强的象牙螺xEgj倔强的象牙螺xEgj

这个可以

 

点赞0


评论


Python-happyyyyPython-happyyyy

GUI不好吗,我做过一个2048,用的tkinter。。

点赞0


评论


KIHIOKIHIO

修复BUG:

import random

h=[]
i=0
while i<16:
    h.append(0)
    i+=1
l=random.randint(0,15)
l2=random.randint(0,15)
while l2==l:
    l2=random.randint(0,15)
h[l]=2
h[l2]=2
win=0
while True:
    i=0
    pzf=' '
    while i<16:
        if (i+1)%4==1:
            pzf=str(h[i])
        else:
            pzf=pzf+str(h[i])
        if (i+1)%4==0:
            print(pzf)
        else:
            j=0
            while j<5-(len(str(h[i]))):
                pzf=pzf+' '
                j+=1
        i+=1
    i=0
    while i<16:
        if h[i]==2048:
            print("你组合出了2048!!!你赢了!!!")
            win=1
            break
        i+=1
    if win==1:
        break
    i=0
    xh=1
    while i<16:
        if h[i]==0:
            xh=0
            break
        i+=1
    if xh==1:
        js=0
        i=0
        while i<16:
            if i%4!=3:
                if h[i]==h[i+1]:
                    js+=1
            i+=1
        i=0
        while i<4:
            j=0
            while j<=8:
                if h[i+j]==h[i+j+4]:
                    js+=1
                j+=4
            i=i+1
        if js==0:
            print("没法拼接了,你输了……")
            break
    try:
        x=float(input("你要做什么决策?(1上2下3左4右):"))
    except ValueError:
        print("ERROR:你的输入为字符串")
        break
    if x-(float(int(x)))>0:
        print("ERROR:你的输入为小数")
        break
    x=int(x)
    if x==1:
        i=4
        while i<16:
            f=i
            if h[i]!=0:
                while f>=0:
                    if h[f]!=0 and f!=i:
                        if h[f]==h[i]:
                            h[f]+=h[i]
                            h[i]=0
                        else:
                            if f!=i-4:
                                h[f+4]=h[i]
                                h[i]=0
                        break
                    f-=4
                if h[f+4]==0 and f<0:
                    h[f+4]=h[i]
                    h[i]=0
            i+=1
        i=0
        g=0
        while i<16:
            if h[i]==0:
                g=1
                break
            i+=1
        if g==1:
            l=random.randint(0,15)
            while h[l]!=0:
                l=random.randint(0,15)
            js=random.randint(1,2)
            h[l]=js*2
    elif x==2:
        i=11
        while i>=0:
            f=i
            if h[i]!=0:
                while f<=15:
                    if h[f]!=0 and f!=i:
                        if h[f]==h[i]:
                            h[f]+=h[i]
                            h[i]=0
                        else:
                            if f!=i+4:
                                h[f-4]=h[i]
                                h[i]=0
                        break
                    f+=4
                if h[f-4]==0 and f>15:
                    h[f-4]=h[i]
                    h[i]=0
            i-=1
        i=0
        g=0
        while i<16:
            if h[i]==0:
                g=1
                break
            i+=1
        if g==1:
            l=random.randint(0,15)
            while h[l]!=0:
                l=random.randint(0,15)
            js=random.randint(1,2)
            h[l]=js*2
    elif x==4:
        i=15
        while i>=0:
            if i%4!=3:
                f=i
                if h[i]!=0:
                    while f<=((int(i/4))*4+3):
                        if h[f]!=0 and f!=i:
                            if h[f]==h[i]:
                                h[f]+=h[i]
                                h[i]=0
                            else:
                                if f!=i+1:
                                    h[f-1]=h[i]
                                    h[i]=0
                            break
                        f+=1
                    if h[f-1]==0 and f>((int(i/4))*4+3):
                        h[f-1]=h[i]
                        h[i]=0
            i-=1
        i=0
        g=0
        while i<16:
            if h[i]==0:
                g=1
                break
            i+=1
        if g==1:
            l=random.randint(0,15)
            while h[l]!=0:
                l=random.randint(0,15)
            js=random.randint(1,2)
            h[l]=js*2
    elif x==3:
        i=1
        while i<16:
            if i%4!=0:
                f=i
                if h[i]!=0:
                    while f>=((int(i/4))*4):
                        if h[f]!=0 and f!=i:
                            if h[f]==h[i]:
                                h[f]+=h[i]
                                h[i]=0
                            else:
                                if f!=i-1:
                                    h[f+1]=h[i]
                                    h[i]=0
                            break
                        f-=1
                    if h[f+1]==0 and f<((int(i/4))*4):
                        h[f+1]=h[i]
                        h[i]=0
            i+=1
        i=0
        g=0
        while i<16:
            if h[i]==0:
                g=1
                break
            i+=1
        if g==1:
            l=random.randint(0,15)
            while h[l]!=0:
                l=random.randint(0,15)
            js=random.randint(1,2)
            h[l]=js*2
    else:
        print("ERROR:你输入的整数不合理")
        break

点赞0


评论