Help with my minimax Algorithm

Updated on June 17, 2018 in [A] C# .Net
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
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;
        }
        //aiMoves.Add(bestMove);
        print(bestMove.point);
    }
 
 
    //return the best move
    return bestMove;
}
  • Liked by
Reply
0 on June 17, 2018

What’s happening in the CheckVictorySim method?

 

And if making a decision on an empty board is your only problem, you could just hard code it to pick the center in such an edge case.

The center being, arguably, the most valuable position in tic-tac-toe.

  • Liked by
Reply
Cancel