使用python语言编写一个五子棋小游戏,包括双人对战和人机对战(可以手动选择),自己绘制棋盘和棋子(棋子只能是黑白两色,棋盘位网格,棋子需落在网格交线处),只通过是否成功连成五个棋子进行判断输赢
时间: 2023-07-16 22:13:15 浏览: 86
好的,让我们开始编写五子棋小游戏吧!
首先,我们需要安装一些库。在命令行中输入以下命令来安装:
```
pip install pygame
```
接下来,我们需要创建一个 Pygame 窗口,绘制五子棋棋盘和棋子。我们可以使用 Pygame 的 `pygame.draw` 模块来绘制。
```python
import pygame
# 初始化 Pygame
pygame.init()
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (128, 128, 128)
# 定义棋盘网格大小
GRID_SIZE = 50
# 定义棋盘大小
BOARD_SIZE = (15 * GRID_SIZE, 15 * GRID_SIZE)
# 创建 Pygame 窗口
screen = pygame.display.set_mode(BOARD_SIZE)
# 设置窗口标题
pygame.display.set_caption("五子棋")
# 绘制棋盘网格
for i in range(15):
pygame.draw.line(screen, GRAY, (GRID_SIZE, (i + 1) * GRID_SIZE), ((15 - 1) * GRID_SIZE, (i + 1) * GRID_SIZE))
pygame.draw.line(screen, GRAY, ((i + 1) * GRID_SIZE, GRID_SIZE), ((i + 1) * GRID_SIZE, (15 - 1) * GRID_SIZE))
# 实现事件循环
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
# 退出游戏
pygame.quit()
sys.exit()
# 刷新窗口
pygame.display.update()
```
现在,我们已经成功绘制了一个棋盘网格。接下来,我们需要在网格交叉点处绘制棋子。我们可以使用一个二维数组来表示棋盘,0 表示空位,1 表示黑子,2 表示白子。
```python
import pygame
import sys
# 初始化 Pygame
pygame.init()
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (128, 128, 128)
# 定义棋盘网格大小
GRID_SIZE = 50
# 定义棋盘大小
BOARD_SIZE = (15 * GRID_SIZE, 15 * GRID_SIZE)
# 创建 Pygame 窗口
screen = pygame.display.set_mode(BOARD_SIZE)
# 设置窗口标题
pygame.display.set_caption("五子棋")
# 初始化棋盘
board = [[0 for x in range(15)] for y in range(15)]
# 绘制棋盘网格
def draw_grid():
for i in range(15):
pygame.draw.line(screen, GRAY, (GRID_SIZE, (i + 1) * GRID_SIZE), ((15 - 1) * GRID_SIZE, (i + 1) * GRID_SIZE))
pygame.draw.line(screen, GRAY, ((i + 1) * GRID_SIZE, GRID_SIZE), ((i + 1) * GRID_SIZE, (15 - 1) * GRID_SIZE))
# 绘制棋子
def draw_piece(x, y, color):
if color == 1:
pygame.draw.circle(screen, BLACK, ((x + 1) * GRID_SIZE, (y + 1) * GRID_SIZE), GRID_SIZE // 2 - 2)
elif color == 2:
pygame.draw.circle(screen, WHITE, ((x + 1) * GRID_SIZE, (y + 1) * GRID_SIZE), GRID_SIZE // 2 - 2)
# 实现事件循环
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
# 退出游戏
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
# 获取鼠标点击位置
x, y = event.pos
# 计算棋子在棋盘上的位置
row = y // GRID_SIZE - 1
col = x // GRID_SIZE - 1
# 落子
board[row][col] = 1
# 绘制棋盘
draw_grid()
# 绘制棋子
for i in range(15):
for j in range(15):
if board[i][j] == 1:
draw_piece(j, i, 1)
elif board[i][j] == 2:
draw_piece(j, i, 2)
# 刷新窗口
pygame.display.update()
```
现在,我们已经可以在棋盘上绘制黑子了。接下来,我们需要实现一个简单的 AI,使得人机对战成为可能。
对于 AI,我们可以使用一个简单的算法:
1. 如果 AI 可以在下一步落子连成五子,则落子;
2. 如果玩家可以在下一步落子连成五子,则阻止玩家;
3. 如果 AI 可以在下一步落子连成四子,则落子;
4. 如果玩家可以在下一步落子连成四子,则阻止玩家;
5. 如果 AI 可以在下一步落子连成三子,则落子;
6. 如果玩家可以在下一步落子连成三子,则阻止玩家;
7. 如果 AI 可以在下一步落子连成两个活三,则落子;
8. 如果玩家可以在下一步落子连成两个活三,则阻止玩家;
9. 如果 AI 可以在下一步落子连成两个半活三,则落子;
10. 随机落子。
以下是实现代码:
```python
import pygame
import sys
import random
# 初始化 Pygame
pygame.init()
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (128, 128, 128)
# 定义棋盘网格大小
GRID_SIZE = 50
# 定义棋盘大小
BOARD_SIZE = (15 * GRID_SIZE, 15 * GRID_SIZE)
# 创建 Pygame 窗口
screen = pygame.display.set_mode(BOARD_SIZE)
# 设置窗口标题
pygame.display.set_caption("五子棋")
# 初始化棋盘
board = [[0 for x in range(15)] for y in range(15)]
# 绘制棋盘网格
def draw_grid():
for i in range(15):
pygame.draw.line(screen, GRAY, (GRID_SIZE, (i + 1) * GRID_SIZE), ((15 - 1) * GRID_SIZE, (i + 1) * GRID_SIZE))
pygame.draw.line(screen, GRAY, ((i + 1) * GRID_SIZE, GRID_SIZE), ((i + 1) * GRID_SIZE, (15 - 1) * GRID_SIZE))
# 绘制棋子
def draw_piece(x, y, color):
if color == 1:
pygame.draw.circle(screen, BLACK, ((x + 1) * GRID_SIZE, (y + 1) * GRID_SIZE), GRID_SIZE // 2 - 2)
elif color == 2:
pygame.draw.circle(screen, WHITE, ((x + 1) * GRID_SIZE, (y + 1) * GRID_SIZE), GRID_SIZE // 2 - 2)
# 判断是否连成五子
def is_win(board, color):
for i in range(15):
for j in range(15):
if board[i][j] == color:
# 判断横向是否连成五子
if j <= 10 and board[i][j+1] == color and board[i][j+2] == color and board[i][j+3] == color and board[i][j+4] == color:
return True
# 判断纵向是否连成五子
if i <= 10 and board[i+1][j] == color and board[i+2][j] == color and board[i+3][j] == color and board[i+4][j] == color:
return True
# 判断左上到右下是否连成五子
if i <= 10 and j <= 10 and board[i+1][j+1] == color and board[i+2][j+2] == color and board[i+3][j+3] == color and board[i+4][j+4] == color:
return True
# 判断右上到左下是否连成五子
if i <= 10 and j >= 4 and board[i+1][j-1] == color and board[i+2][j-2] == color and board[i+3][j-3] == color and board[i+4][j-4] == color:
return True
return False
# 判断是否连成四子
def is_four(board, color):
for i in range(15):
for j in range(15):
if board[i][j] == 0:
# 判断横向是否连成四子
if j <= 10 and board[i][j+1] == color and board[i][j+2] == color and board[i][j+3] == color and board[i][j+4] == 0 and j >= 1 and board[i][j-1] == 0:
return (i, j+4)
if j >= 4 and board[i][j-1] == color and board[i][j-2] == color and board[i][j-3] == color and board[i][j-4] == 0 and j <= 13 and board[i][j+1] == 0:
return (i, j-4)
# 判断纵向是否连成四子
if i <= 10 and board[i+1][j] == color and board[i+2][j] == color and board[i+3][j] == color and board[i+4][j] == 0 and i >= 1 and board[i-1][j] == 0:
return (i+4, j)
if i >= 4 and board[i-1][j] == color and board[i-2][j] == color and board[i-3][j] == color and board[i-4][j] == 0 and i <= 13 and board[i+1][j] == 0:
return (i-4, j)
# 判断左上到右下是否连成四子
if i <= 10 and j <= 10 and board[i+1][j+1] == color and board[i+2][j+2] == color and board[i+3][j+3] == color and board[i+4][j+4] == 0 and i >= 1 and j >= 1 and board[i-1][j-1] == 0:
return (i+4, j+4)
if i >= 4 and j >= 4 and board[i-1][j-1] == color and board[i-2][j-2] == color and board[i-3][j-3] == color and board[i-4][j-4] == 0 and i <= 13 and j <= 13 and board[i+1][j+1] == 0:
return (i-4, j-4)
# 判断右上到左下是否连成四子
if i <= 10 and j >= 4 and board[i+1][j-1] == color and board[i+2][j-2] == color and board[i+3][j-3] == color and board[i+4][j-4] == 0 and i >= 1 and j <= 13 and board[i-1][j+1] == 0:
return (i+4, j-4)
if i >= 4 and j <= 10 and board[i-1][j+1] == color and board[i-2][j+2] == color and board[i-3][j+3] == color and board[i-4][j+4] == 0 and i <= 13 and j >= 1 and board[i+1][j-1] == 0:
return (i-4, j+4)
return None
# 判断是否连成三子
def is_three(board, color):
for i in range(15):
for j in range(15):
if board[i][j] == 0:
# 判断横向是否连成三子
if j <= 10 and board[i][j+1] == color and board[i][j+2] == color and board[i][j+3] == 0 and j >= 1 and board[i][j-1] == 0:
return (i, j+3)
if j >= 3 and board[i][j-1] == color and board[i][j-2] == color and board[i][j-3] == 0 and j <= 12 and board[i][j+1] == 0:
return (i, j-3)
# 判断纵向是否连成三子
if i <= 10 and board[i+1][j] == color and board[i+2][j] == color and board[i+3][j] == 0 and i >= 1 and board[i-1][j] == 0:
return (i+3, j)
if i >= 3 and board[i-1][j] == color and board[i-2][j] == color and board[i-3][j] == 0 and i <= 12 and board[i+1][j] == 0:
return (i-3, j)
# 判断左上到右下是否连成三子
if i <= 10 and j <= 10 and board[i+1][j+1] == color and board[i+2][j+2] == color and board[i+3][j+3] == 0 and i >= 1 and j >= 1 and board[i-1][j-1] == 0:
return (i+3, j+3)
if i >= 3 and j >= 3 and board[i-1][j-1] == color and board[i-2][j-2] == color and board[i-3][j-3] == 0 and i <= 12 and j <= 12 and board[i+1][j+1] == 0:
return (i-3, j-3)
# 判断右上到左下是否连成三子
if i <= 10 and j >= 4 and board[i+1][j-1] == color and board[i+2][j-2] == color and board[i+3][j-3] == 0 and i >= 1 and j <= 13 and board[i-1][j+1] == 0:
return (i+3, j-3)
if i >= 3 and j <= 10 and board[i-1][j+1] == color and board[i-2][j+2] == color and board[i-3][j+3] == 0 and i <= 12 and j >= 1 and board[i+1][j-1] == 0:
return (i-3, j+3)
return None
# 判断是否连成两个活三
def is_two_live_three(board, color):
for i in range(15):
for j in range(15):
if board[i][j] == 0:
# 判断横向是否连成两个活三
if j <= 9 and board[i][j+1] == color and board[i][j+2] == 0 and board[i][j+3] == color and board[i][j+4] == color and j >= 2 and board[i][j-1] == 0 and board[i][j-2] == color and board[i][j-3] == color and board[i][j-4] == 0:
return (i, j+2)
if j >= 5 and board[i][j-1] == color and board[i][j-2] == color and board[i][j-3] == 0 and board[i][j-4] == color and j <= 12 and board[i][j+1] == 0 and board[i][j+2] == color and board[i][j+3] == color and board[i][j+4] == 0:
return (i, j-3)
# 判断纵向是否连成两个活三
if i <= 9 and board[i+1][j] == color and board[i+2][j] == 0 and board[i+3][j] == color and board[i+4][j] == color and i >= 2 and board[i-1][j] == 0 and board[i-2][j] == color and board[i-3][j] == color and board[i-4][j] == 0
阅读全文