Groovy negaMax算法产生一些奇怪的结果
我目前正在实施一个跳棋游戏,唯一阻碍我的是我的人工智能状态不佳。它是用Groovy编写的 我有以下(尝试过的)带有alpha、beta修剪的negaMax算法。我已经遵循了几个伪指南,但我显然在某个地方失败了,因为结果相当荒谬 方法调用如下:Groovy negaMax算法产生一些奇怪的结果,groovy,artificial-intelligence,2d-games,minimax,negamax,Groovy,Artificial Intelligence,2d Games,Minimax,Negamax,我目前正在实施一个跳棋游戏,唯一阻碍我的是我的人工智能状态不佳。它是用Groovy编写的 我有以下(尝试过的)带有alpha、beta修剪的negaMax算法。我已经遵循了几个伪指南,但我显然在某个地方失败了,因为结果相当荒谬 方法调用如下:negaMax(3,Integer.MIN\u值,Integer.MAX\u值,1) 我已经决定1将是电脑播放器;用户的其他任何内容 def negaMax(int depth, int alpha, int beta, int player) {
negaMax(3,Integer.MIN\u值,Integer.MAX\u值,1)
我已经决定1将是电脑播放器;用户的其他任何内容
def negaMax(int depth, int alpha, int beta, int player) {
int score
int bestScore = Integer.MIN_VALUE
def moves = getMoves(player) // this function returns a hashmap as I felt I needed not just the move but the checker
// loop through all moves
for (move in moves) {
Position origin = move.key.location // save original position to allow undo
move.key.location = move.value // move piece
if (depth == 0) {
score = evaluateGameState(player)
} else {
score = -negaMax(depth - 1, -beta, -alpha, -player) // move score = - opponents best move
}
move.key.location = origin // undo move
if (player == 1) { // save successor evaluations for the computer to search
evaluations.put((move.key) , new PositionAndScore(score, move.value))
}
bestScore = Math.max(bestScore, score)
alpha = Math.max(alpha, bestScore)
if (alpha >= beta) {
break // prune
}
}
return bestScore
}
我选择了一个移动的哈希映射,键作为棋盘格(工件对象),值作为实际移动。我认为仅仅存储移动没有任何意义,因为我需要跟踪实际可以实现的操作
我使用另一个散列映射来存储后续评估,再次将检查器存储为键,但这次我存储了该值的位置和位置分数(我为此制作了一个类PositionAndScore)
evaluateGameState函数初始化一个分数,即该玩家可以移动多少个棋子,为任何国王添加一分,并为处于错误位置的任何棋子收回一分
在玩游戏时,电脑做出的前两个动作似乎很智能,但从那时起,它就走下坡路了。很多时候,计算机试图进行无效的移动,因此无法执行
我非常感谢任何人给我时间,看看我迄今为止所做的一切,如果有什么事情是错的,我会发表评论
非常感谢
编辑:好的,我取得了一些进展。正如我可能没有提到的那样,
评估
hashmap用于计算计算机的最佳移动。它的得分最高
这导致的问题是,在玩家为1的每个循环中都添加了评估hashmap,因此添加了不合法的移动(即,它们是未来的移动)
为了解决这个问题,我决定添加一个名为callSearch()
的前体方法,该方法被调用,而不是使用所有相同的参数调用negaMax
,但是它也将rootDepth
设置为depth
的方法
然后我对算法做了一个小小的修改
if (player == 1 && depth == rootDepth) {
}
我的想法是,我只想在搜索返回到根之后添加后续评估
不管怎样,在完成了所有这些之后,计算机不再试图做出非法的动作,但是它仍然不能做出有能力的动作。这可能是我的评估功能,虽然有点初级