用python写一个五子棋小游戏,尽量简单
时间: 2023-07-01 07:04:05 浏览: 93
好的,让我们来写一个简单的五子棋小游戏。
首先,我们需要导入 `tkinter` 库来创建图形界面,以及一些必要的变量和函数。
```python
import tkinter as tk
# 棋盘大小
BOARD_SIZE = 500
# 棋盘行列数
ROW_COUNT = 15
COL_COUNT = 15
# 棋子大小和颜色
PIECE_SIZE = 20
BLACK_COLOR = "#000000"
WHITE_COLOR = "#ffffff"
# 棋盘数组,0 表示空,1 表示黑子,2 表示白子
board = [[0] * COL_COUNT for _ in range(ROW_COUNT)]
# 当前下棋方,1 表示黑方,2 表示白方
current_player = 1
# 判断是否结束游戏的标志
game_over = False
# 记录赢法的数组
wins = [[[0, 0] for _ in range(5)] for _ in range(ROW_COUNT * COL_COUNT)]
# 记录赢法的总数
count = 0
# 初始化所有横向赢法
for i in range(ROW_COUNT):
for j in range(COL_COUNT - 4):
for k in range(5):
wins[count][k] = [i, j + k]
count += 1
# 初始化所有纵向赢法
for i in range(COL_COUNT):
for j in range(ROW_COUNT - 4):
for k in range(5):
wins[count][k] = [j + k, i]
count += 1
# 初始化所有斜向赢法(正斜线)
for i in range(ROW_COUNT - 4):
for j in range(COL_COUNT - 4):
for k in range(5):
wins[count][k] = [i + k, j + k]
count += 1
# 初始化所有斜向赢法(反斜线)
for i in range(4, ROW_COUNT):
for j in range(COL_COUNT - 4):
for k in range(5):
wins[count][k] = [i - k, j + k]
count += 1
# 初始化胜利的情况
black_win = [False] * count
white_win = [False] * count
# 创建主窗口
root = tk.Tk()
root.title("五子棋")
# 创建画布
canvas = tk.Canvas(root, width=BOARD_SIZE, height=BOARD_SIZE)
canvas.pack()
```
接下来,我们需要定义一些函数来实现游戏的逻辑。
```python
def draw_board():
"""
绘制棋盘。
"""
for i in range(ROW_COUNT):
canvas.create_line(PIECE_SIZE, PIECE_SIZE * (i + 1), BOARD_SIZE - PIECE_SIZE, PIECE_SIZE * (i + 1))
for i in range(COL_COUNT):
canvas.create_line(PIECE_SIZE * (i + 1), PIECE_SIZE, PIECE_SIZE * (i + 1), BOARD_SIZE - PIECE_SIZE)
def draw_piece(row, col, color):
"""
绘制棋子。
"""
x = PIECE_SIZE * (col + 1)
y = PIECE_SIZE * (row + 1)
canvas.create_oval(x - PIECE_SIZE, y - PIECE_SIZE, x + PIECE_SIZE, y + PIECE_SIZE, fill=color)
def check_win(player):
"""
检查当前玩家是否胜利。
"""
global black_win, white_win
for i in range(count):
if player == 1:
if black_win[i]:
continue
if all(board[w[0]][w[1]] == 1 for w in wins[i]):
black_win[i] = True
return True
else:
if white_win[i]:
continue
if all(board[w[0]][w[1]] == 2 for w in wins[i]):
white_win[i] = True
return True
return False
def place_piece(event):
"""
下棋。
"""
global current_player, game_over
if game_over:
return
row = int((event.y - PIECE_SIZE) / PIECE_SIZE)
col = int((event.x - PIECE_SIZE) / PIECE_SIZE)
if row < 0 or row >= ROW_COUNT or col < 0 or col >= COL_COUNT:
return
if board[row][col] != 0:
return
draw_piece(row, col, BLACK_COLOR if current_player == 1 else WHITE_COLOR)
board[row][col] = current_player
if check_win(current_player):
game_over = True
message = "黑方胜利!" if current_player == 1 else "白方胜利!"
canvas.create_text(BOARD_SIZE / 2, BOARD_SIZE / 2, text=message, font=("Arial", 32), fill="red")
return
current_player = 2 if current_player == 1 else 1
if current_player == 2:
ai_place_piece()
def ai_place_piece():
"""
AI 下棋。
"""
global current_player, game_over
if game_over:
return
# 先检查是否有必胜的情况
for i in range(count):
if white_win[i]:
continue
pieces = [(w[0], w[1]) for w in wins[i] if board[w[0]][w[1]] == 2]
if len(pieces) == 4:
for w in wins[i]:
if board[w[0]][w[1]] == 0:
row, col = w
draw_piece(row, col, WHITE_COLOR)
board[row][col] = 2
if check_win(2):
game_over = True
message = "白方胜利!"
canvas.create_text(BOARD_SIZE / 2, BOARD_SIZE / 2, text=message, font=("Arial", 32), fill="red")
current_player = 1
return
# 再检查是否需要堵住黑方的必胜情况
for i in range(count):
if black_win[i]:
continue
pieces = [(w[0], w[1]) for w in wins[i] if board[w[0]][w[1]] == 1]
if len(pieces) == 4:
for w in wins[i]:
if board[w[0]][w[1]] == 0:
row, col = w
draw_piece(row, col, WHITE_COLOR)
board[row][col] = 2
current_player = 1
return
# 如果没有必胜情况,则随机下棋
while True:
row = random.randint(0, ROW_COUNT - 1)
col = random.randint(0, COL_COUNT - 1)
if board[row][col] == 0:
draw_piece(row, col, WHITE_COLOR)
board[row][col] = 2
if check_win(2):
game_over = True
message = "白方胜利!"
canvas.create_text(BOARD_SIZE / 2, BOARD_SIZE / 2, text=message, font=("Arial", 32), fill="red")
current_player = 1
return
```
最后,我们将绘制棋盘并绑定点击事件。
```python
draw_board()
canvas.bind("<Button-1>", place_piece)
root.mainloop()
```
这样,我们就完成了五子棋小游戏的编写。完整代码如下:
阅读全文