# Help with my minimax Algorithm

Updated on June 17, 2018 in
1 on June 16, 2018

I’ve been trying to implement minimax(negamax) algorithm in a tic-tac-toe game, It works very well in an almost terminal state(about 2pieces from each player pre-placed on the board on start), I just cant get it to make perfect decisions from an empty board on start. If you need more information, let me know thanks.

```//recursive function call
internal AIMove GetBestMove(char[] board, char player)
{
// testboard = board;
AIMove bestMove = null;

for (int i = 0; i < moveTile.emptyPoints.Count; i++)
{
AIMove move = new AIMove();
move.point = System.Convert.ToInt32(moveTile.emptyPoints[i].name);

board[System.Convert.ToInt32(moveTile.emptyPoints[i].name)] = player == GameManager.Player2Piece ? GameManager.Player2Piece : GameManager.Player1Piece; // If player is O use O else use X

var rv = CheckVictorySim(board, player, System.Convert.ToInt32(moveTile.emptyPoints[i].name));
// print(rv);

if (rv == 10) //AI wins
{
move.score = 1;
}
else if (rv == -10) // AI loses
{
move.score = -1;
}
else if (rv == 0) // draw
{
move.score = 0;
}

else if (rv == -1) //other
{
char[] newboard = new char[9]; //System.Array.Copy(umpire.board, newboard, 9); //board state
move.score = -GetBestMove(newboard, player == GameManager.Player2Piece ? GameManager.Player1Piece : GameManager.Player2Piece).score; // if player is O use X else use O
}

if (bestMove == null || move.score > bestMove.score)
{
bestMove = move;
}
print(bestMove.point);
}

//return the best move
return bestMove;
}
```
• Liked by