本文是使用python创建的一个基于tkinter库的GUI界面,用于实现五子棋游戏。编辑器使用的是spyder,该工具。既方便做数据分析,又可以做小工具开发,
首先,导入tkinter库:import tkinter as tk
,这里使用tkinter库来创建GUI界面。
然后创建GobangGame
类:这个类包含了五子棋游戏的逻辑和界面显示。其中,make_move
函数是在玩家点击棋盘上的按钮时调用的函数,用于处理玩家的落子操作。具体来说,当玩家点击棋盘上的某个按钮时,make_move
函数会被调用,并传入按钮所在的行和列的索引。然后,该函数会检查对应的棋盘位置是否为空,如果为空,则将该位置标记为当前玩家的标记('X'或'O'),并禁用对应的按钮,表示该位置已经落子。接着,函数会检查当前玩家是否获胜,如果有则显示获胜信息,并禁用所有按钮。否则,切换到另一个玩家,准备让另一个玩家继续落子。
class GobangGame:
def __init__(self, root):
self.root = root
self.root.title("五子棋游戏")
self.board = [[' ' for _ in range(15)] for _ in range(15)]
self.current_player = 'X'
self.buttons = [[tk.Button(root, text=' ', width=2, height=1, command=lambda i=i, j=j: self.make_move(i, j)) for j in range(15)] for i in range(15)]
for i in range(15):
for j in range(15):
self.buttons[i][j].grid(row=i, column=j)
self.status_label = tk.Label(root, text=f"当前玩家: {self.current_player}")
self.status_label.grid(row=15, columnspan=15)
def make_move(self, i, j):
if self.board[i][j] == ' ':
self.board[i][j] = self.current_player
self.buttons[i][j].config(text=self.current_player, state='disabled')
if self.check_winner(i, j):
self.status_label.config(text=f"玩家 {self.current_player} 赢了!")
for i in range(15):
for j in range(15):
self.buttons[i][j].config(state='disabled')
else:
self.current_player = 'O' if self.current_player == 'X' else 'X'
self.status_label.config(text=f"当前玩家: {self.current_player}")
def check_winner(self, i, j):
directions = [(1, 0), (0, 1), (1, 1), (1, -1)]
for dx, dy in directions:
count = 1
for k in range(1, 5):
x, y = i + k*dx, j + k*dy
if 0 <= x < 15 and 0 <= y < 15 and self.board[x][y] == self.current_player:
count += 1
else:
break
for k in range(1, 5):
x, y = i - k*dx, j - k*dy
if 0 <= x < 15 and 0 <= y < 15 and self.board[x][y] == self.current_player:
count += 1
else:
break
if count >= 5:
return True
return False
check_winner
函数用于检查当前玩家是否获胜。它遍历四个方向(水平、垂直、正斜线、反斜线),检查是否有连续的五个棋子,如果有则返回True,表示当前玩家获胜。具体来说,该函数会对每个棋盘位置进行检查,以当前位置为起点,向四个方向(水平、垂直、正斜线、反斜线)延伸,统计相同标记的棋子数目。如果在某个方向上找到了连续的五个相同标记的棋子,就表示当前玩家获胜。
完整代码如下:
import tkinter as tk
class GobangGame:
def __init__(self, root):
self.root = root
self.root.title("五子棋游戏")
self.board = [[' ' for _ in range(15)] for _ in range(15)]
self.current_player = 'X'
self.buttons = [[tk.Button(root, text=' ', width=2, height=1, command=lambda i=i, j=j: self.make_move(i, j)) for j in range(15)] for i in range(15)]
for i in range(15):
for j in range(15):
self.buttons[i][j].grid(row=i, column=j)
self.status_label = tk.Label(root, text=f"当前玩家: {self.current_player}")
self.status_label.grid(row=15, columnspan=15)
def make_move(self, i, j):
if self.board[i][j] == ' ':
self.board[i][j] = self.current_player
self.buttons[i][j].config(text=self.current_player, state='disabled')
if self.check_winner(i, j):
self.status_label.config(text=f"玩家 {self.current_player} 赢了!")
for i in range(15):
for j in range(15):
self.buttons[i][j].config(state='disabled')
else:
self.current_player = 'O' if self.current_player == 'X' else 'X'
self.status_label.config(text=f"当前玩家: {self.current_player}")
def check_winner(self, i, j):
directions = [(1, 0), (0, 1), (1, 1), (1, -1)]
for dx, dy in directions:
count = 1
for k in range(1, 5):
x, y = i + k*dx, j + k*dy
if 0 <= x < 15 and 0 <= y < 15 and self.board[x][y] == self.current_player:
count += 1
else:
break
for k in range(1, 5):
x, y = i - k*dx, j - k*dy
if 0 <= x < 15 and 0 <= y < 15 and self.board[x][y] == self.current_player:
count += 1
else:
break
if count >= 5:
return True
return False
# 创建主窗口
root = tk.Tk()
game = GobangGame(root)
root.mainloop()
运行结果如下,支持双人对战