Skip to content

Commit

Permalink
调整了五子棋代码结构顺序,给代码添加了一些注释
Browse files Browse the repository at this point in the history
  • Loading branch information
NICE-FUTURE committed Jul 26, 2018
1 parent b1422d7 commit dfc3ee7
Showing 1 changed file with 35 additions and 20 deletions.
55 changes: 35 additions & 20 deletions python/gobang/gobang.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
person_flag = 1
piece_color = "black"

#右上方的棋子提示(工具)
def showChange(color):
global piece_color
piece_color = color
Expand All @@ -29,13 +30,15 @@ def showChange(color):
110 + PIECE_SIZE, 25 + PIECE_SIZE,
fill = piece_color, tags = ("show_piece"))

#输赢的提示、游戏结束的提示(工具)
def pushMessage():
if person_flag == -1:
var1.set("白棋赢")
elif person_flag == 1:
var1.set("黑棋赢")
var2.set("游戏结束")

#棋子的计数(工具)
def piecesCount(coor, pieces_count, t1, t2):
for i in range(1, 5):
(x, y) = (click_x + t1 * 35 * i, click_y + t2 * 35 * i)
Expand All @@ -45,6 +48,31 @@ def piecesCount(coor, pieces_count, t1, t2):
break
return pieces_count

#事件监听处理
def coorBack(event): #return coordinates of cursor 返回光标坐标
global click_x, click_y
click_x = event.x
click_y = event.y
coorJudge()

#定义重置按钮的功能
def gameReset():
global person_flag, coor_black, coor_white, piece_color
person_flag = 1 #打开落子开关
var.set("执黑棋") #还原提示标签
var1.set("") #还原输赢提示标签
var2.set("") #还原游戏结束提示标签
showChange("black") #还原棋子提示图片
canvas.delete("piece")#删除所有棋子
coor_black = [] #清空黑棋坐标存储器
coor_white = [] #清空白棋坐标存储器


'''判断输赢的逻辑'''
#preJudge调用realJudge0,realJudge0调用realJudge1和realJudge2;
#realJudge1负责横纵两轴的计数,realJudge2负责两斜线方向的计数
#realJudge0汇总指定颜色棋子结果,作出决策,判断是否游戏结束
#preJudge决定是判断黑棋还是判断白棋,对两种颜色的棋子判断进行导流
def preJudge(piece_color):
if piece_color == "black":
realJudge0(coor_black)
Expand Down Expand Up @@ -88,7 +116,8 @@ def realJudge2(coor):
else:
return 0

#放置棋子
'''落子的逻辑'''
#落子
def putPiece(piece_color):
global coor_black, coor_white
canvas.create_oval(click_x - PIECE_SIZE, click_y - PIECE_SIZE,
Expand All @@ -98,8 +127,9 @@ def putPiece(piece_color):
coor_white.append( (click_x, click_y) )
elif piece_color == "black":
coor_black.append( (click_x, click_y) )
preJudge(piece_color)
preJudge(piece_color) #每放置一枚棋子就对该种颜色的棋子进行一次判断

#找出离鼠标点击位置最近的棋盘线交点,调用putPiece落子
def coorJudge():
global click_x, click_y
coor = coor_black + coor_white
Expand Down Expand Up @@ -134,22 +164,7 @@ def coorJudge():
#else:
#print("False")

def coorBack(event): #return coordinates of cursor 返回光标坐标
global click_x, click_y
click_x = event.x
click_y = event.y
coorJudge()

def gameReset():
global person_flag, coor_black, coor_white, piece_color
person_flag = 1 #打开落子开关
var.set("执黑棋") #还原提示标签
var1.set("") #还原输赢提示标签
var2.set("") #还原游戏结束提示标签
showChange("black") #还原棋子提示图片
canvas.delete("piece")#删除所有棋子
coor_black = [] #清空黑棋坐标存储器
coor_white = [] #清空白棋坐标存储器


"""窗口主体"""
root = tk.Tk()
Expand Down Expand Up @@ -192,7 +207,7 @@ def gameReset():
"""棋盘绘制"""
#背景
canvas = tk.Canvas(root, bg = "saddlebrown", width = 540, height = 540)
canvas.bind("<Button-1>", coorBack)
canvas.bind("<Button-1>", coorBack) #鼠标单击事件绑定
canvas.grid(row = 0, column = 0, rowspan = 6)
#线条
for i in range(15):
Expand All @@ -205,7 +220,7 @@ def gameReset():
canvas.create_oval(35 * point_x[i] + 28, 35 * point_y[i] + 33,
35 * point_x[i] + 36, 35 * point_y[i] + 41, fill = "black")

#透明棋子
#透明棋子(设置透明棋子,方便后面落子的坐标定位到正确的位置)
for i in pieces_x:
for j in pieces_y:
canvas.create_oval(i - PIECE_SIZE, j - PIECE_SIZE,
Expand Down

0 comments on commit dfc3ee7

Please sign in to comment.