作者 | 李秋键责编| 郭芮
出品| CSDN(ID:CSDNnews)
近几年来,AI在游戏方面的发展如火如荼,尤其是自从阿法狗AI围棋战胜围棋之后,更是引起了AI发展的狂潮,同时也引起了很多AI游戏的应用与深化发展。其实游戏中的AI有非常悠久的历史,相当多的游戏都是围绕着对抗“敌人”展开,而这个“敌人”,就是AI,其中包含一些行为方式固定没有一丁点变化的低级AI,也有一些另外的随机因素高级一点的AI,不过这里的AI本质上是一段固定的程序脚本,如果玩家掌握到其中的规律,游戏性就会瞬间降低。
而深度学习的AI版本却是不同,他与多个位置的参数与多方向的选择,拓展了其中AI的智能性,让玩家找到其中的规律性变得基本不可能,这也是深度学习的重要意义之一。今天,我们就将利用CNN实现智能五子棋。
实验前的准备
首先我们使用的python版本是3.6.5。所测试的系统有windows10,windows7,Linux系统以及苹果系统。从这点也可以修剪python多平台和多扩展性,易于迁移的优点。
所使用的的python库有tkinter,其目的是用于规划棋盘布局,实现下棋功能;SGF文件用于读取棋谱并加载训练模型;os库用于读取和存储本地文件;TensorFlow库用于建立CNN网络模型以及训练等事项。
棋盘的建立
1.初始化棋盘:
其中各参数设定意义如下:初始化:someoneWin:标识是否有人赢了;humanChessed:人类玩家是否下了;IsStart:是否开始游戏了;玩家:玩家是哪一方;玩法:模式,和机器人下棋,还是和ai下棋;bla_start_pos:黑棋开局时下在正中间的位置;bla_chessed:保存黑棋已经下过的棋子;whi_chessed:保存白棋已经下过过的棋子;board:棋盘;窗口:窗口;var:用于标记选择玩家颜色的一个变量;var1:用于标记选择robot或ai的一个变量;可以:画布,用于绘制出棋盘;net_board:棋盘的点信息;robot:机器人;sgf:处理棋谱;cnn:cnnc神经网络。
其中代码如下:
def __init__(self):
self.someoneWin = False
self.humanChessed = False
self.IsStart = False
self.player = 0
self.playmethod = 0
self.bla_start_pos = [235, 235]
self.whi_chessed =
self.bla_chessed =
self.board = self.init_board
self.window = Tk
self.var = IntVar
self.var.set(0)
self.var1 = IntVar
self.var1.set(0)
self.window.title("myGoBang")
self.window.geometry("600x470+80+80")
self.window.resizable(0, 0)
self.can = Canvas(self.window, bg="#EEE8AC