# 在沒有遞歸的情況下實現Minimax

int miniMax (char board[BOARD_DIM][BOARD_DIM], _Bool minNode, int *xBest, int *yBest)
{
int possibleMoves[NSQUARES][2];
int nPossibleMoves = generateMoves(board, possibleMoves);
char boardChild [BOARD_DIM][BOARD_DIM];
int ind, x_ind, y_ind;
int minScore, maxScore;
if (gameOver(board))
return evaluateState(board);
else if (minNode)
{
minScore = +INFINITY;
for (ind = 0 ; ind < nPossibleMoves; ind++)
{
duplicateBoard(board, boardChild);
x_ind = possibleMoves[ind][0];
y_ind = possibleMoves[ind][1];
updateboard(boardChild, x_ind, y_ind, cPlayer);
int score = miniMax(boardChild,!minNode ,&x_ind ,&y_ind);
if (minScore > score)
minScore = score;
}
return minScore;
}
else if (!minNode)
{
maxScore = -INFINITY;
for (ind = 0 ; ind < nPossibleMoves; ind++)
{
duplicateBoard(board, boardChild);
x_ind = possibleMoves[ind][0];
y_ind = possibleMoves[ind][1];
updateboard(boardChild, x_ind, y_ind, cComputer);
int score = miniMax(boardChild,!minNode ,&x_ind ,&y_ind);
if (maxScore < score)
{
maxScore = score;
*xBest = x_ind;
*yBest = y_ind;
}
}
return maxScore;
}


## 最佳答案

• 以二進制編碼位置（位矩陣而不是2dim字節數組）
• 編碼完整的解決方案圖，所以一切都只是查找（線性查找可以很好地處理這種復雜性）