游戏开发介绍:
使用窗口设计界面布局,画出三行三列的表格样子的棋盘,画出圆形棋子,并找出所有的落棋位置,
添加鼠标点击事件,设置一系列初始值(比如鼠标是否点击,棋子颜色交替改变,结束标志等)。
定义timi()函数判断胜负,bestScore()函数选择最优下棋方案,AI()函数设置电脑下棋的步骤。
井字棋的完整走法:首先画出两层博弈树的所有节点,然后计算出所有叶节点,也就是最下层节点的评估值,即评价函数的值。自上而下进行倒推,计算出根节点的评估值,然后就能确定合适的走法。
如下图所示,当我方棋子用叉表示,敌方用圆圈表示,我方选择第二行第一列的位置做先手,那么判断地方可能落子的五种走法,当敌方落到中心位置时,我发方赢得可能性最大,再从第一层扩展图中选择评估值最小的走法作为我方最终的落子点。
#最优方案(状态,最小值,最大值)
def bestScore(sta, alpha, beta):
if sta:
pi = 1
else:
pi = 2
#局势判断 0(-1):红胜 1(1):绿胜 3(0)