作为经典的棋类游戏,五子棋深受大众喜爱,但如果仅实现人与人的博弈,那程序很简单,如果要实现人机对战,教会计算机如何战胜人类,那就不是十分容易的事了。本文我们先从简单入手,完成五子棋游戏的基本操作,然后再尝试引入一些实现人工智能的编程方法和技巧,从而实现人机博弈。
1.创建棋盘和棋子
对于棋类游戏来说,游戏的场景就是下棋的棋盘,而游戏的主要角色就是棋子。接下来我们开始创建棋盘和棋子。
1.1绘制棋盘
五子棋的棋盘可以看成是一个1818的网格,他由19条竖线和19条横线(包含边界)构成,直线的交叉处就下棋子的位置。这里,我们用1818个棕色格子来填满棋盘,每个格子的间距就是棋盘的纵横交错的线条,python可以实现该功能的库有pgzero、tkinter。这里我们用pygame
# 创建窗口,背景为棕色screen = pygame.display.set_mode((615, 615))pygame.display.set_caption('五子棋')screen.fill("#DD954F")
#创建外边框a = pygame.Surface((603, 603), flags=pygame.HWSURFACE)a.fill(color='#121010')b = pygame.Surface((585, 585), flags=pygame.HWSURFACE)b.fill(color="#DD954F")c = pygame.Surface((579, 579), flags=pygame.HWSURFACE)c.fill(color='#121010')## d = pygame.Surface((576, 576), flags=pygame.HWSURFACE)# d.fill(color="#DD954F")#棋盘格子e = pygame.Surface((31, 31), flags=pygame.HWSURFACE)e.fill(color="#DD954F")screen.blit(a, (6.5, 6.5))screen.blit(b, (15, 15))screen.blit(c, (18, 18))for j in range(18):for i in range(18):#起点是20,间隔是32,每个格子大小31,所以格子间距1screen.blit(e, (20 + 32 * i, 20 + 32 * j))#存储棋盘状态alist = np.zeros((19, 19))#星位pygame.draw.circle(screen, '#121010', [307.5, 307.5], 5)pygame.draw.circle(screen, '#121010', [115.5, 307.5], 5)pygame.draw.circle(screen, '#121010', [499.5, 307.5], 5)pygame.draw.circle(screen, '#121010', [115.5, 499.5], 5)pygame.draw.circle(screen, '#121010', [499.5, 499.5], 5)pygame.draw.circle(screen, '#121010', [115.5, 115.5], 5)pygame.draw.circle(screen, '#121010', [499.5, 115.5], 5)pygame.draw.circle(screen, '#121010', [307.5, 499.5], 5)pygame.draw.circle(screen, '#121010', [307.5, 115.5], 5)
#刷新窗口pygame.display.flip()
1.2创建棋子
本来打算直接上图片,后来参考了别的博主的做法,其实可以通过多个半径颜色不同的同心圆叠加来绘制圆滑的棋子
def black(x, y):a = 20b =20c =20d = 0.01#循环50次,每次绘制50个半径颜色不同的同心圆for i in range(50):pygame.draw.circle(screen, (a, b, c), [19.5 + 32 * x, 19.5 + 32 * y], (16/(d-5)+16))a += 1b += 1c += 1d += 0.08pygame.display.update()def white(x, y):a = 170b = 170c = 170d = 0.02for i in range(50):pygame.draw.circle(screen, (a, b, c), [19.5 + 32 * x, 19.5 + 32 * y],(16/(d-5)+16))a += 1b += 1c += 1d += 0.08pygame.display.update()
2.完成走棋操作
棋盘和棋子都准备好了,下面来实现下棋的功能,看看如何把棋子摆放在棋盘上。
五子棋的规则如下
:
1.对局双方各执一色棋子。 空棋盘开局。 黑先、白后,交替下子,每次只能下一子。
2.棋子下在棋盘的空白点上,棋子下定后,不得向其它点移动,不得从棋盘上拿掉或拿起另落别处。
3.五颗连续的同色棋子先连城一条线的那方赢。
人可以通过眼睛来判断那个位置能下棋和何时输赢,那计算机怎么判断这些呢。计算不像人那样有聪明的大脑,想要让计算机能做出这些判断,就必须把这些操作转换成计算机能理解的数据。
怎么让计算机知道棋盘的情况呢?我们用一个二维数组来模拟棋盘,棋盘上每个位置都有三种状态(无子、黑子、白子)那我们就用(0、1、2)来表示这三种情况。用wb来表示当前棋子,如果该位置为0则,更新该位置的值和wb的值,然后调用对用绘画函数来更新窗口
#存储棋盘状态alist = np.zeros((19, 19))if event.type == pygame.MOUSEBUTTONDOWN:x, y = pygame.mouse.get_pos()# 棋盘边界线的中点是19.5, 通过计算得到当前坐标在棋盘的行号和列号(x,y)x = round((x - 19.5) / 32)y = round((y - 19.5) / 32)if x < 0:x = 0if x > 18:x = 18if y < 0:y = 0if y > 18:y = 18z = Falseif alist[x][y] == 0:#这步相当于black(x,y)或者是white(x,y)取决于wbeval(wb + "({},{})".format(x, y))
3.判断输赢
怎么判断赢了没?我们通过鼠标左机屏幕来下棋子,pygame也给我们提供了获取鼠标坐标的函数,每次鼠标点击后就进行check()操做
分别检查四个方向上是否有连子的情况,先找到该方向的第一个棋子然后向后统计同色数量,判断输赢。
def check(x,y):xx = xyy = ywhile True:#从最上边的棋子开始检查,记录颜色相同得棋子数量#先找到最同一条线上最上边的同色棋子if xx == 0:breakelif alist[xx][yy] != alist[x][y]:xx += 1breakelse:xx -= 1num = 0while True:if xx == 18:breakelif alist[xx][yy] != alist[x][y]:breakelse:xx += 1num += 1if num >= 5:win(wb1)# 从最边的棋子开始检查,记录颜色相同得棋子数量# 先找到最同一条线上最左边的同色棋子xx = xyy = ywhile True:if yy == 0:breakelif alist[xx][yy] != alist[x][y]:yy += 1breakelse:yy -= 1num = 0while True:if yy == 18:breakelif alist[xx][yy] != alist[x][y]:breakelse:yy += 1num += 1if num >= 5:win(wb1)# 从左上方的棋子开始检查,记录颜色相同得棋子数量# 先找到最同一条线上左上方的同色棋子xx = xyy = ywhile True:if xx == 0:breakelif yy == 0:breakelif alist[xx][yy] != alist[x][y]:xx += 1yy += 1breakelse:xx -= 1yy -= 1num = 0while True:if xx == 18:breakelif yy == 18:breakelif alist[xx][yy] != alist[x][y]:breakelse:xx += 1yy += 1num += 1if num >= 5:win(wb1)# 从右上方的棋子开始检查,记录颜色相同得棋子数量# 先找到最同一条线上右上方的同色棋子xx = xyy = ywhile True:if xx == 0:breakelif yy == 18:breakelif alist[xx][yy] != alist[x][y]:xx += 1yy -= 1breakelse:xx -= 1yy += 1num = 0while True:if xx == 18:breakelif yy == 0:breakelif alist[xx][yy] != alist[x][y]:breakelse:xx += 1yy -= 1num += 1if num >= 5:pygame.font.init()win(wb1)def win(wb1):font = pygame.font.Font(None, 36)text = font.render("{}赢了".format(wb1), True, (255, 255, 255))screen.blit(text, ((665 - text.get_width()) / 2, (665 - text.get_height()) / 2))pygame.display.update()while True:for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()if event.type == pygame.MOUSEBUTTONDOWN:do()
完成了这些步骤后,五子棋的基本规则已经实现,敲了几十行代码,终于能得到点反馈了。
下面是初级版的完整代码
import random
import sys
import pygameimport numpy as npdef init():pygame.init()# 创建窗口,背景为棕色global screenscreen = pygame.display.set_mode((615, 615))pygame.display.set_caption('五子棋')screen.fill("#DD954F")# 创建外边框a = pygame.Surface((603, 603), flags=pygame.HWSURFACE)a.fill(color='#121010')b = pygame.Surface((585, 585), flags=pygame.HWSURFACE)b.fill(color="#DD954F")c = pygame.Surface((579, 579), flags=pygame.HWSURFACE)c.fill(color='#121010')e = pygame.Surface((31, 31), flags=pygame.HWSURFACE)e.fill(color="#DD954F")screen.blit(a, (6.5, 6.5))screen.blit(b, (15, 15))screen.blit(c, (18, 18))# 棋盘格子for j in range(18):for i in range(18):# 起点是20,间隔是32,每个格子大小31,所以格子间距1screen.blit(e, (20 + 32 * i, 20 + 32 * j))# 存储棋盘状态global alistalist = np.zeros((19, 19))# 星位pygame.draw.circle(screen, '#121010', [307.5, 307.5], 5)pygame.draw.circle(screen, '#121010', [115.5, 307.5], 5)pygame.draw.circle(screen, '#121010', [499.5, 307.5], 5)pygame.draw.circle(screen, '#121010', [115.5, 499.5], 5)pygame.draw.circle(screen, '#121010', [499.5, 499.5], 5)pygame.draw.circle(screen, '#121010', [115.5, 115.5], 5)pygame.draw.circle(screen, '#121010', [499.5, 115.5], 5)pygame.draw.circle(screen, '#121010', [307.5, 499.5], 5)pygame.draw.circle(screen, '#121010', [307.5, 115.5], 5)# 刷新窗口pygame.display.flip()# 绘制棋子
def black(x, y):a = 20b = 20c = 20d = 0.01# 循环50次,每次绘制50个半径颜色不同的同心圆for i in range(50):pygame.draw.circle(screen, (a, b, c), [19.5 + 32 * x, 19.5 + 32 * y], (16 / (d - 5) + 16))a += 1b += 1c += 1d += 0.08pygame.display.update()def white(x, y):a = 170b = 170c = 170d = 0.02for i in range(50):pygame.draw.circle(screen, (a, b, c), [19.5 + 32 * x, 19.5 + 32 * y], (16 / (d - 5) + 16))a += 1b += 1c += 1d += 0.08pygame.display.update()pygame.font.init()
font1 = pygame.font.Font(None, 250)# 主要操作
def do(wb):while True:for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()if event.type == pygame.MOUSEBUTTONDOWN:x, y = pygame.mouse.get_pos()# 棋盘边界线的中点是19.5, 通过计算得到当前坐标在棋盘的行号和列号(x,y)x = round((x - 19.5) / 32)y = round((y - 19.5) / 32)if x < 0:x = 0if x > 18:x = 18if y < 0:y = 0if y > 18:y = 18if alist[x][y] == 0:eval(wb + "({},{})".format(x, y))if wb == "black":alist[x][y] = 1wb1 = "black"wb = "white"elif wb == "white":alist[x][y] = 2wb1 = "white"wb = "black"check(x, y, wb1)
def check(x, y, wb1):xx = xyy = ywhile True:# 从最上边的棋子开始检查,记录颜色相同得棋子数量# 先找到最同一条线上最上边的同色棋子if xx == 0:breakelif alist[xx][yy] != alist[x][y]:xx += 1breakelse:xx -= 1num = 0while True:if xx == 18:breakelif alist[xx][yy] != alist[x][y]:breakelse:xx += 1num += 1if num >= 5:win(wb1)# 从最边的棋子开始检查,记录颜色相同得棋子数量# 先找到最同一条线上最左边的同色棋子xx = xyy = ywhile True:if yy == 0:breakelif alist[xx][yy] != alist[x][y]:yy += 1breakelse:yy -= 1num = 0while True:if yy == 18:breakelif alist[xx][yy] != alist[x][y]:breakelse:yy += 1num += 1if num >= 5:win(wb1)# 从左上方的棋子开始检查,记录颜色相同得棋子数量# 先找到最同一条线上左上方的同色棋子xx = xyy = ywhile True:if xx == 0:breakelif yy == 0:breakelif alist[xx][yy] != alist[x][y]:xx += 1yy += 1breakelse:xx -= 1yy -= 1num = 0while True:if xx == 18:breakelif yy == 18:breakelif alist[xx][yy] != alist[x][y]:breakelse:xx += 1yy += 1num += 1if num >= 5:win(wb1)# 从右上方的棋子开始检查,记录颜色相同得棋子数量# 先找到最同一条线上右上方的同色棋子xx = xyy = ywhile True:if xx == 0:breakelif yy == 18:breakelif alist[xx][yy] != alist[x][y]:xx += 1yy -= 1breakelse:xx -= 1yy += 1num = 0while True:if xx == 18:breakelif yy == 0:breakelif alist[xx][yy] != alist[x][y]:breakelse:xx += 1yy -= 1num += 1if num >= 5:pygame.font.init()win(wb1)def win(wb1):pygame.font.init()font = pygame.font.Font(None, 70)text = font.render(f"{wb1} won", True, (255, 255, 255))screen.blit(text, ((655 - text.get_width()) / 2, (665 - text.get_height()) / 2))pygame.display.update()pygame.display.flip()while True:for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()if event.type == pygame.MOUSEBUTTONDOWN:init()do("black")if __name__ == "__main__":init()do('black')
4.实现人机对弈
现在五子棋的基本公能已经实现了,但这种充满策略与博弈的棋类游戏,猜测并应对对手可能的布局,相互之间进行激烈的智力对决才是游戏最好玩的部分,一个人玩也太无聊了,要是电脑能像人一样可以思考该多好呀,就像近几年的特别火的“阿尔法狗”一样,我很期待被自己的程序打败。那么怎样实现人机对弈呢?
随着人工智能的快速发展,其在许多领域都得到了广泛应用,其中就包括游戏人工智能。游戏人工智能分为两大类,
一类叫做确定性人工智能,另一类叫做非确定性人工智能。
确定性人工智能,是指电脑按照确定的规则进行判断和抉择。非确定性人工智能是指电脑根据已有的规则来学习新的规则,例如神经网络、遗传算法、决策树和概率方法等。
因为五子棋的规则是确定的,所以我们只用让电脑记住规则,并按照规则来决策和判断。在棋类游戏中,如果把棋子的每一种布局都当作是一种状态
,那对局中所有的可能局面的集合被称为状态空间
,状态空间搜素就是根据某种规则,在所有的局面中找到最有利的局面,并以此来决定下一步走法。由于五子棋的规则简单,不同状态的最优解比较少,不必采用耗费空间的状态空间搜素法,只用让电脑记住所有的最优解即可。
4.1列举最优解
回想一下自己下棋的思考过程,决策和判断的依据无非就那几种,进过权衡后选择最有利的走法。模仿人的思考过程,我们可以告诉电脑所有的走法,然后将这些走法按等级排序,电脑每次决策的时候就在这些走法集合中按照等级由高到低的顺序依次搜素,没有找到最优解则随机下子。
为了让电脑能识别走法,首先要对棋局的各种走法进行描述和储存,这里我们用一个cdata列表实现,0表示空棋、1表示黑棋、 2表示白棋、3表示将要下棋的位置。
# 0表示空棋
# 1表示黑棋
# 2表示白棋
# 3表示下棋的位置cdata = [# 一颗棋子的情况[1, 3, 0, 0, 0], [0, 1, 3, 0, 0], [0, 0, 1, 3, 0], [0, 0, 0, 1, 3], [0, 0, 0, 3, 1],[2, 3, 0, 0, 0], [0, 2, 3, 0, 0], [0, 0, 2, 3, 0], [0, 0, 0, 2, 3], [0, 0, 0, 3, 2],# 二颗棋子的情况[0, 1, 3, 1, 0], [1, 1, 3, 0, 0], [0, 0, 3, 1, 1],[2, 2, 3, 0, 0], [0, 0, 3, 2, 2], [0, 2, 3, 2, 0],# 三颗棋子的情况[1, 1, 1, 3, 0], [0, 3, 1, 1, 1], [1, 1, 3, 1, 0], [1, 3, 1, 1, 0],[2, 2, 0, 3, 2], [2, 3, 0, 2, 2], [0, 3, 2, 2, 2], [2, 2, 3, 2, 0],[2, 3, 2, 2, 0], [0, 2, 3, 2, 2], [0, 2, 2, 3, 2], [2, 2, 2, 3, 0], [3, 2, 2, 2, 0],# 四颗棋子情况[1, 1, 1, 1, 3], [3, 1, 1, 1, 1], [1, 1, 1, 3, 1], [1, 3, 1, 1, 1], [1, 1, 3, 1, 1],[2, 2, 2, 2, 3], [3, 2, 2, 2, 2], [2, 2, 3, 2, 2], [2, 3, 2, 2, 2], [2, 2, 2, 3, 2]
]
4.2匹配最优解
现在电脑已经掌握各种走法了,决胜秘籍在手,电脑改怎么用呢?接下来我们定义三个全局变量,用来辅助匹配算法的执行。 用ai_col 下棋位置列号, ai_row下棋位置行号,max_level走法等级
global ai_col, ai_row, max_levelai_col = -1ai_row = -1max_level = -1
接着我们来定义auto_match()函数
def auto_mach(row, col, level, dx, dy):global ai_col, ai_row, max_levelcol_sel = -1 # 暂存棋子列号row_sel = -1 # 暂存棋子行号isfind = True # 匹配成功的标记for j in range(5):cs = alist[row + j * dx][col + j * dy]if cs == 0:if cdata[level][j] == 3:row_sel = row + j * dxcol_sel = col + j * dyelif cdata[level][j] == 1:isfind = Falsebreakelif cdata[level][j] == 2:isfind = Falsebreakelif cs != cdata[level][j]:isfind = Falsebreakif isfind:ai_row = row_selai_col = col_selmax_level = levelreturn Truereturn False
函数需要4个参数row和col是棋子的位置,level是走法等级,dx,dy是下一步方向,用row_sel,col_sel来存储匹配过程中棋子的位值,棋子按照dx,dy的方向一次往后匹配,
如果棋盘布局和走法列表匹配成功则发挥True,反正返回False。
4.3自主操作
完成了匹配操作后,电脑就有了判断的能力了,接下来我们来实现电脑的自主操作。下面定义ai_play()
def ai_play():global ai_col, ai_row, max_levelai_col = -1ai_row = -1max_level = -1# 搜素棋盘每个位置for i in range(19):for j in range(19):# 从高到低搜索for level in range(len(cdata) - 1, -1, -1):if level > max_level:if i + 4 < 19:if auto_mach(i, j, level, 1, 0):breakif j + 4 < 19:if auto_mach(i, j, level, 0, 1):breakif i + 4 < 19 and j + 4 < 19:if auto_mach(i, j, level, 1, 1):breakif i + 4 < 19 and j - 4 > 0:if auto_mach(i, j, level, 1, -1):breakif ai_row!=-1 and ai_row!=-1:alist[ai_row][ai_col]=2return Truewhile True:col = random.randint(0,18)row = random.randint(0, 18)if alist[row][col]==0:alist[row][col]=2ai_row=rowai_col=colreturn Truereturn False
该函数首先将全局变量max_level,ai_row,ai_col重置为-1,清除上次结果的影响,然后从棋盘第一个位置开始匹配,max_level记录最高等级,凡是等级比其低的走法直接不考虑,这样能减少不必要的操作,加快程序运行速率,匹配结束后,若找到最优解了就更新ai_row,ai_col,没找到就随机选一个符合要求的位置。
完整代码
至此程序全部编写完成,现在运行游戏玩一下,看看你和计算机谁更厉害,如果被打败了这就是你肝了半天的代码最好的反馈。
import random
import sys
import pygameimport numpy as np# 0表示空棋
# 1表示黑棋
# 2表示白棋
# 3表示下棋的位置cdata = [# 一颗棋子的情况[1, 3, 0, 0, 0], [0, 1, 3, 0, 0], [0, 0, 1, 3, 0], [0, 0, 0, 1, 3], [0, 0, 0, 3, 1],[2, 3, 0, 0, 0], [0, 2, 3, 0, 0], [0, 0, 2, 3, 0], [0, 0, 0, 2, 3], [0, 0, 0, 3, 2],# 二颗棋子的情况[0, 1, 3, 1, 0], [1, 1, 3, 0, 0], [0, 0, 3, 1, 1],[2, 2, 3, 0, 0], [0, 0, 3, 2, 2], [0, 2, 3, 2, 0],# 三颗棋子的情况[1, 1, 1, 3, 0], [0, 3, 1, 1, 1], [1, 1, 3, 1, 0], [1, 3, 1, 1, 0],[2, 2, 0, 3, 2], [2, 3, 0, 2, 2], [0, 3, 2, 2, 2], [2, 2, 3, 2, 0],[2, 3, 2, 2, 0], [0, 2, 3, 2, 2], [0, 2, 2, 3, 2], [2, 2, 2, 3, 0], [3, 2, 2, 2, 0],# 四颗棋子情况[1, 1, 1, 1, 3], [3, 1, 1, 1, 1], [1, 1, 1, 3, 1], [1, 3, 1, 1, 1], [1, 1, 3, 1, 1],[2, 2, 2, 2, 3], [3, 2, 2, 2, 2], [2, 2, 3, 2, 2], [2, 3, 2, 2, 2], [2, 2, 2, 3, 2]
]def auto_mach(row, col, level, dx, dy):global ai_col, ai_row, max_levelcol_sel = -1 # 暂存棋子列号row_sel = -1 # 暂存棋子行号isfind = True # 匹配成功的标记for j in range(5):cs = alist[row + j * dx][col + j * dy]if cs == 0:if cdata[level][j] == 3:row_sel = row + j * dxcol_sel = col + j * dyelif cdata[level][j] == 1:isfind = Falsebreakelif cdata[level][j] == 2:isfind = Falsebreakelif cs != cdata[level][j]:isfind = Falsebreakif isfind:ai_row = row_selai_col = col_selmax_level = levelreturn Truereturn Falsedef ai_play():global ai_col, ai_row, max_levelai_col = -1ai_row = -1max_level = -1# 搜素棋盘每个位置for i in range(19):for j in range(19):# 从高到低搜索for level in range(len(cdata) - 1, -1, -1):if level > max_level:if i + 4 < 19:if auto_mach(i, j, level, 1, 0):breakif j + 4 < 19:if auto_mach(i, j, level, 0, 1):breakif i + 4 < 19 and j + 4 < 19:if auto_mach(i, j, level, 1, 1):breakif j + 4 < 19 and i - 4 > 0:if auto_mach(i, j, level, -1, 1):breakif ai_row!=-1 and ai_row!=-1:alist[ai_row][ai_col]=2return Truewhile True:col = random.randint(0,18)row = random.randint(0, 18)if alist[row][col]==0:alist[row][col]=2ai_row=rowai_col=colreturn Truereturn Falsedef init():pygame.init()# 创建窗口,背景为棕色global screenscreen = pygame.display.set_mode((615, 615))pygame.display.set_caption('五子棋')screen.fill("#DD954F")# 创建外边框a = pygame.Surface((603, 603), flags=pygame.HWSURFACE)a.fill(color='#121010')b = pygame.Surface((585, 585), flags=pygame.HWSURFACE)b.fill(color="#DD954F")c = pygame.Surface((579, 579), flags=pygame.HWSURFACE)c.fill(color='#121010')e = pygame.Surface((31, 31), flags=pygame.HWSURFACE)e.fill(color="#DD954F")screen.blit(a, (6.5, 6.5))screen.blit(b, (15, 15))screen.blit(c, (18, 18))# 棋盘格子for j in range(18):for i in range(18):# 起点是20,间隔是32,每个格子大小31,所以格子间距1screen.blit(e, (20 + 32 * i, 20 + 32 * j))# 存储棋盘状态global alistalist = np.zeros((19, 19))# 星位pygame.draw.circle(screen, '#121010', [307.5, 307.5], 5)pygame.draw.circle(screen, '#121010', [115.5, 307.5], 5)pygame.draw.circle(screen, '#121010', [499.5, 307.5], 5)pygame.draw.circle(screen, '#121010', [115.5, 499.5], 5)pygame.draw.circle(screen, '#121010', [499.5, 499.5], 5)pygame.draw.circle(screen, '#121010', [115.5, 115.5], 5)pygame.draw.circle(screen, '#121010', [499.5, 115.5], 5)pygame.draw.circle(screen, '#121010', [307.5, 499.5], 5)pygame.draw.circle(screen, '#121010', [307.5, 115.5], 5)# 刷新窗口pygame.display.flip()# 绘制棋子
def black(x, y):a = 20b = 20c = 20d = 0.01# 循环50次,每次绘制50个半径颜色不同的同心圆for i in range(50):pygame.draw.circle(screen, (a, b, c), [19.5 + 32 * x, 19.5 + 32 * y], (16 / (d - 5) + 16))a += 1b += 1c += 1d += 0.08pygame.display.update()def white(x, y):a = 170b = 170c = 170d = 0.02for i in range(50):pygame.draw.circle(screen, (a, b, c), [19.5 + 32 * x, 19.5 + 32 * y], (16 / (d - 5) + 16))a += 1b += 1c += 1d += 0.08pygame.display.update()pygame.font.init()
font1 = pygame.font.Font(None, 250)# 主要操作
def do(wb):while True:for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()if event.type == pygame.MOUSEBUTTONDOWN:x, y = pygame.mouse.get_pos()# 棋盘边界线的中点是19.5, 通过计算得到当前坐标在棋盘的行号和列号(x,y)x = round((x - 19.5) / 32)y = round((y - 19.5) / 32)if x < 0:x = 0if x > 18:x = 18if y < 0:y = 0if y > 18:y = 18if alist[x][y] == 0:black(x, y)alist[x][y] = 1wb1 = "You"wb = "white"check(x, y, wb1)pygame.time.wait(100)if ai_play():white(ai_row, ai_col)wb1 = "AI"wb = "black"check(ai_row, ai_col, wb1)def check(x, y, wb1):xx = xyy = ywhile True:# 从最上边的棋子开始检查,记录颜色相同得棋子数量# 先找到最同一条线上最上边的同色棋子if xx == 0:if alist[xx][yy] != alist[x][y]:xx += 1breakelif alist[xx][yy] != alist[x][y]:xx += 1breakelse:xx -= 1num = 0while True:if xx == 18:if alist[xx][yy] == alist[x][y]:num += 1breakelif alist[xx][yy] != alist[x][y]:breakelse:xx += 1num += 1if num >= 5:win(wb1)# 从最边的棋子开始检查,记录颜色相同得棋子数量# 先找到最同一条线上最左边的同色棋子xx = xyy = ywhile True:if yy == 0:if alist[xx][yy] != alist[x][y]:yy += 1breakelif alist[xx][yy] != alist[x][y]:yy += 1breakelse:yy -= 1num = 0while True:if yy == 18:if alist[xx][yy] == alist[x][y]:num += 1breakelif alist[xx][yy] != alist[x][y]:breakelse:yy += 1num += 1if num >= 5:win(wb1)# 从左上方的棋子开始检查,记录颜色相同得棋子数量# 先找到最同一条线上左上方的同色棋子xx = xyy = ywhile True:if xx == 0:if alist[xx][yy] != alist[x][y]:xx += 1yy += 1breakelif yy == 0:if alist[xx][yy] != alist[x][y]:xx += 1yy += 1breakelif alist[xx][yy] != alist[x][y]:xx += 1yy += 1breakelse:xx -= 1yy -= 1num = 0while True:if xx == 18:if alist[xx][yy] == alist[x][y]:num += 1breakelif yy == 18:if alist[xx][yy] == alist[x][y]:num += 1breakelif alist[xx][yy] != alist[x][y]:breakelse:xx += 1yy += 1num += 1if num >= 5:win(wb1)# 从右上方的棋子开始检查,记录颜色相同得棋子数量# 先找到最同一条线上右上方的同色棋子xx = xyy = ywhile True:if xx == 0:if alist[xx][yy] != alist[x][y]:xx += 1yy -= 1breakelif yy == 18:if alist[xx][yy] != alist[x][y]:xx += 1yy -= 1breakelif alist[xx][yy] != alist[x][y]:xx += 1yy -= 1breakelse:xx -= 1yy += 1num = 0while True:if xx == 18:if alist[xx][yy] == alist[x][y]:num += 1breakelif yy == 0:if alist[xx][yy] == alist[x][y]:num += 1breakelif alist[xx][yy] != alist[x][y]:breakelse:xx += 1yy -= 1num += 1if num >= 5:win(wb1)def win(wb1):pygame.font.init()font = pygame.font.Font(None, 70)text = font.render(f"{wb1} won", True, (255, 255, 255))screen.blit(text, ((655 - text.get_width()) / 2, (665 - text.get_height()) / 2))pygame.display.update()while True:for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()if event.type == pygame.MOUSEBUTTONDOWN:init()text = font.render("Start the game", True, (255, 255, 255))screen.blit(text, ((655 - text.get_width()) / 2, (665 - text.get_height()) / 2))pygame.display.update()pygame.time.wait(500)init()do("black")if __name__ == "__main__":init()do('black')
参考资料《趣学python游戏编程》