2020幻影围棋 playChess模块

news/2025/1/15 20:48:46/

PlayChess类

PlayChess类用于幻影围棋的行棋,对于平台发过来的每条指令有实现的处理逻辑。

"""这是幻影围棋的行棋引擎"""def __init__(self, condition=None,  mcts=None, position=None):self.condition = condition if condition is not None else gr.Condition()self.gameStatus = gf.GameStatusself.position = position if position is not None else Position()self.mcts = mcts if mcts is not None else gmcts.MCTS(value_fn=gp.win_probability, policy_fn=gp.next_probability, rollout_policy_fn=gp.next_probability)

Position类用于给MCT决策引擎提供棋盘数据与下棋逻辑。

def printStatus(self):"""输出当前状态:return:"""log.info(gf.Board.baseBoardToString())log.info('\n' + str(self.condition.libTracker.groupIndexMap))log.info('\n' + str(self.condition.libTracker.libertyCacheMap))log.info('\n' + str(gf.GameStatus()))def refuse(self, point):"""当行棋被拒绝时执行这个方法:param point::return:"""gf.GameStatus.refused = Truegf.Board.setPiecesByPoint(point, gf.GameStatus.opponentColor)try:self.condition.libTracker.addStone(color=gf.GameStatus.opponentColor, point=point)except gr.IllegalMove as illegal:passexcept Exception as e:passself.printStatus()position = Position()Record.saveStatus(position)def suggestMove(self):self.position = Position()log.info(self.position.baseBoardToString())log.info('start mct...')log.info(self.position.baseBoardToString())self.mcts = gmcts.MCTS(value_fn=gp.win_probability, policy_fn=gp.next_probability, rollout_policy_fn=gp.fast_next_probability)move = self.mcts.get_move(self.position)log.info("try to move " + str(move))return movedef takeStones(self, points, taked=False):"""当我方提子或者被对方提子时执行这个方法:param points::param taked: 如果我方被提子,设置为True:return:"""if taked is True:if gf.GameStatus.ourColor == gf.Chess.WHITE:gf.GameStatus.whiteCaputured += len(points)else:gf.GameStatus.blackCaputured += len(points)elif taked is False:if gf.GameStatus.opponentColor == gf.Chess.WHITE:gf.GameStatus.whiteCaputured += len(points)else:gf.GameStatus.blackCaputured += len(points)if taked is True:# 如果我方被提子的话,说明被提子的周围全是敌方的棋子oppoStones = gr.findTakeStonesAroundPoints(points)for point in oppoStones:gf.Board.placeStone(point, gf.GameStatus.opponentColor)gf.Board.placeStones(points, gf.Chess.EMPTY)#判断该点是否为我方劫点,如果是,则不能立即提回if taked is True and len(points) == 1 and (gr.isKo(points[0], self.condition.baseBoard) is gf.GameStatus.opponentColor):gf.GameStatus.koPoint = points[0]gf.GameStatus.koTimes += 1self.condition.libTracker = gr.LibertyTracker.newTrackerFromBaseBoard(gf.Board.getStaticBaseBoard())self.printStatus()def oppoMoveStone(self):if gf.GameStatus.refused is True:gf.GameStatus.curColor = gf.GameStatus.ourColorreturngf.GameStatus.curColor = gf.GameStatus.ourColorgf.GameStatus.opponentStep += 1def moveStone(self, point):"""accept后执行下棋操作:param point::return:"""if gf.GameStatus.koTimes > 0:gf.GameStatus.koPoint = (-1, -1)gf.GameStatus.koTimes = 0gf.GameStatus.refused = Falseif isinstance(point, gf.Point):point = point.toTuple()gf.Board.placeStone(point, gf.GameStatus.ourColor)try:capturedStones = self.condition.libTracker.addStone(color=gf.GameStatus.ourColor, point=point)if point == gf.GameStatus.koPoint:gf.GameStatus.koTimes += 1if (len(capturedStones) == 1) and (gr.isKo(point, gf.Board.baseBoard) == gf.GameStatus.opponentColor):gf.GameStatus.koPoint = list(capturedStones)[0]gf.GameStatus.round += 1gf.GameStatus.ourStep += 1self.printStatus()except gr.IllegalMove as illegal:log.exception(illegal)except Exception as e:passposition = Position()Record.saveStatus(position)

suggestMove方法调用MCT决策引擎得到下棋点并返回,这里重点讲下moveStone方法和takeStones方法:
moveStone方法:

 def moveStone(self, point):"""accept后执行下棋操作:param point::return:"""if gf.GameStatus.koTimes > 0:gf.GameStatus.koPoint = (-1, -1)gf.GameStatus.koTimes = 0gf.GameStatus.refused = Falseif isinstance(point, gf.Point):point = point.toTuple()gf.Board.placeStone(point, gf.GameStatus.ourColor)try:capturedStones = self.condition.libTracker.addStone(color=gf.GameStatus.ourColor, point=point)if point == gf.GameStatus.koPoint:gf.GameStatus.koTimes += 1if (len(capturedStones) == 1) and (gr.isKo(point, gf.Board.baseBoard) == gf.GameStatus.opponentColor):gf.GameStatus.koPoint = list(capturedStones)[0]gf.GameStatus.round += 1gf.GameStatus.ourStep += 1self.printStatus()except gr.IllegalMove as illegal:log.exception(illegal)except Exception as e:passposition = Position()Record.saveStatus(position)

moveStone用于accept后在棋盘上下棋,该方法首先会更新劫点信息,(如果之前有过打劫判断的话,就把劫点重置)在棋盘上下棋后addStone方法更新棋子气等信息,得到被吃掉的棋子的集合,这里有个对打劫的判断:

if (len(capturedStones) == 1) and (gr.isKo(point, gf.Board.baseBoard) == gf.GameStatus.opponentColor):gf.GameStatus.koPoint = list(capturedStones)[0]

最后调用Record类保存当前状态。

takeStones方法:

def takeStones(self, points, taked=False):"""当我方提子或者被对方提子时执行这个方法:param points::param taked: 如果我方被提子,设置为True:return:"""if taked is True:if gf.GameStatus.ourColor == gf.Chess.WHITE:gf.GameStatus.whiteCaputured += len(points)else:gf.GameStatus.blackCaputured += len(points)elif taked is False:if gf.GameStatus.opponentColor == gf.Chess.WHITE:gf.GameStatus.whiteCaputured += len(points)else:gf.GameStatus.blackCaputured += len(points)if taked is True:# 如果我方被提子的话,说明被提子的周围全是敌方的棋子oppoStones = gr.findTakeStonesAroundPoints(points)for point in oppoStones:gf.Board.placeStone(point, gf.GameStatus.opponentColor)gf.Board.placeStones(points, gf.Chess.EMPTY)#判断该点是否为我方劫点,如果是,则不能立即提回if taked is True and len(points) == 1 and (gr.isKo(points[0], self.condition.baseBoard) is gf.GameStatus.opponentColor):gf.GameStatus.koPoint = points[0]gf.GameStatus.koTimes += 1self.condition.libTracker = gr.LibertyTracker.newTrackerFromBaseBoard(gf.Board.getStaticBaseBoard())self.printStatus()

如果我方被提子的话,有种情况就是进入了打劫环节,此时我方不能立即提回。takeStone方法在把对应点设为EMPTY后重新生成一个libertyTracker。


http://www.ppmy.cn/news/259844.html

相关文章

一个让围棋软件自动对弈的工具

银星围棋5的目录下有一个GTool2.exe,利用它可以让两个不同的具有人机对弈功能的围棋软件互相对弈,感觉类似于左右手互搏。:) 步骤简单介绍如下: 1)分别启动两个具有围棋对弈功能的软件,例如MuiltiGo4(这里我采用Gnugo3.7.10做围棋引擎)和…

超级计算机比赛规则,介绍围棋的规则与玩法

介绍围棋的规则与玩法 围棋起源于中国古代,推测起源时间为大约公元前6世纪,是一种策略性二人棋类游戏,那么如何去玩围棋呢?以下是小编为大家搜集整理提供到的介绍围棋的规则与玩法,希望对您有所帮助。欢迎阅读参考学习…

围棋单机版-纵横十九道,迷煞多少人

纵横十九道,迷煞多少人,源于中国古代的围棋,虽历经千年,至今仍令无数人为之疯狂。下面分享几个完全免费的,好玩的单机版 围棋游戏软件 : 1、思佳围棋:棋力5级左右   思佳围棋采用…

Tom围棋(弈城围棋)运行错误:当前运行的版本是系统无法识别的版本

从http://duiyi.sports.tom.com/service/download.htm 下裁TOM围棋安装后。 管理员级别的用户可以正常运行,但是受限用户就报错:“当前运行的版本是系统无法识别的版本”,然后就退出了。 系统环境为Windows xp sp2 英文版。 解决办法分2大步&…

html游戏围棋源码,C++围棋游戏

【实例简介】 尚未完善的围棋游戏。使用vs2010开发。具有围棋棋盘,棋子界面,已经完成了提子算法。有兴趣的朋友可以与我一起来研究。欢迎大家前来交流 【实例截图】 【核心代码】 MyFirstMFC └── MyFirstMFC ├── MyFirstMFC │ ├── AssiDlg.c…

6.6 线程的回收及内存演示

目录 创建一个线程 pthread_exit pthread——join 线程的回收: 编译错误: 使用线程的分离: 创建一个线程 pthread_create是一个创建线程的函数,它的入参包括: 1. pthread_t *thread:指向线程标识符…

C#绘制围棋棋盘

一、创建项目 转到窗体视图,在pictureBox下方放置一个Button。点击button1,双击,系统自动生成的button1的按键响应函数Button1_Click中添加代码,添加完后如下: namespace WindowsForms1 { public partial class For…

网络围棋单机版

我在开发工作之余写了一个网络围棋,有单机版和网络版.现在把单机版上传给大家使用.欢迎多提意见,并帮我完善其中的围棋类,比如增加下完棋后如何算胜负. 请到我上传的资源去下载. http://download.csdn.net/user/whwqs 下棋去,可能需要安装svgviewer : http://groups.google…