鱼C论坛

 找回密码
 立即注册
查看: 1168|回复: 4

[作品展示] [tkinter] 三子棋游戏

[复制链接]
发表于 2024-9-4 19:29:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
双方各三子,轮流走子。不能吃子,只能走到空位。将对方三子全逼到上部圆内为胜。如一方无棋可走(没有被逼到圆内),另一方要退一步,让对方走棋。
本代码还未实现这个功能。

  1. from tkinter import *
  2. from tkinter import messagebox
  3. root=Tk()
  4. root.title("三子棋")
  5. screen_width = root.winfo_screenwidth()
  6. screen_height = root.winfo_screenheight()
  7. w=500
  8. h=600
  9. x=(screen_width-w)/2
  10. y=(screen_height-h)/2
  11. root.geometry('%dx%d+%d+%d' % (w,h,x,y))
  12. cv=Canvas(root,width=500,height=500,bg='#008B8B')
  13. chessname=['红','黑']
  14. chessmap=[[-1,-1,-1] for x in range(3)]
  15. dict_ChessName={}
  16. LocalPlayer='红'
  17. first=True
  18. rect1=0
  19. rect2=0
  20. player=0
  21. def DrawBoard():
  22.     for i in range(0, 3):
  23.         cv.create_oval(100+160*i-50,50,100+160*i+50,150, width=5,outline='#C0C0C0')
  24.         cv.create_line(100+160*i,150, 100+160*i, 420, width=5,fill='#C0C0C0')
  25.     cv.create_line(100 , 260, 420,  260, width=5,fill='#C0C0C0')
  26.     cv.create_line(100, 420, 420, 420, width=5,fill='#C0C0C0')
  27. def LoadChess():
  28.     global chessmap
  29.     for i in range(0,3):
  30.         id=cv.create_oval(70,70 + 160 * i , 130, 130 + 160 * i,  fill='#FF7F50',outline='#FF7F50')
  31.         chessmap[0][i]=id
  32.         dict_ChessName[id] = '红'
  33.         print('d0=', dict_ChessName[id])

  34.     for i in range(0,3):
  35.         id = cv.create_oval(390,70 + 160 * i, 450, 130 + 160 * i , fill='black')
  36.         chessmap[2][i]=id
  37.         dict_ChessName[id] = '黑'
  38.         print('d=',dict_ChessName[id])

  39. def play(event):
  40.     global LocalPlayer,player,player1 ,player2 ,player3 ,chessmap,rect1,rect2,x1,x2,y1,y2,first
  41.     player1 = chessmap[0][0]
  42.     player2 = chessmap[1][0]
  43.     player3 = chessmap[2][0]
  44.     x=(event.x-20)//160
  45.     y=(event.y-20)//160
  46.     cv.delete(rect2)
  47.     if first:
  48.         x1=x
  49.         y1=y
  50.         player=chessmap[x1][y1]
  51.         if not(chessmap[x1][y1]==-1):
  52.             print('第一次单击')
  53.             first=False
  54.             rect1=cv.create_rectangle(160*x1+100-40,y1*160+100-40,160*x1+100+40,y1*160+100+40,outline='red')
  55.     else:
  56.         x2=x
  57.         y2=y
  58.         print(x1,y1,x2,y2)
  59.         if x2==x1 and y2==y1:
  60.             myMsg3()
  61.             print('取消第一次单击')
  62.             cv.delete(rect1)
  63.             first = True
  64.             return
  65.         if not(chessmap[x2][y2]==-1):
  66.             myMsg2()
  67.             print('只能走到空位!')
  68.             return None

  69.         if IsAbleToPut(x1,x2,y1,y2):
  70.             rect2 = cv.create_rectangle(160 * x2 + 100 - 40, y2 * 160 + 100 - 40, 160 * x2 + 100 + 40,
  71.                                         y2 * 160 + 100 + 40, outline='yellow')
  72.             cv.move(player,160*(x2-x1),160*(y2-y1))
  73.             chessmap[x1][y1]=-1
  74.             chessmap[x2][y2]=player
  75.             print('chessmap[x2][y2]=',chessmap[x2][y2])
  76.             print('chessmap=',chessmap)
  77.             cv.delete(rect1)
  78.             if game_over():
  79.                 root.destroy()
  80.             first=True
  81.             SetMyTurn(False)
  82.             return
  83.         else:
  84.             return None

  85. def game_over():
  86.     try:
  87.         if dict_ChessName[player1]==dict_ChessName[player2] and dict_ChessName[player1]==dict_ChessName[player3] :
  88.             if dict_ChessName[player1] == '黑':
  89.                 label1['text'] = '红方胜利!'
  90.                 myMsg4()
  91.                 return True
  92.             elif dict_ChessName[player1] == '红':
  93.                 label1['text'] = '黑方胜利!'
  94.                 myMsg5()
  95.                 return True
  96.     except KeyError :
  97.         pass
  98.     else:
  99.         return False

  100. def SetMyTurn(flag):
  101.     global LocalPlayer
  102.     IsMyTurn=flag
  103.     if LocalPlayer=='红':
  104.         LocalPlayer='黑'
  105.         label1['text']='轮到黑方走'
  106.     else:
  107.         LocalPlayer='红'
  108.         label1['text'] = '轮到红方走'
  109. def IsAbleToPut(x1,x2,y1,y2):
  110.     if ((x2-x1)*(y2-y1)!=0):
  111.         return False
  112.     if(abs(x2-x1)>1 or abs(y2-y1)>1):
  113.         return False
  114.     if (x2<0 or x2>2 or y2<0 or y2>2):
  115.         return False
  116.     if x1==0 and y1==0 and x2==1 and y2==0:
  117.         myMsg1()
  118.         print('此路不通!')
  119.         return False
  120.     if x1==2 and y1==0 and x2==1 and y2==0:
  121.         myMsg1()
  122.         print('此路不通!')
  123.         return False
  124.     if x1==1 and y1==0 and x2==0 and  y2==0:
  125.         myMsg1()
  126.         print('此路不通!')
  127.         return False
  128.     if  x1==1 and y1==0 and x2==2 and y2==0:
  129.         myMsg1()
  130.         print('此路不通!')
  131.         return False
  132.     return True

  133. def myMsg1():
  134.     messagebox.showerror('错误!','此路不通!')
  135. def myMsg2():
  136.     messagebox.showerror('错误!','只能走到空位!')
  137. def myMsg3():
  138.     messagebox.showinfo('提示','取消第一次单击!')
  139. def myMsg4():
  140.     messagebox.showinfo('提示','红方胜利!')
  141. def myMsg5():
  142.     messagebox.showinfo('提示','黑方胜利!')

  143. DrawBoard()
  144. LoadChess()
  145. cv.bind('<Button-1>',play)
  146. cv.pack()
  147. label1=Label(root,fg='red',bg='white',text='红方先走', font=("华文行楷", 20))
  148. label1.pack()
  149. label2=Label(root,fg='blue',bg='white',text='走法:不能吃子,一方三子全被逼到上面圆里为输。', font=("kaiti", 10))
  150. label2.pack()
  151. root.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-9-6 13:39:04 | 显示全部楼层
沙发
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-9-7 16:45:25 | 显示全部楼层
@FishC, 能将上面游戏改为人机对战吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-10-21 19:20:18 From FishC Mobile | 显示全部楼层
到这个水平需要多久
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-10-21 21:17:54 | 显示全部楼层
初级水平,好玩而已
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-7 19:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表