猫史档案馆


【Python作品分享】井字格

用户:逃跑矜持音符zs逃跑矜持音符zs查看:0 回复:1 评论:0 创建时间:2020-05-21T20:41:55


【作品展示】

center_image

 

【作品介绍】

选择格子的数字好打下来

 

【作品源代码】

import time,random

#             ((())) 
#        o((((((())))))))o    
#         !!!!!!!!!!!!!!!       
#         (   o     o   )                                        
#       (        8       )
#  OOO    (  \_______/  )   OOO
#  OOO     (((((()))))))    OOO 
#   \ \   kkkkkkkkkkkkkkk   / /
#   \ \ kkkkkkkkkkkkkkkkk / /
#     \ kkkkkkkkkkkkkkkkkkk / 
#      kkkkkkkkkkkkkkkkkkkkk   
#      xxxxxxxxxxxxxxxxxxxxx   
#      xxxxxxxxxxxxxxxxxxxxx   
#       xxxxxxxxxxxxxxxxxxx  
#        11111      11111
#         111        111
#         111        111
#         111        111
#         111        111 
#         111        111 
#       (_____|     |____)


























a="""+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 4 | 5 | 6 |
+---+---+---+
| 7 | 8 | 9 |
+---+---+---+
"""
aa=a
def fun(line):
    for i in range(len(line)):
        print("\r"+line[0:i+1],end="")
        time.sleep(0.1)
    print()
print("\033[1;36m欢迎访问\033[1;33m饼干牌\033[1;36m井字棋游戏!💕")
fun("本游戏可转载收藏,借鉴修改")
fun("游戏三分钟,编程三小时")
fun("玩法如下:")
time.sleep(0.5)
print("\n\033[1;37m"+a)
fun("\033[1;33m游戏玩法:轮流落棋")
fun("达成三行、三列、斜对角")
fun("任意三子一线即为胜利!!!")
print("\033[1;36m",end="")
time.sleep(0.5)


def sun():
    global a,flag,all,aa,pas
    print("\033[1;31m")
    fun("胜负无常,落子无悔💕")
    print("\033[1;36m")
    fun("1:人机对弈模式💕")
    fun("2:双人对弈模式💕")
    type=input("\n\033[1;33mQ: 您选择的模式是:")
    while type!="1" and type!="2":
        print("\033[1A\033[K\033[1;31m输入有误,请重新输入")
        time.sleep(1)
        print("\033[1A\033[K",end="")
        type=input("\033[1;33mQ: 您选择的模式是:")
    print("\033[0m",end="")
    
    b=[
    [16,20,24], 
    [44,48,52],
    [72,76,80],
    [16,44,72],
    [20,48,76],
    [24,52,80],
    [16,48,80], 
    [72,48,24],]
    flag=True
    all=[]
    def bin():
        print("\033[1;33m",end="")
        global a,flag,all
        if flag:
            print("🎈  回合",end="---")
        else:
            print("🚀  回合",end="---")
        location=input("请输入落子位置:")
        print("\033[0m",end="")
        while location not in ["1","2","3","4","5","6","7","8","9"]:
            print("\033[1A\033[K\033[1;31m输入有误,请输入1~9")
            time.sleep(1)
            print("\033[1A\033[K\033[1;33m",end="")
            if flag:
                print("🎈  回合",end="---")
            else:
                print("🚀  回合",end="---")
            location=input("请输入落子位置:")
        if location in all:
            print("\033[1A\033[K\033[1;31m已有落子,请选择其他位置")
            time.sleep(1)
            print("\033[1A\033[K",end="")
            return 
        all.append(location)
        print(len(all))
        if flag:
            a1=a.replace(location,"🎈")
            a=a1
            flag=not flag
        else:
            a2=a.replace(location,"🚀")
            a=a2
            flag=not flag
        print("\033[0m",end="")
        print("\033[100A\033[2J"+a)
      
    def gan():
        global a,aa,all
        while True:
            bin()
            for i in b:
                if a[i[0]]==a[i[1]]==a[i[2]]:
                    print("\033[1;36m"+a[i[0]]+"  胜利,游戏结束")
                    goon=input("\n继续畅玩输入1,其余键退出")
                    if goon=="1":
                        a=aa
                        all=[]
                        print("\033[100A\033[2J"+a)
                        sun()
                    else:
                        print("\033[1;36m"+"小饼干祝你天天开心")
                    return
            if len(all)==9:
                name=input("和棋!重玩输入1,其余键退出:")
                if name =="1":
                    a=aa
                    all=[]
                    print("\033[100A\033[2J"+a)
                    gan()
                else:
                    print("\033[1;36m"+"小饼干祝你天天开心")
                return
    
    pas=["1","2","3","4","5","6","7","8","9"]
    def bin1():
        print("\033[1;33m",end="")
        global a,flag,all,pas
        if flag:
            print("🎈  回合",end="---")
            location=input("请输入落子位置:")
            if location in pas:
                pas.remove(location)
        else:
            print("🚀  回合",end="---")
            rand=random.choice(pas)
            location=str(rand)
            pas.remove(rand)
        print("\033[0m",end="")
        while location not in ["1","2","3","4","5","6","7","8","9"]:
            print("\033[1A\033[K\033[1;31m输入有误,请输入1~9")
            time.sleep(1)
            print("\033[1A\033[K\033[1;33m",end="")
            if flag:
                print("🎈  回合",end="---")
                location=input("请输入落子位置:")
                if location in pas:
                    pas.remove(location)
            else:
                print("🚀  回合",end="---")
                rand=random.choice(pas)
                location=str(rand)
                pas.remove(rand)
        if location in all:
            print("\033[1A\033[K\033[1;31m已有落子,请选择其他位置")
            time.sleep(1)
            print("\033[1A\033[K",end="")
            return 
        all.append(location)
        if flag:
            a1=a.replace(location,"🎈")
            a=a1
            flag=not flag
        else:
            time.sleep(0.5)
            a2=a.replace(location,"🚀")
            a=a2
            flag=not flag
        print(a)
        
        print("\n\033[0m\033[100A\033[2J"+a)
      
    def gan1():
        global a,aa,all,pas
        while True:
            bin1()
            for i in b:
                if a[i[0]]==a[i[1]]==a[i[2]]:
                    print("\033[1;36m"+a[i[0]]+"  胜利,游戏结束")
                    goon=input("\n继续畅玩输入1,其余键退出")
                    if goon=="1":
                        a=aa
                        pas=["1","2","3","4","5","6","7","8","9"]
                        all=[]
                        print("\033[100A\033[2J"+a)
                        sun()
                    else:
                        print("\033[1;36m"+"小饼干祝你天天开心")
                    return
            if len(all)==9:
                name=input("和棋!重玩输入1,其余键退出:")
                if name =="1":
                    a=aa
                    pas=["1","2","3","4","5","6","7","8","9"]
                    all=[]
                    print("\033[100A\033[2J"+a)
                    gan1()
                else:
                    print("\033[1;36m"+"小饼干祝你天天开心")
                return
    if type=="2":
        print("\033[100A\033[2J"+a)
        gan()
    if type=="1":
        print("\033[100A\033[2J"+a)
        gan1()
sun()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
三百克薄荷油三百克薄荷油

Imports

点赞1


评论