tensorflow五子棋_如何用CNN玩转AlphaGo版的五子棋?

9e4f330763a74a00b8345d5bc736a7b8.gif

作者 | 李秋键责编| 郭芮

出品| CSDN(ID:CSDNnews)

近几年来,AI在游戏方面的发展如火如荼,尤其是自从阿法狗AI围棋战胜围棋之后,更是引起了AI发展的狂潮,同时也引起了很多AI游戏的应用与深化发展。其实游戏中的AI有非常悠久的历史,相当多的游戏都是围绕着对抗“敌人”展开,而这个“敌人”,就是AI,其中包含一些行为方式固定没有一丁点变化的低级AI,也有一些另外的随机因素高级一点的AI,不过这里的AI本质上是一段固定的程序脚本,如果玩家掌握到其中的规律,游戏性就会瞬间降低。

e4183c94e6994156d7cd33b1c8457fa7.png

而深度学习的AI版本却是不同,他与多个位置的参数与多方向的选择,拓展了其中AI的智能性,让玩家找到其中的规律性变得基本不可能,这也是深度学习的重要意义之一。今天,我们就将利用CNN实现智能五子棋。

baa85c075c8c03d7b302f87fe0ebe184.png

实验前的准备

首先我们使用的python版本是3.6.5。所测试的系统有windows10,windows7,Linux系统以及苹果系统。从这点也可以修剪python多平台和多扩展性,易于迁移的优点。

所使用的的python库有tkinter,其目的是用于规划棋盘布局,实现下棋功能;SGF文件用于读取棋谱并加载训练模型;os库用于读取和存储本地文件;TensorFlow库用于建立CNN网络模型以及训练等事项。

09b18e6dc7ab98f59d2531108afe6582.png

棋盘的建立

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
======================================================================== MICROSOFT FOUNDATION CLASS LIBRARY : fir ======================================================================== AppWizard has created this fir application for you. This application not only demonstrates the basics of using the Microsoft Foundation classes but is also a starting point for writing your application. This file contains a summary of what you will find in each of the files that make up your fir application. fir.dsp This file (the project file) contains information at the project level and is used to build a single project or subproject. Other users can share the project (.dsp) file, but they should export the makefiles locally. fir.h This is the main header file for the application. It includes other project specific headers (including Resource.h) and declares the CFirApp application class. fir.cpp This is the main application source file that contains the application class CFirApp. fir.rc This is a listing of all of the Microsoft Windows resources that the program uses. It includes the icons, bitmaps, and cursors that are stored in the RES subdirectory. This file can be directly edited in Microsoft Visual C++. fir.clw This file contains information used by ClassWizard to edit existing classes or add new classes. ClassWizard also uses this file to store information needed to create and edit message maps and dialog data maps and to create prototype member functions. res\fir.ico This is an icon file, which is used as the application's icon. This icon is included by the main resource file fir.rc. res\fir.rc2 This file contains resources that are not edited by Microsoft Visual C++. You should place all resources not editable by the resource editor in this file. ///////////////////////////////////////////////////////////////////////////// For the main frame window: MainFrm.h, MainFrm.cpp These files contain the frame class CMainFrame, which is derived from CFrameWnd and controls all SDI frame features. ///////////////////////////////////////////////////////////////////////////// AppWizard creates one document type and one view: firDoc.h, firDoc.cpp - the document These files contain your CFirDoc class. Edit these files to add your special document data and to implement file saving and loading (via CFirDoc::Serialize). firView.h, firView.cpp - the view of the document These files contain your CFirView class. CFirView objects are used to view CFirDoc objects. ///////////////////////////////////////////////////////////////////////////// Other standard files: StdAfx.h, StdAfx.cpp These files are used to build a precompiled header (PCH) file named fir.pch and a precompiled types file named StdAfx.obj. Resource.h This is the standard header file, which defines new resource IDs. Microsoft Visual C++ reads and updates this file. ///////////////////////////////////////////////////////////////////////////// Other notes: AppWizard uses "TODO:" to indicate parts of the source code you should add to or customize. If your application uses MFC in a shared DLL, and your application is in a language other than the operating system's current language, you will need to copy the corresponding localized resources MFC42XXX.DLL from the Microsoft Visual C++ CD-ROM onto the system or system32 directory, and rename it to be MFCLOC.DLL. ("XXX" stands for the language abbreviation. For example, MFC42DEU.DLL contains resources translated to German.) If you don't do this, some of the UI elements of your application will remain in the language of the operating system. /////////////////////////////////////////////////////////////////////////////
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值