gpt4 book ai didi

javascript - 协助在 Javascript 中连接四极小极大算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:47:52 26 4
gpt4 key购买 nike

我正在尝试使用 minimax 算法在我的连连四游戏中创建 AI,但我无法让它发挥作用。我相信我已经很接近了,但我仍然无法正确理解。谁能帮我解决任何错误?我意识到我的代码不是很好,因为我使用 Javascript 的效率还不是很高,这就是我想尝试一下的原因。如果我完全偏离轨道,有人可以告诉我更好的方法吗?提前致谢。

编辑:我已经用更新后的代码替换了下面的代码。就给我 AI Action 而言,它现在“有效”,但问题是它们不是“智能” Action 。我看过许多 minimax 定义,我觉得我已经正确地实现了它。即使当我达到 7 的深度时,一个 5 岁的 child 也能击败它。任何帮助将不胜感激。

    function getBestMove(currBoard,depth,who) {
var opp;
//Get opponent for next piece
if(who == 'a') {
opp = 'p';
} else {
opp = 'a';
}

var tBoard = new Array(rows);
for(var i=0; i<tBoard.length; i++) {
tBoard[i] = new Array(cols);
}

var moves = new Array(aiOpenCols.length);
//Drop each piece and use minimax function until depth == 0
for(var i=0; i<aiOpenCols.length; i++) {
for(var j=0; j<rows; j++) {
for(var k=0; k<cols; k++) {
tBoard[j][k] = currBoard[j][k];
}
}
tBoard = dropPiece(aiOpenCols[i],who,tBoard);
moves[i] = minimax(tBoard,(+depth - 1),opp,aiOpenCols[i]);
}

var bestAlpha = -100000; //Large negative
//Use random column if no moves are "good"
var bestMove = Math.floor(Math.random() * aiOpenCols.length);
bestMove = +aiOpenCols[bestMove];
//Get largest value from moves for best move
for(var i=0; i<aiOpenCols.length; i++) {
if(+moves[i] > bestAlpha) {
bestAlpha = moves[i];
bestMove = aiOpenCols[i];
}
}

bestMove++; //Offset by 1 due to actual drop function
return bestMove;
}
function minimax(currBoard,depth,who,col) {
//Drop current piece, called from getBestMove function
currBoard = dropPiece(col,who,currBoard);

//When depth == 0 return heuristic/eval of board
if(+depth == 0) {
var ev = evalMove(currBoard);
return ev;
}
var alpha = -100000; //Large negative
var opp;
//Get opponent for next piece
if(who == 'a') {
opp = 'p';
} else {
opp = 'a';
}

//Loop through all available moves
for(var i=0; i<aiOpenCols.length; i++) {
var tBoard = new Array(rows);
for(var i=0; i<tBoard.length; i++) {
tBoard[i] = new Array(cols);
}
for(var j=0; j<rows; j++) {
for(var k=0; k<cols; k++) {
tBoard[j][k] = currBoard[j][k];
}
}
//Continue recursive minimax until depth == 0
var next = minimax(tBoard,(+depth - 1),opp,aiOpenCols[i]);
//Alpha = max(alpha, -minimax()) for negamax
alpha = Math.max(alpha, (0 - +next));
}
return alpha;
}
function evalMove(currBoard) {
//heuristic function
//AI = # of 4 streaks + # of 3 streaks + # of 2 streaks - # of 3 streaks opp - # of 2 streaks opp
var fours = checkFours(currBoard,'b') * 1000;
var threes = checkThrees(currBoard,'b') * 100;
var twos = checkTwos(currBoard,'b') * 10;
var oppThrees = checkThrees(currBoard,'r') * 100;
var oppTwos = checkTwos(currBoard,'r') * 10;

var scores = fours + threes + twos - oppThrees - oppTwos;

//If opponent wins, return large negative
var oppFours = checkFours(currBoard,'r');
if(+oppFours > 0) {
return -100000;
} else {
return scores;
}
}
function dropPiece(col,who,currBoard) {
for(var i=0; i<currBoard.length; i++) {
if(currBoard[i][col] != 'w') {
//Make sure column isn't full
if(i != 0) {
if(who == 'p') {
currBoard[i-1][col] = 'r';
} else {
currBoard[i-1][col] = 'b';
}
break;
}
}
//If column is empty, place in first row
if(i == (currBoard.length - 1)) {
if(who == 'p') {
currBoard[i][col] = 'r';
} else {
currBoard[i][col] = 'b';
}
}
}
return currBoard;
}

最佳答案

您的评估函数正在为没有值(value)的位置特征赋值。连续两个相邻的单元格没有任何值(value)。如果第四格可以被对手占据而不允许在其他地方获胜,那么连续三个也没有任何值(value)。

简化。对于 Connect Four,您只需要关心该位置是赢还是输,或者该位置是否可以在下一步中赢或输。如果赢了,返回一个大的正值。如果是亏损,则返回一个大的负值。如果下一步可以赢或输,则将该分支的搜索深度扩展一层,然后再次调用 minimax(),返回结果。后者将避免 horizon effect所有固定深度极小极大搜索都容易受到影响。否则返回零。

关于javascript - 协助在 Javascript 中连接四极小极大算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17865714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com