#javascript #minimax
Вопрос:
Я пытаюсь реализовать алгоритм минимакса в шестнадцатеричной игре, но функция никогда не прекращает зацикливаться, и я, кажется, не могу точно сказать, в чем проблема, пожалуйста, помогите мне найти ошибку.
function initializeBoard() {
let n = 0;
board = (Array(boardSize)).fill().map(() => Array(boardSize).fill(0));
for (let i = 0; i < boardSize; i ){
for (let j= 0; j < boardSize; j ) {
board[i][j] = n ;
}
}
emptyCells = Array.from(Array(boardSize * boardSize).keys());
hBoard = Array.from(Array(boardSize * boardSize).keys());
}
function turn(squareId, p, row, col) {
board[row][col] = p.character;
hBoard[squareId] = p.character;
storeCells(squareId);
let colorClass = document.createElement("div");
colorClass.setAttribute("class", p.color);
document.getElementById(squareId).appendChild(colorClass);
let gameDecision = checkWin(board, p);
if (gameDecision != 0) {
updatePlayerWin(gameDecision);
gameOver = true;
}
}
Я использую helperBoard, потому что не знаю, как получить рандомизированные перемещения с помощью 2D-массива или получить из него только пустые ячейки.
// minmax still in process
function botLevel_3(newBoard, helperBoard, player, currentPlayer, opponentPlayer){
var availSpots = helperBoard.filter(s => typeof s == 'number');
if (checkWin(newBoard, opponentPlayer) == opponentPlayer.character){
return {score: -10};
}
else if (checkWin(newBoard, currentPlayer) == currentPlayer.character){
return {score : 10};
}
var moves = [];
while (availSpots.length != 0){
var currentMove = {};
var randMove = randomizeMove(availSpots);
var index = availSpots.splice(randMove, 1);
let row = Math.floor(index / 9);
let col = index % 9;
currentMove.index = newBoard[row][col];
newBoard[row][col] = player;
helperBoard[index] = player;
if (player == currentPlayer.character) {
if (checkWin(newBoard, currentPlayer) == currentPlayer.character) {
currentMove.score = 10;
newBoard[row][col] = currentMove.index;
helperBoard[index] = currentMove.index;
return currentMove;
}
var result = botLevel_3(newBoard, helperBoard, opponentPlayer.character, currentPlayer, opponentPlayer);
currentMove.score = result.score;
}
else {
var result = botLevel_3(newBoard, helperBoard, currentPlayer.character, currentPlayer, opponentPlayer);
currentMove.score = result.score;
}
newBoard[row][col] = currentMove.index;
helperBoard[index] = currentMove.index;
moves.push(currentMove);
}
// maximize
var bestMove;
if (player == currentPlayer.character){
var bestScore = -10000;
for (var i = 0; i < moves.length; i ){
if (moves[i].score > bestScore){
bestScore = moves[i].score;
bestMove = i;
}
}
}
//minimize
else {
var bestScore = 10000;
for (var i = 0; i < moves.length; i ){
if (moves[i].score < bestScore){
bestScore = moves[i].score;
bestMove = i;
}
}
}
return moves[bestMove];
}
исходный код == https://github.com/alvin-2002/Hex-Board-Game
Спасибо