[Python3] 三子棋游戏!祝大家中国 71 周年国庆节快乐!
发布于: 2020 年 10 月 03 日
祝大家71周年国庆节快乐!
为大家带来一款我用Python 3写的棋牌类游戏。
使用方法详见:
西瓜视频
http://www.ixigua.com/i6851926525639000589/
AcFun
http://www.acfun.cn/v/ac16941455
哔哩哔哩
http://www.bilibili.com/video/av711388784
from sys import exit,exc_info,excepthook,versionfrom time import localtime,sleepclass Bright: def PermList(L,/): if L.__class__ is not ().__class__ and L.__class__ is not [].__class__ and L.__class__ is not ''.__class__: raise TypeError(b'~{2NJ}1XPkJGT*Wi!"AP1m;rU_WV7{4.~}'.decode("hz","strict")) X,R=[],[] R+=[[a for a in L]] N=0 for _ in L: X+=[N] N+=1 Y=[N-a for a in X] Z=[a-1 for a in Y] while True: for I in Z: J=I+1 Y[I]-=1 if Y[I]==0: X[I:N]=X[J:N]+X[I:J] Y[I]=N-I else: K=N-Y[I] X[I],X[K]=X[K],X[I] R+=[[L[a] for a in X]] break else: return Rtry: from turtle import setup,title,screensize,isvisible,ht,speed,isdown,pu,pd,goto,setx,sety,write,circle,Terminatorexcept ModuleNotFoundError as E0: excepthook(E0.__class__,E0,exc_info()[2]) print(b'~{!>4mNs4zBk#:~}4~{!?DzTZJ9SC~}zip~{0f5D~}Python 3~{Bp#?~}'.decode("hz","strict")) exit(4)except: EI=exc_info() excepthook(EI[0],EI[1],EI[2]) print(b"~{!>4mNs4zBk#:~}5~{!?~}'turtle'~{D#?i3vAKR;P)NJLb!#=(RiDzVXPB02W0;rU_P^84~}Python 3~{!#~}".decode("hz","strict")) exit(5)def Initialize(): global Grid,Hint,GridStr,Player1Grid,Player2Grid,Log,Horizontal1,Horizontal2,Horizontal3,Vertical1,Vertical2,Vertical3,Diagonal1,Diagonal2,Win Grid,Hint,T=(1,2,3,4,5,6,7,8,9),("-"*8,b'~{TZSNO79}3LVP#,~}'.decode("hz","strict"),b'~{<|Hk!0~}credits~{!12i?41>3LPr5DPEO"#,~}'.decode("hz","strict"),b'~{<|Hk!0~}rules~{!12i?4Hg:N=xPP1>SNO7!#~}'.decode("hz","strict"),"-"*8),(1,2,3) Title=b'~{H}WSFe#(K+HK6TU=#)~}'.decode("hz","strict") print("Python "+version+"\n\n"+Title+"\n\n"+b'~{W<18FeEL!-!-~}'.decode("hz","strict"),end='') setup(600,600,-1,-1) title(Title) screensize(300,300) if isvisible(): ht() speed(0) print(b'~{Mx8q!-!-~}'.decode("hz","strict"),end='') for I in (100,200): if isdown(): pu() goto(I,0) pd() sety(300) pu() goto(0,I) pd() setx(300) pu() print(b'~{;9SPJ}WV!-!-~}'.decode("hz","strict"),end='') for I in T: for J in T: goto(I*100-50,340-J*100) write((I+J*3-3).__str__(),False,"center",("Arial",20,"normal")) print(b'~{Mj3I#!~}'.decode("hz","strict")+"\n"+b'~{W<18J}>]!-!-~}'.decode("hz","strict"),end='') GridStr,Player1Grid,Player2Grid,Log=[a.__str__() for a in Grid],[],[],[">"*8] Horizontal1,Horizontal2,Horizontal3,Vertical1,Vertical2,Vertical3,Diagonal1,Diagonal2=Bright.PermList(Grid[0:3]),Bright.PermList(Grid[3:6]),Bright.PermList(Grid[6:9]),Bright.PermList(Grid[0:7:3]),Bright.PermList(Grid[1:8:3]),Bright.PermList(Grid[2:9:3]),Bright.PermList(Grid[0:9:4]),Bright.PermList(Grid[2:7:2]) Win=Horizontal1+Horizontal2+Horizontal3+Vertical1+Vertical2+Vertical3+Diagonal1+Diagonal2 print(b'~{Mj3I#!~}'.decode("hz","strict")+"\n"+b'~{3uJ<;/Mj3I#!~}'.decode("hz","strict")+"\n") return Noneclass Draw: def Circle(P,/): if P.__class__ is not (0).__class__: raise TypeError if isdown(): pu() speed(0) if P==Grid[0]: goto(50,210) elif P==Grid[1]: goto(150,210) elif P==Grid[2]: goto(250,210) elif P==Grid[3]: goto(50,110) elif P==Grid[4]: goto(150,110) elif P==Grid[5]: goto(250,110) elif P==Grid[6]: goto(50,10) elif P==Grid[7]: goto(150,10) elif P==Grid[8]: goto(250,10) else: raise OverflowError pd() speed(3) circle(40,360) speed(0) pu() return None def Cross(P,/): if P.__class__ is not (0).__class__: raise TypeError if isdown(): pu() speed(0) if P==Grid[0]: X,Y=90,290 elif P==Grid[1]: X,Y=190,290 elif P==Grid[2]: X,Y=290,290 elif P==Grid[3]: X,Y=90,190 elif P==Grid[4]: X,Y=190,190 elif P==Grid[5]: X,Y=290,190 elif P==Grid[6]: X,Y=90,90 elif P==Grid[7]: X,Y=190,90 elif P==Grid[8]: X,Y=290,90 else: raise OverflowError goto(X,Y) pd() speed(3) goto(X-80,Y-80) pu() sety(Y) pd() goto(X,Y-80) speed(0) pu() return None def WinLine(L,/): if L.__class__ is not [].__class__: raise TypeError if isdown(): pu() speed(0) def SlowDraw(X,Y,/): goto(X,Y) pd() speed(3) return None if L in Horizontal1: SlowDraw(10,250) setx(290) elif L in Horizontal2: SlowDraw(10,150) setx(290) elif L in Horizontal3: SlowDraw(10,50) setx(290) elif L in Vertical1: SlowDraw(50,290) sety(10) elif L in Vertical2: SlowDraw(150,290) sety(10) elif L in Vertical3: SlowDraw(250,290) sety(10) elif L in Diagonal1: SlowDraw(10,290) goto(290,10) elif L in Diagonal2: SlowDraw(290,290) goto(10,10) else: raise IndexError speed(0) pu() return Noneclass Query: Information,HowToPlay,Key=("="*8,b'~{1`3L#:~}Bright_Leader'.decode("hz","strict"),b'~{2bJT;7>3#:~}Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)]'.decode("hz","strict"),"="*8),("+"*8,b'~{9fTr#:8CH}WSFeSNO7SIK+HKSNMf!#~}'.decode("hz","strict"),b'~{TZ~}3~{!A~}3~{Mx8qVP#,Mf<RR;SkMf<R6~BVAwTZ?U0W8qWS@o;-8wWT5DM<08!#~}'.decode("hz","strict"),b'~{5ZR;8vTZ:a!"W];rP17=OrIO;-3vA,Px~}3~{8vM<085DMf<R=+S.5CSNO7J$@{!#~}'.decode("hz","strict"),b'~{5+Hg9{WnVUC;SPMf<RWv5=;-3vA,Px~}3~{8vM<08#,SNO7=+F=>V!#~}'.decode("hz","strict"),"+"*8),("credits","rules") def Show(): for I in Hint: print(I) return None def PlayerLog(): Query.Show() for I in Log: print(I) print("<"*8) return None def Credits(): for I in Query.Information: print(I) return None def Rules(): for I in Query.HowToPlay: print(I) return None def Begin(): global Player1Circle,Player1First while True: Player1Circle=input(b'\xcd\xe6\xbc\xd2\xd2\xbb\xa1\xaa\xa1\xaa\xc4\xfa\xcf\xeb\xbb\xad\xd4\xb2\xbb\xb9\xca\xc7\xbb\xad\xb2\xe6\xa3\xbf\xc8\xe7\xb9\xfb\xc4\xfa\xcf\xeb\xbb\xad\xb2\xe6\xa3\xac\xc7\xeb\xca\xe4\xc8\xeb\xa1\xb00\xa1\xb1\xa3\xba'.decode("gbk","strict"))!="0" print(b'~{Mf<RR;Q!Tq;-T2#!~}'.decode("hz","strict")) if Player1Circle else print(b'~{Mf<RR;Q!Tq;-2f#!~}'.decode("hz","strict")) if input(b'\xcd\xe6\xbc\xd2\xb6\xfe\xa1\xaa\xa1\xaa\xc4\xfa\xca\xc7\xb7\xf1\xcd\xac\xd2\xe2\xcd\xe6\xbc\xd2\xd2\xbb\xb5\xc4\xbe\xf6\xb6\xa8\xa3\xbf\xc8\xe7\xb9\xfb\xc4\xfa\xcf\xeb\xbe\xdc\xbe\xf8\xa3\xac\xc7\xeb\xca\xe4\xc8\xeb\xa1\xb00\xa1\xb1\xa3\xba'.decode("gbk","strict"))=="0": print(b'~{Mf<R6~>\\>xMf<RR;5D>v6(#!~}'.decode("hz","strict")) else: break while True: Player1First=input(b'\xcd\xe6\xbc\xd2\xd2\xbb\xa1\xaa\xa1\xaa\xc4\xfa\xca\xc7\xb7\xf1\xcf\xeb\xcf\xc8\xd7\xdf\xc6\xe5\xa3\xbf\xc8\xe7\xb9\xfb\xc4\xfa \xb2\xbb\xcf\xeb \xcf\xc8\xd7\xdf\xc6\xe5\xa3\xac\xc7\xeb\xca\xe4\xc8\xeb\xa1\xb00\xa1\xb1\xa3\xba'.decode("gbk","strict"))!="0" print(b'~{Mf<RR;OkOHW_Fe#!~}'.decode("hz","strict")) if Player1First else print(b'~{Mf<RR;Ok:sW_Fe#!~}'.decode("hz","strict")) if input(b'\xcd\xe6\xbc\xd2\xb6\xfe\xa1\xaa\xa1\xaa\xc4\xfa\xca\xc7\xb7\xf1\xcd\xac\xd2\xe2\xcd\xe6\xbc\xd2\xd2\xbb\xb5\xc4\xbe\xf6\xb6\xa8\xa3\xbf\xc8\xe7\xb9\xfb\xc4\xfa\xcf\xeb\xbe\xdc\xbe\xf8\xa3\xac\xc7\xeb\xca\xe4\xc8\xeb\xa1\xb00\xa1\xb1\xa3\xba'.decode("gbk","strict"))=="0": print(b'~{Mf<R6~>\\>xMf<RR;5D>v6(#!~}'.decode("hz","strict")) else: break print(b'~{Mf<RR;;-T2#,Mf<R6~;-2f!#~}'.decode("hz","strict")) if Player1Circle else print(b'~{Mf<RR;;-2f#,Mf<R6~;-T2!#~}'.decode("hz","strict")) print(b'~{OVTZGkMf<RR;W_Fe#!~}'.decode("hz","strict")) if Player1First else print(b'~{OVTZGkMf<R6~W_Fe#!~}'.decode("hz","strict")) Query.Show() return Noneclass Action: def Player1(): global Player1Grid,Log while True: AnswerStr=input(b'\xcd\xe6\xbc\xd2\xd2\xbb\xa1\xaa\xa1\xaa\xc7\xeb\xd4\xda\xb8\xf1\xbf\xda\xc4\xda\xcf\xc2\xc6\xe5\xa3\xba'.decode("gbk","strict")) if AnswerStr in GridStr: Answer=int(AnswerStr) if Answer in Grid: if Answer in Player1Grid: Query.PlayerLog() print(b'\xcd\xe6\xbc\xd2\xd2\xbb\xa1\xaa\xa1\xaa\xc4\xfa\xd2\xd1\xbe\xad\xd4\xda\xd5\xe2\xb8\xf6\xb8\xf1\xbf\xda\xcf\xc2\xb9\xfd\xc6\xe5\xa3\xa1'.decode("gbk","strict")) elif Answer in Player2Grid: Query.PlayerLog() print(b'\xcd\xe6\xbc\xd2\xd2\xbb\xa1\xaa\xa1\xaa\xc4\xfa\xb5\xc4\xb6\xd4\xca\xd6\xd2\xd1\xbe\xad\xd4\xda\xd5\xe2\xb8\xf6\xb8\xf1\xbf\xda\xcf\xc2\xb9\xfd\xc6\xe5\xa3\xa1'.decode("gbk","strict")) else: CurrentTime=localtime() Draw.Circle(Answer) if Player1Circle else Draw.Cross(Answer) Player1Grid+=[Answer] Log+=[('{0}'+b'~{Dj~}'.decode("hz","strict")+'{1}'+b'~{TB~}'.decode("hz","strict")+'{2}'+b'~{HU~}'.decode("hz","strict")+'{3:02d}'+b'~{J1~}'.decode("hz","strict")+'{4:02d}'+b'~{7V~}'.decode("hz","strict")+'{5:02d}'+b'~{Ck#,Mf<RR;TZ8q?Z~}'.decode("hz","strict")+'{6}'+b'~{;-~}'.decode("hz","strict")+'{7}').format(CurrentTime[0],CurrentTime[1],CurrentTime[2],CurrentTime[3],CurrentTime[4],CurrentTime[5],Answer,b'~{T2~}'.decode("hz","strict") if Player1Circle else b'~{2f~}'.decode("hz","strict"))] return None else: Query.PlayerLog() print(b'\xcd\xe6\xbc\xd2\xd2\xbb\xa1\xaa\xa1\xaa\xce\xb4\xd6\xaa\xd2\xe7\xb3\xf6\xb4\xed\xce\xf3\xa1\xa3\xb7\xb3\xc7\xeb\xd6\xd8\xc0\xb4\xa1\xa3'.decode("gbk","strict")) elif AnswerStr==Query.Key[0]: Query.Credits() elif AnswerStr==Query.Key[1]: Query.Rules() else: Query.PlayerLog() print(b'\xcd\xe6\xbc\xd2\xd2\xbb\xa1\xaa\xa1\xaa\xc7\xeb\xca\xe4\xc8\xeb\xd2\xbb\xb8\xf6\xb7\xb6\xce\xa7\xc4\xda\xb5\xc4\xd3\xd0\xd0\xa7\xd5\xfb\xca\xfd\xa3\xa1'.decode("gbk","strict")) def Player2(): global Player2Grid,Log while True: AnswerStr=input(b'\xcd\xe6\xbc\xd2\xb6\xfe\xa1\xaa\xa1\xaa\xc7\xeb\xd4\xda\xb8\xf1\xbf\xda\xc4\xda\xcf\xc2\xc6\xe5\xa3\xba'.decode("gbk","strict")) if AnswerStr in GridStr: Answer=int(AnswerStr) if Answer in Grid: if Answer in Player2Grid: Query.PlayerLog() print(b'\xcd\xe6\xbc\xd2\xb6\xfe\xa1\xaa\xa1\xaa\xc4\xfa\xd2\xd1\xbe\xad\xd4\xda\xd5\xe2\xb8\xf6\xb8\xf1\xbf\xda\xcf\xc2\xb9\xfd\xc6\xe5\xa3\xa1'.decode("gbk","strict")) elif Answer in Player1Grid: Query.PlayerLog() print(b'\xcd\xe6\xbc\xd2\xb6\xfe\xa1\xaa\xa1\xaa\xc4\xfa\xb5\xc4\xb6\xd4\xca\xd6\xd2\xd1\xbe\xad\xd4\xda\xd5\xe2\xb8\xf6\xb8\xf1\xbf\xda\xcf\xc2\xb9\xfd\xc6\xe5\xa3\xa1'.decode("gbk","strict")) else: CurrentTime=localtime() Draw.Cross(Answer) if Player1Circle else Draw.Circle(Answer) Player2Grid+=[Answer] Log+=[('{0}'+b'~{Dj~}'.decode("hz","strict")+'{1}'+b'~{TB~}'.decode("hz","strict")+'{2}'+b'~{HU~}'.decode("hz","strict")+'{3:02d}'+b'~{J1~}'.decode("hz","strict")+'{4:02d}'+b'~{7V~}'.decode("hz","strict")+'{5:02d}'+b'~{Ck#,Mf<R6~TZ8q?Z~}'.decode("hz","strict")+'{6}'+b'~{;-~}'.decode("hz","strict")+'{7}').format(CurrentTime[0],CurrentTime[1],CurrentTime[2],CurrentTime[3],CurrentTime[4],CurrentTime[5],Answer,b'~{2f~}'.decode("hz","strict") if Player1Circle else b'~{T2~}'.decode("hz","strict"))] return None else: Query.PlayerLog() print(b'\xcd\xe6\xbc\xd2\xb6\xfe\xa1\xaa\xa1\xaa\xce\xb4\xd6\xaa\xd2\xe7\xb3\xf6\xb4\xed\xce\xf3\xa1\xa3\xb7\xb3\xc7\xeb\xd6\xd8\xc0\xb4\xa1\xa3'.decode("gbk","strict")) elif AnswerStr==Query.Key[0]: Query.Credits() elif AnswerStr==Query.Key[1]: Query.Rules() else: Query.PlayerLog() print(b'\xcd\xe6\xbc\xd2\xb6\xfe\xa1\xaa\xa1\xaa\xc7\xeb\xca\xe4\xc8\xeb\xd2\xbb\xb8\xf6\xb7\xb6\xce\xa7\xc4\xda\xb5\xc4\xd3\xd0\xd0\xa7\xd5\xfb\xca\xfd\xa3\xa1'.decode("gbk","strict"))class Judge: def Winner(): Pl1Gl,Pl2Gl=Player1Grid.__len__(),Player2Grid.__len__() S=("",b'~{Mf<RR;J$@{#!~}'.decode("hz","strict"),b'~{Mf<R6~J$@{#!~}'.decode("hz","strict"),b'~{F=>V#!~}'.decode("hz","strict")) if Player1First: if Pl1Gl>2: if Pl1Gl==3: if Player1Grid in Win: Draw.WinLine(Player1Grid) return S[1] if Pl2Gl==3: if Player2Grid in Win: Draw.WinLine(Player2Grid) return S[2] elif Pl1Gl==4: for I in [Player1Grid[0:3],Player1Grid[1:4],Player1Grid[0:2]+Player1Grid[3:4],Player1Grid[2:4]+Player1Grid[0:1]]: if I in Win: Draw.WinLine(I) return S[1] if Pl2Gl==4: for I in [Player2Grid[0:3],Player2Grid[1:4],Player2Grid[0:2]+Player2Grid[3:4],Player2Grid[2:4]+Player2Grid[0:1]]: if I in Win: Draw.WinLine(I) return S[2] elif Pl1Gl==5: for I in [Player1Grid[0:3],Player1Grid[1:4],Player1Grid[2:5],Player1Grid[0:5:2],Player1Grid[0:2]+Player1Grid[3:4],Player1Grid[0:2]+Player1Grid[4:5],Player1Grid[2:4]+Player1Grid[0:1],Player1Grid[3:5]+Player1Grid[0:1],Player1Grid[1:3]+Player1Grid[4:5],Player1Grid[3:5]+Player1Grid[1:2]]: if I in Win: Draw.WinLine(I) return S[1] return S[3] else: if Pl2Gl>2: if Pl2Gl==3: if Player2Grid in Win: Draw.WinLine(Player2Grid) return S[2] if Pl1Gl==3: if Player1Grid in Win: Draw.WinLine(Player1Grid) return S[1] elif Pl2Gl==4: for I in [Player2Grid[0:3],Player2Grid[1:4],Player2Grid[0:2]+Player2Grid[3:4],Player2Grid[2:4]+Player2Grid[0:1]]: if I in Win: Draw.WinLine(I) return S[2] if Pl1Gl==4: for I in [Player1Grid[0:3],Player1Grid[1:4],Player1Grid[0:2]+Player1Grid[3:4],Player1Grid[2:4]+Player1Grid[0:1]]: if I in Win: Draw.WinLine(I) return S[1] elif Pl2Gl==5: for I in [Player2Grid[0:3],Player2Grid[1:4],Player2Grid[2:5],Player2Grid[0:5:2],Player2Grid[0:2]+Player2Grid[3:4],Player2Grid[0:2]+Player2Grid[4:5],Player2Grid[2:4]+Player2Grid[0:1],Player2Grid[3:5]+Player2Grid[0:1],Player2Grid[1:3]+Player2Grid[4:5],Player2Grid[3:5]+Player2Grid[1:2]]: if I in Win: Draw.WinLine(I) return S[2] return S[3] return S[0] def Turn(): Player1sTurn=Player1First while True: Continue=Judge.Winner() if Continue=="": Action.Player1() if Player1sTurn else Action.Player2() Player1sTurn=not Player1sTurn Query.PlayerLog() else: return Continuedef Run(): Initialize() Query.Begin() print(b'~{U=9{#:~}'.decode("hz","strict")+Judge.Turn()+"\n"+b'~{>+2J5D6TU=#!;6S-TY@4#!~}'.decode("hz","strict")) sleep(5) return Nonetry: Run()except: EI=exc_info() print() excepthook(EI[0],EI[1],EI[2]) if Terminator.__subclasscheck__(EI[0]): print(b"~{!>4mNs4zBk#:~}2~{!?Dz9JRb9X1UAK~}'turtle'~{40?Z#!~}".decode("hz","strict")) exit(2) else: print(b'~{!>4mNs4zBk#:~}3~{!?3LPrSv5=AKFdK|4mNs#,U}TZMK3v!-!-~}'.decode("hz","strict")) exit(3)else: exit(0)
划线
评论
复制
发布于: 2020 年 10 月 03 日阅读数: 171
版权声明: 本文为 InfoQ 作者【MengZian】的原创文章。
原文链接:【http://xie.infoq.cn/article/19437f67342e0c5d949f1fe66】。未经作者许可,禁止转载。
MengZian
关注
还未添加个人签名 2020.10.03 加入
还未添加个人简介
评论