用python生成一个五子棋游戏
时间: 2024-05-10 14:20:46 浏览: 12
以下是一个简单的五子棋游戏,使用Python编写:
```
import tkinter as tk
class Game:
def __init__(self):
self.board_size = 15
self.board = [[0 for _ in range(self.board_size)] for _ in range(self.board_size)]
self.current_player = 1
self.winner = None
def move(self, row, col):
if self.board[row][col] != 0:
return False
self.board[row][col] = self.current_player
if self.check_win(row, col):
self.winner = self.current_player
self.current_player = 3 - self.current_player
return True
def check_win(self, row, col):
directions = [(0, 1), (1, 0), (1, 1), (-1, 1)]
for dx, dy in directions:
count = 1
r, c = row + dx, col + dy
while r >= 0 and r < self.board_size and c >= 0 and c < self.board_size and self.board[r][c] == self.current_player:
count += 1
r += dx
c += dy
r, c = row - dx, col - dy
while r >= 0 and r < self.board_size and c >= 0 and c < self.board_size and self.board[r][c] == self.current_player:
count += 1
r -= dx
c -= dy
if count >= 5:
return True
return False
class GUI:
def __init__(self, game):
self.game = game
self.window = tk.Tk()
self.window.title("五子棋")
self.canvas = tk.Canvas(self.window, width=500, height=500)
self.canvas.pack()
self.draw_board()
self.canvas.bind("<Button-1>", self.on_click)
self.window.mainloop()
def draw_board(self):
for i in range(self.game.board_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)
def draw_piece(self, row, col):
x = 50 + col * 30
y = 50 + row * 30
if self.game.current_player == 1:
self.canvas.create_oval(x - 13, y - 13, x + 13, y + 13, fill="black")
else:
self.canvas.create_oval(x - 13, y - 13, x + 13, y + 13, fill="white")
def on_click(self, event):
if self.game.winner is not None:
return
row = (event.y - 50) // 30
col = (event.x - 50) // 30
if row < 0 or row >= self.game.board_size or col < 0 or col >= self.game.board_size:
return
if not self.game.move(row, col):
return
self.draw_piece(row, col)
if self.game.winner is not None:
self.canvas.create_text(250, 250, text="Player {} wins!".format(self.game.winner), font=("Arial", 30))
else:
self.canvas.create_text(250, 500, text="Player {}'s turn".format(self.game.current_player), font=("Arial", 20))
game = Game()
gui = GUI(game)
```
运行以上代码,将会打开一个窗口,显示五子棋棋盘。玩家可以通过点击棋盘上的交叉点来下棋。当有一方取得五子连珠时,会在窗口中显示胜利信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)