#### Minimax function Checkers game

I would like to ask if this is the correct use of minimax. I call Minimax only for computer moves and try to choose the best one.

In the main function, the minimax steering wheel in this form is gradually applied to all moves by the computer. Understand that all possible coordinates are passed to him in turn

``````CGame gameCopy = game;
gameCopy.m_Board.makeMove(from.first, from.second, to[i].first, to[i].second, gameCopy.m_Computer);
int val = gameCopy.minimax(gameCopy, depth, alpha, beta, true, gameCopy.m_Computer);
``````

In minimax, I try to choose the best moves for the computer, but from the test games I get the impression that the selection is not optimal.

``````int CGame::minimax(CGame& game, int depth, int alpha, int beta, bool isMaximizingPlayer, Color playerColor)
{
if (isFinished() != 0 || depth == 0)
return evaluateBoard(game.m_Board, playerColor);

if (isMaximizingPlayer)
{
int maxEval = std::numeric_limits<int>::min();

for (const auto& [from, to] : game.m_Computer_Playable_Positions)
{
CBoard newBoard = game.m_Board;

if(to.size() == 1 && isMove(from.first, from.second, to[0].first, to[0].second))
{
newBoard.makeMove(from.first, from.second, to[0].first, to[0].second, playerColor);

int eval = minimax(game, depth - 1, alpha, beta, false, m_Player);
maxEval = std::max(maxEval, eval);
alpha = std::max(alpha, eval);
if(beta <= alpha)
break;
}
else
{
for (const auto& [toI, toJ] : to)
{
newBoard.makeAttack(from.first, from.second, toI, toJ, playerColor);

int eval = minimax(game, depth - 1, alpha, beta, false, m_Player);
maxEval = std::max(maxEval, eval);
alpha = std::max(alpha, eval);
if (beta <= alpha)
break; // Pruning
}
}
}

return maxEval;
}
else
{
int minEval = std::numeric_limits<int>::max();

for (const auto& [from, to] : game.m_Player_Playable_Positions)
{
CBoard newBoard = game.m_Board;

if (to.size() == 1 && isMove(from.first, from.second, to[0].first, to[0].second))
{
newBoard.makeMove(from.first, from.second, to[0].first, to[0].second, playerColor);

int eval = minimax(game, depth - 1, alpha, beta, true, m_Computer);
minEval = std::min(minEval, eval);
beta = std::min(beta, eval);
if (beta <= alpha)
break; // Pruning
}
else
{
for (const auto& [toI, toJ] : to)
{
newBoard.makeAttack(from.first, from.second, toI, toJ, playerColor);

int eval = minimax(game, depth - 1, alpha, beta, true, m_Computer);
minEval = std::min(minEval, eval);
beta = std::min(beta, eval);
if (beta <= alpha)
break; // Pruning
}
}
}

return minEval;
}
}
``````
``````int CGame::evaluateBoard(const CBoard& board, Color player)
{
int score = 0;

for(long int i=0; i<board.getSize(); i++)
{
for(long int j=0; j<board.getSize(); j++)
{
if(board[i][j].getType() == Type::STONE)
{
if(board[i][j].getColor() == player)
score += 10;
else
score -= 10;
}
else if(board[i][j].getType() == Type::QUEEN)
{
if(board[i][j].getColor() == player)
score += 30;
else
score -= 30;
}
}
}

return score;
}

``````

minimax checkers game c++

New contributor

user24995323 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.