Created
January 23, 2022 15:43
-
-
Save weirddan455/718acd6dc7c0d51b75ebaa7fbd0546b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define CHECKMATE_EVALUATION -9001 | |
#define STALEMATE_EVALUATION 0 | |
static int AISearch(int depth, GameState *state, int alpha, int beta) | |
{ | |
if (depth == 0) | |
{ | |
return AIEvaluate(state); | |
} | |
uint16_t moves[1024]; | |
int numMoves = getAllLegalMoves(moves, state); | |
if (numMoves <= 0) | |
{ | |
if (playerInCheck(state)) | |
{ | |
return CHECKMATE_EVALUATION; | |
} | |
else | |
{ | |
return STALEMATE_EVALUATION; | |
} | |
} | |
for (int i = 0; i < numMoves; i++) | |
{ | |
GameState copyState = *state; | |
movePiece(moves[i], ©State); | |
int score = AISearch(depth - 1, ©State, -beta, -alpha); | |
score = -score; | |
if (score >= beta) | |
{ | |
return beta; | |
} | |
if (score > alpha) | |
{ | |
alpha = score; | |
} | |
} | |
return alpha; | |
} | |
// Working but probably not optimal | |
uint16_t getComputerMove(void) | |
{ | |
uint16_t bestMoves[1024]; | |
uint32_t numBestMoves = 0; | |
uint16_t moves[1024]; | |
int numMoves = getAllLegalMoves(moves, &gameState); | |
int bestScore = CHECKMATE_EVALUATION; | |
for (int i = 0 ; i < numMoves; i++) | |
{ | |
GameState copyState = gameState; | |
movePiece(moves[i], ©State); | |
int score = AISearch(3, ©State, CHECKMATE_EVALUATION, -CHECKMATE_EVALUATION); | |
score = -score; | |
if (score > bestScore) | |
{ | |
bestScore = score; | |
bestMoves[0] = moves[i]; | |
numBestMoves = 1; | |
} | |
else if (score == bestScore) | |
{ | |
bestMoves[numBestMoves] = moves[i]; | |
numBestMoves++; | |
} | |
} | |
// Pick a move at random if multiple moves are tied for best evaluation. | |
// Helps stop AI from repeating moves. | |
if (numBestMoves == 0) | |
{ | |
debugLog("getComputerMove: Did not find a move (this should never happen)"); | |
} | |
return bestMoves[pcgRangedRandom(numBestMoves)]; | |
} | |
// Not working | |
uint16_t getComputerMove(void) | |
{ | |
uint16_t bestMoves[1024]; | |
uint32_t numBestMoves = 0; | |
uint16_t moves[1024]; | |
int numMoves = getAllLegalMoves(moves, &gameState); | |
int alpha = CHECKMATE_EVALUATION; | |
int beta = -CHECKMATE_EVALUATION; | |
for (int i = 0 ; i < numMoves; i++) | |
{ | |
GameState copyState = gameState; | |
movePiece(moves[i], ©State); | |
int score = AISearch(3, ©State, -beta, -alpha); | |
score = -score; | |
if (score > alpha) | |
{ | |
alpha = score; | |
bestMoves[0] = moves[i]; | |
numBestMoves = 1; | |
} | |
else if (score == alpha) | |
{ | |
bestMoves[numBestMoves] = moves[i]; | |
numBestMoves++; | |
} | |
} | |
// Pick a move at random if multiple moves are tied for best evaluation. | |
// Helps stop AI from repeating moves. | |
if (numBestMoves == 0) | |
{ | |
debugLog("getComputerMove: Did not find a move (this should never happen)"); | |
} | |
return bestMoves[pcgRangedRandom(numBestMoves)]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment