GPT生成五子棋
用python编写五子棋游戏要有游戏界面游戏规则如下
1.五子棋棋盘为 15x15的方格棋盘,两人轮流在空位上落子先连成一线(即五个同色棋子相邻) 者获胜。
2.先手执黑,后手执白,轮流下子
3.黑方先走,每次只能下一子,黑白双方的子不能落在已经有
子的点上。
4.棋子下在棋盘格点的交叉点上
5.如果一方的棋子形成了一个和棋(即不论如何都无法形成五子连珠),则游戏结束,该局为平局。
6.当一个玩家的棋子连成五个时,即为胜利,游戏结束。
7.如果双方都未形成五子连珠,而棋盘已满,则游戏结束,该局为平局。
8.游戏结束提示其中一方胜利,点击确定游戏重新开始.
源码:
import pygame
import sys
from pygame.locals import *
# 初始化游戏
pygame.init()
# 设置游戏界面大小、背景图片及标题
width, height = 600, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('五子棋')
# 定义颜色
beige = (245, 245, 220)
black = (0, 0, 0)
# 创建一个 Surface 对象来作为背景
background = pygame.Surface(screen.get_size())
background.fill(beige)
# 绘制网格
for x in range(0, width, 40):
pygame.draw.line(background, black, (x, 0), (x, height))
for y in range(0, height, 40):
pygame.draw.line(background, black, (0, y), (width, y))
# 棋盘坐标系
x1, y1 = 40, 40
x2, y2 = 540, 540
# 用字典存储棋子的位置,颜色以及个数
pieces = {'black':[], 'white':[]}
piece_num = {'black':0, 'white':0}
# 标志变量,用于判断当前轮到哪一方下棋
flag = 'black'
# 判断是否有一方获胜的函数
def isWin(list):
# 判断水平方向是否有五个相连
for i in range(15):
for j in range(11):
if (i, j) in list and (i, j + 1) in list and (i, j + 2) in list and (i, j + 3) in list and (i, j + 4) in list:
return True
# 判断垂直方向是否有五个相连
for i in range(15):
for j in range(11):
if (j, i) in list and (j + 1, i) in list and (j + 2, i) in list and (j + 3, i) in list and (j + 4, i) in list:
return True
# 判断正斜方向是否有五个相连
for i in range(11):
for j in range(11):
if (i, j) in list and (i + 1, j + 1) in list and (i + 2, j + 2) in list and (i + 3, j + 3) in list and (i + 4, j + 4) in list:
return True
# 判断反斜方向是否有五个相连
for i in range(11):
for j in range(14, 3, -1):
if (i, j) in list and (i + 1, j - 1) in list and (i + 2, j - 2) in list and (i + 3, j - 3) in list and (i + 4, j - 4) in list:
return True
return False
# 游戏主循环
while True:
# 绘制棋盘
screen.blit(background, (0, 0))
# 绘制文字提示信息
font = pygame.font.Font(None, 24)
text = font.render('请黑方下棋', True, (0, 0, 0))
screen.blit(text, (250, 590))
# 遍历所有事件
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
# 鼠标点击事件
elif event.type == MOUSEBUTTONDOWN and x1 < event.pos[0] < x2 and y1 < event.pos[1] < y2:
# 计算鼠标点击处的棋盘坐标
x = int((event.pos[0] - x1) / 40)
y = int((event.pos[1] - y1) / 40)
# 如果该点没有棋子,则在该点上落子
if (x, y) not in pieces['black'] and (x, y) not in pieces['white']:
pieces[flag].append((x, y))
# 判断是否有一方获胜
if isWin(pieces[flag]):
font = pygame.font.Font(None, 48)
text = font.render(flag + '方获胜!', True, (255, 0, 0))
screen.blit(text, (200, 300))
pygame.display.update()
# 等待3秒后,重新开始游戏
pygame.time.delay(3000)
flag = 'black'
pieces = {'black':[], 'white':[]}
piece_num = {'black':0, 'white':0}
continue
# 如果棋盘已满,则游戏平局
elif len(pieces['black']) + len(pieces['white']) == 15 * 15:
font = pygame.font.Font(None, 48)
text = font.render('平局!', True, (255, 0, 0))
screen.blit(text, (250, 300))
pygame.display.update()
# 等待3秒后,重新开始游戏
pygame.time.delay(3000)
flag = 'black'
pieces = {'black':[], 'white':[]}
piece_num = {'black':0, 'white':0}
continue
# 更改标志变量,改变落子方
if flag == 'black':
flag = 'white'
else:
flag = 'black'
# 遍历字典中的棋子,绘制所有棋子
for i in pieces['black']:
pygame.draw.circle(screen, (10, 10, 10), (int(x1 + 40 * i[0]), int(y1 + 40 * i[1])), 15)
for i in pieces['white']:
pygame.draw.circle(screen, (255, 255, 255), (int(x1 + 40 * i[0]), int(y1 + 40 * i[1])), 15)
# 更新棋盘
pygame.display.update()