用python写一个字符界面人机对战的五子棋游戏,游戏难度中等
时间: 2024-05-16 07:12:57 浏览: 115
好的,我可以为您提供一个中等难度的五子棋游戏,使用 Python 的 tkinter 模块实现字符界面。以下是代码:
```python
import tkinter as tk
import tkinter.messagebox as msg
class Gobang:
def __init__(self):
self.size = 15
self.board = [[0] * self.size for _ in range(self.size)]
self.current_player = 1
self.winner = 0
self.init_gui()
def init_gui(self):
self.root = tk.Tk()
self.root.title('五子棋人机对战')
self.root.resizable(0, 0)
self.canvas = tk.Canvas(self.root, width=500, height=500, bg='white')
self.canvas.pack(side=tk.LEFT)
self.canvas.bind('<Button-1>', self.click)
self.draw_board()
self.status_var = tk.StringVar()
self.status_var.set('黑方先手')
self.status_label = tk.Label(self.root, textvariable=self.status_var, font=('Arial', 12), width=20, height=2)
self.status_label.pack(side=tk.TOP, pady=10)
self.restart_button = tk.Button(self.root, text='重新开始', font=('Arial', 12), width=10, height=2, command=self.restart)
self.restart_button.pack(side=tk.TOP, pady=10)
def draw_board(self):
for i in range(self.size):
self.canvas.create_line(50, 50 + i * 30, 470, 50 + i * 30)
self.canvas.create_line(50 + i * 30, 50, 50 + i * 30, 470)
self.canvas.create_oval(117, 117, 123, 123, fill='black')
self.canvas.create_oval(357, 117, 363, 123, fill='black')
self.canvas.create_oval(117, 357, 123, 363, fill='black')
self.canvas.create_oval(357, 357, 363, 363, fill='black')
def click(self, event):
if self.winner != 0:
return
x, y = event.x, event.y
if x < 50 - 15 or x > 470 + 15 or y < 50 - 15 or y > 470 + 15:
return
i, j = (x - 50) // 30, (y - 50) // 30
if self.board[i][j] != 0:
return
self.board[i][j] = self.current_player
self.draw_piece(i, j)
self.check_winner(i, j)
self.switch_player()
def draw_piece(self, i, j):
x, y = 50 + i * 30, 50 + j * 30
if self.current_player == 1:
self.canvas.create_oval(x - 12, y - 12, x + 12, y + 12, fill='black')
else:
self.canvas.create_oval(x - 12, y - 12, x + 12, y + 12, fill='white')
def check_winner(self, i, j):
directions = [(1, 0), (0, 1), (1, 1), (1, -1)]
for d in directions:
count = 1
x, y = i, j
while count < 5:
x, y = x + d[0], y + d[1]
if x < 0 or x >= self.size or y < 0 or y >= self.size or self.board[x][y] != self.current_player:
break
count += 1
x, y = i, j
while count < 5:
x, y = x - d[0], y - d[1]
if x < 0 or x >= self.size or y < 0 or y >= self.size or self.board[x][y] != self.current_player:
break
count += 1
if count >= 5:
self.winner = self.current_player
self.game_over()
def switch_player(self):
self.current_player = 3 - self.current_player
self.status_var.set('黑方先手' if self.current_player == 1 else '白方先手')
def game_over(self):
if self.winner == 1:
msg.showinfo('游戏结束', '黑方胜利!')
else:
msg.showinfo('游戏结束', '白方胜利!')
def restart(self):
self.canvas.delete('all')
self.board = [[0] * self.size for _ in range(self.size)]
self.current_player = 1
self.winner = 0
self.draw_board()
self.status_var.set('黑方先手')
def run(self):
self.root.mainloop()
if __name__ == '__main__':
game = Gobang()
game.run()
```
该游戏使用了 tkinter 模块,实现了一个字符界面的五子棋游戏。玩家可以使用鼠标来下棋,黑方先手。游戏结束后会弹出对话框提示胜利方。
您可以将该代码保存为 gobang.py,然后在命令行中执行 `python gobang.py` 来运行游戏。
阅读全文