目标:
接上篇,此篇完成五子棋的逻辑部分。
思路:
- 分析:需要一个判断输赢的逻辑、一个将棋子落在目标位置的逻辑、标签和按钮对应的方法
- 判断输赢:
- 分别对落子位置的横纵轴位置和两个斜边位置的同色棋子进行计数
- 如果任意一处同色棋子数量达到5或5以上,则判断赢家和游戏结束
- 每次棋子落下,都要调用一次判断输赢的逻辑
- 落子:
- 每次坐标点击会捕获一个坐标位置,找出离这个坐标最近的棋位
- 落子
实现:
1# -*- coding: utf-8 -*-
2"""
3Created on Fri Feb 2 20:04:38 2018
4
5@author: 23755
6"""
7
8import tkinter as tk
9
10PIECE_SIZE = 10
11
12click_x = 0
13click_y = 0
14
15pieces_x = [i for i in range(32, 523, 35)]
16pieces_y = [i for i in range(38, 529, 35)]
17
18coor_black = []
19coor_white = []
20
21person_flag = 1
22piece_color = "black"
23
24#右上方的棋子提示(工具)
25def showChange(color):
26 global piece_color
27 piece_color = color
28 side_canvas.delete("show_piece")
29 side_canvas.create_oval(110 - PIECE_SIZE, 25 - PIECE_SIZE,
30 110 + PIECE_SIZE, 25 + PIECE_SIZE,
31 fill = piece_color, tags = ("show_piece"))
32
33#输赢的提示、游戏结束的提示(工具)
34def pushMessage():
35 if person_flag == -1:
36 var1.set("白棋赢")
37 elif person_flag == 1:
38 var1.set("黑棋赢")
39 var2.set("游戏结束")
40
41#棋子的计数(工具)
42def piecesCount(coor, pieces_count, t1, t2):
43 for i in range(1, 5):
44 (x, y) = (click_x + t1 * 35 * i, click_y + t2 * 35 * i)
45 if (x, y) in coor:
46 pieces_count += 1
47 else:
48 break
49 return pieces_count
50
51#事件监听处理
52def coorBack(event): #return coordinates of cursor 返回光标坐标
53 global click_x, click_y
54 click_x = event.x
55 click_y = event.y
56 coorJudge()
57
58#定义重置按钮的功能
59def gameReset():
60 global person_flag, coor_black, coor_white, piece_color
61 person_flag = 1 #打开落子开关
62 var.set("执黑棋") #还原提示标签
63 var1.set("") #还原输赢提示标签
64 var2.set("") #还原游戏结束提示标签
65 showChange("black") #还原棋子提示图片
66 canvas.delete("piece")#删除所有棋子
67 coor_black = [] #清空黑棋坐标存储器
68 coor_white = [] #清空白棋坐标存储器
69
70
71'''判断输赢的逻辑'''
72#preJudge调用realJudge0,realJudge0调用realJudge1和realJudge2;
73#realJudge1负责横纵两轴的计数,realJudge2负责两斜线方向的计数
74#realJudge0汇总指定颜色棋子结果,作出决策,判断是否游戏结束
75#preJudge决定是判断黑棋还是判断白棋,对两种颜色的棋子判断进行导流
76def preJudge(piece_color):
77 if piece_color == "black":
78 realJudge0(coor_black)
79 elif piece_color == "white":
80 realJudge0(coor_white)
81
82def realJudge0(coor):
83 global person_flag, person_label
84
85 if realJudge1(coor) == 1 or realJudge2(coor) == 1:
86 pushMessage()
87 person_flag = 0
88
89def realJudge1(coor):
90 pieces_count = 0
91 pieces_count = piecesCount(coor, pieces_count, 1, 0) #右边
92 pieces_count = piecesCount(coor, pieces_count, -1, 0)#左边
93 if pieces_count >= 4:
94 return 1
95 else:
96 pieces_count = 0
97 pieces_count = piecesCount(coor, pieces_count, 0, -1)#上边
98 pieces_count = piecesCount(coor, pieces_count, 0, 1) #下边
99 if pieces_count >= 4:
100 return 1
101 else:
102 return 0
103
104def realJudge2(coor):
105 pieces_count = 0
106 pieces_count = piecesCount(coor, pieces_count, 1, 1) #右下角
107 pieces_count = piecesCount(coor, pieces_count, -1, -1)#左上角
108 if pieces_count >= 4:
109 return 1
110 else:
111 pieces_count = 0
112 pieces_count = piecesCount(coor, pieces_count, 1, -1) #右上角
113 pieces_count = piecesCount(coor, pieces_count, -1, 1) #左下角
114 if pieces_count >= 4:
115 return 1
116 else:
117 return 0
118
119'''落子的逻辑'''
120#落子
121def putPiece(piece_color):
122 global coor_black, coor_white
123 canvas.create_oval(click_x - PIECE_SIZE, click_y - PIECE_SIZE,
124 click_x + PIECE_SIZE, click_y + PIECE_SIZE,
125 fill = piece_color, tags = ("piece"))
126 if piece_color == "white":
127 coor_white.append( (click_x, click_y) )
128 elif piece_color == "black":
129 coor_black.append( (click_x, click_y) )
130 preJudge(piece_color) #每放置一枚棋子就对该种颜色的棋子进行一次判断
131
132#找出离鼠标点击位置最近的棋盘线交点,调用putPiece落子
133def coorJudge():
134 global click_x, click_y
135 coor = coor_black + coor_white
136 global person_flag, show_piece
137 #print("x = %s, y = %s" % (click_x, click_y))
138 item = canvas.find_closest(click_x, click_y)
139 tags_tuple = canvas.gettags(item)
140 if len(tags_tuple) > 1:
141 tags_list = list(tags_tuple)
142 coor_list = tags_list[:2]
143 try:
144 for i in range(len(coor_list)):
145 coor_list[i] = int(coor_list[i])
146 except ValueError:
147 pass
148 else:
149 coor_tuple = tuple(coor_list)
150 (click_x, click_y) = coor_tuple
151 #print("tags = ", tags_tuple, "coors = ", coor_tuple)
152 if ( (click_x, click_y) not in coor )and( click_x in pieces_x )and( click_y in pieces_y ):
153 #print("True")
154 if person_flag != 0:
155 if person_flag == 1:
156 putPiece("black")
157 showChange("white")
158 var.set("执白棋")
159 elif person_flag == -1:
160 putPiece("white")
161 showChange("black")
162 var.set("执黑棋")
163 person_flag *= -1
164 #else:
165 #print("False")
166
167
效果图:
延伸:
可以另写一个逻辑作为对手进行对弈。
参考代码
我的GitHub仓库
此为旧文,图片水印为我的CSDN账号,无妨