五子棋是一种二人对弈游戏,其中黑白两方各执所属的棋子在棋盘上交替落子,先在横向、纵向或斜向连成五子者获胜。本文将介绍如何使用Python开发一个简单的五子棋小程序。
一、棋盘与棋子
我们先要构建一个棋盘界面,使用Python的pygame模块可以方便地实现。以下是实现棋盘的代码:
```python
import pygame
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
pygame.init()
# 设置棋盘尺寸
size = (600, 600)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("五子棋")
# 设置格子尺寸和行数
block_size = 30
block_count = 15
start_xy = 30
# 绘制棋盘
for x in range(block_count):
for y in range(block_count):
rect = pygame.Rect(start_xy + x * block_size - block_size / 2,
start_xy + y * block_size - block_size / 2,
block_size, block_size)
pygame.draw.rect(screen, BLACK, rect, 2)
pygame.draw.circle(screen, BLACK, (start_xy + x * block_size,
start_xy + y * block_size), 4)
```
以上代码中,我们设置了棋盘的尺寸、格子尺寸和行数,并使用for循环绘制每一个格子和棋盘边缘的圆点。接下来,我们需要实现棋子的绘制和落子操作。
```python
def draw_chess(x, y, color):
"""
:param x: 棋子x坐标
:param y: 棋子y坐标
:param color: 棋子颜色(1为黑,-1为白)
"""
if color == 1:
pygame.draw.circle(screen, BLACK, (start_xy + x * block_size, start_xy + y * block_size), 15)
else:
pygame.draw.circle(screen, WHITE, (start_xy + x * block_size, start_xy + y * block_size), 15)
is_black = True
chess_map = [[0] * block_count for i in range(block_count)]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos()
click_x = (mouse_pos[0] - start_xy) // block_size
click_y = (mouse_pos[1] - start_xy) // block_size
if chess_map[click_y][click_x] == 0:
# 绘制棋子
if is_black:
draw_chess(click_x, click_y, 1)
chess_map[click_y][click_x] = 1
else:
draw_chess(click_x, click_y, -1)
chess_map[click_y][click_x] = -1
is_black = not is_black
```
以上代码实现了根据鼠标点击位置在棋盘上落子并绘制棋子,然后根据当前下棋者切换黑白棋子颜色。chess_map是棋盘的状态数组,值为0的位置表示当前没有子,1表示黑子,-1表示白子。
二、胜负判断
五子棋游戏的核心逻辑就是胜负判断。在游戏中,每次落子后需要判断当前下棋方是否胜利,如果胜利则游戏结束。
```python
def check_win(chess_map, x, y, color):
"""
:param chess_map: 棋盘状态数组
:param x: 当前落子的x坐标
:param y: 当前落子的y坐标
:param color: 当前落子的颜色(1为黑,-1为白)
:return: 是否胜利
"""
def count(n):
if n < 5:
return False
for i in range(n - 4):
arr = chess_map[y][i:i + 5]
if arr.count(color) == 5:
return True
for i in range(n - 4):
arr = [chess_map[j][i] for j in range(n)]
arr = arr[i:i + 5]
if arr.count(color) == 5:
return True
for i in range(n - 4):
arr = [chess_map[i + k][j + k] for k in range(min(n - i, n - j))]
arr = arr[i:i + 5]
if arr.count(color) == 5:
return True
for i in range(n - 4):
arr = [chess_map[i + k][j - k] for k in range(min(n - i, j + 1))]
arr = arr[i:i + 5]
if arr.count(color) == 5:
return True
return False
return count(block_count) or count(block_count)
# 胜负状态
WIN_NONE = 0
WIN_BLACK = 1
WIN_WHITE = 2
def check_gameover(chess_map, x, y, color):
"""
:param chess_map: 棋盘状态数组
:param x: 当前落子的x坐标
:param y: 当前落子的y坐标
:param color: 当前落子的颜色(1为黑,-1为白)
:return: 是否游戏结束,胜负状态
"""
if check_win(chess_map, x, y, color):
return True, WIN_BLACK if color == 1 else WIN_WHITE
for i in range(block_count):
if 0 in chess_map[i]:
return False, WIN_NONE
return True, WIN_NONE
```
以上代码实现了一个胜负判断函数check_win,它通过判断当前落子位置横向、纵向和两个斜向是否有五个同色的棋子,来决定胜负状态。接下来,我们调用check_win来判断游戏是否结束:
```python
# 胜负状态
WIN_NONE = 0
WIN_BLACK = 1
WIN_WHITE = 2
def check_gameover(chess_map, x, y, color):
"""
:param chess_map: 棋盘状态数组
:param x: 当前落子的x坐标
:param y: 当前落子的y坐标
:param color: 当前落子的颜色(1为黑,-1为白)
:return: 是否游戏结束,胜负状态
"""
if check_win(chess_map, x, y, color):
return True, WIN_BLACK if color == 1 else WIN_WHITE
for i in range(block_count):
if 0 in chess_map[i]:
return False, WIN_NONE
return True, WIN_NONE
is_black = True
chess_map = [[0] * block_count for i in range(block_count)]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos()
click_x = (mouse_pos[0] - start_xy) // block_size
click_y = (mouse_pos[1] - start_xy) // block_size
if chess_map[click_y][click_x] == 0:
# 绘制棋子
if is_black:
draw_chess(click_x, click_y, 1)
chess_map[click_y][click_x] = 1
else:
draw_chess(click_x, click_y, -1)
chess_map[click_y][click_x] = -1
# 判断游戏是否结束
is_gameover, win = check_gameover(chess_map, click_x, click_y, 1 if is_black else -1)
if is_gameover:
if win == WIN_BLACK:
print("黑方胜利!")
elif win == WIN_WHITE:
print("白方胜利!")
else:
print("平局!")
else:
is_black = not is_black
```
以上代码中,我们调用check_win判断当前局面是否已经胜利,如果胜利则输出胜方信息,否则根据落子方切换下一步。
三、完整代码
以上是实现五子棋小程序的核心代码,完整代码如下: