private void AutoPlay()
 {
     Board.HitPosition computerMove = ai.FindBestMove(board.gameState, false);
     Invoke((Action)(() => { board.DrawX(gEngine, computerMove.row, computerMove.column); }));
     Board.HitPosition computerMove2 = ai.FindBestMove(board.gameState, true);
     Invoke((Action)(() => { board.DrawO(gEngine, computerMove2.row, computerMove2.column); }));
 }
 private async void Panel1Board_Click(object sender, EventArgs e)
 {
     try
     {
         if (cbxGameMode.SelectedIndex == 0)
         {
             Point mousePos = Cursor.Position;
             mousePos = panel1Board.PointToClient(mousePos);
             Board.HitPosition move = board.DetectClickPos(mousePos);
             board.DrawO(gEngine, move.row, move.column);
             Board.HitPosition computerMove = ai.FindBestMove(board.gameState, false);
             board.DrawX(gEngine, computerMove.row, computerMove.column);
             label2.Text = "Score: " + ai.playerWin;
             if (ai.playerWin == 10)
             {
                 MessageBox.Show("Computer Win");
                 player2Score++;
                 lblP2Score.Text = player2Score.ToString();
                 ResetBoard();
             }
             if (ai.playerWin == -10)
             {
                 MessageBox.Show("Player Win");
                 player1Score++;
                 lblP1Score.Text = player1Score.ToString();
                 ResetBoard();
             }
         }
         if (cbxGameMode.SelectedIndex == 1)
         {
             Point mousePos = Cursor.Position;
             mousePos = panel1Board.PointToClient(mousePos);
             Board.HitPosition move = board.DetectClickPos(mousePos);
             if (playerTurn)
             {
                 //board.DrawO(gEngine, move.row, move.column);
                 playerTurn = false;
                 client.ReceiveMessage(currentPlayer, $"{move.row} {move.column}");
             }
             //else
             //{
             //    board.DrawX(gEngine, move.row, move.column);
             //    playerTurn = true;
             //}
         }
         if (cbxGameMode.SelectedIndex == 2)
         {
             board.Random(gEngine, 1);
             while (true)
             {
                 await AutoPlayAsync();
             }
         }
     }
     catch (Exception ex)
     {
         return;
     }
 }
 public object[] FindBestMoveParallelThread2(char[,] board, bool playerMove)
 {
     if (playerMove)
     {
         int bestValue          = int.MinValue;
         Board.HitPosition move = new Board.HitPosition();
         move.row    = -1;
         move.column = -1;
         for (int i = ((int)(size / 2) + 1); i < size; i++)
         {
             for (int j = 0; j < size; j++)
             {
                 progressBar.Value++;
                 if (board[i, j] == '-' && LookAround(board, i, j))
                 {
                     board[i, j] = player;
                     int moveValue = MinimaxAB(board, 2, int.MinValue, int.MaxValue, false);
                     board[i, j] = '-';
                     if (moveValue > bestValue)
                     {
                         move.row    = i;
                         move.column = j;
                         bestValue   = moveValue;
                     }
                 }
             }
         }
         playerWin         = bestValue;
         progressBar.Value = 0;
         return(new object[] { bestValue, move });
     }
     else
     {
         int bestValue          = int.MaxValue;
         int score              = int.MinValue;
         Board.HitPosition move = new Board.HitPosition();
         move.row    = -1;
         move.column = -1;
         for (int i = ((int)(size / 2) + 1); i < size; i++)
         {
             for (int j = 0; j < size; j++)
             {
                 progressBar.Value++;
                 if (board[i, j] == '-' && LookAround(board, i, j))
                 {
                     board[i, j] = computer;
                     int moveValue = MinimaxAB(board, 2, int.MinValue, int.MaxValue, true);
                     board[i, j] = '-';
                     if (moveValue < bestValue)
                     {
                         move.row    = i;
                         move.column = j;
                         bestValue   = moveValue;
                     }
                     if (moveValue > score)
                     {
                         score = moveValue;
                     }
                 }
             }
         }
         playerWin         = score;
         progressBar.Value = 0;
         return(new object[] { bestValue, move });
     }
 }
 public Board.HitPosition FindBestMove(char[,] board, bool playerMove)
 {
     if (playerMove)
     {
         int bestValue          = int.MinValue;
         int score              = int.MinValue;
         Board.HitPosition move = new Board.HitPosition();
         move.row    = -1;
         move.column = -1;
         for (int i = 0; i < size; i++)
         {
             for (int j = 0; j < size; j++)
             {
                 progressBar.Invoke((Action)(() => { progressBar.Value++; }));
                 if (board[i, j] == '-' && LookAround(board, i, j))
                 {
                     board[i, j] = player;
                     int moveValue = MinimaxAB(board, 2, int.MinValue, int.MaxValue, false);
                     board[i, j] = '-';
                     if (moveValue > score)
                     {
                         score = moveValue;
                     }
                     if (moveValue > bestValue)
                     {
                         move.row    = i;
                         move.column = j;
                         bestValue   = moveValue;
                     }
                 }
             }
         }
         playerWin = score;
         progressBar.Invoke((Action)(() => { progressBar.Value = 0; }));
         return(move);
     }
     else
     {
         int bestValue          = int.MaxValue;
         int score              = int.MinValue;
         Board.HitPosition move = new Board.HitPosition();
         move.row    = -1;
         move.column = -1;
         for (int i = 0; i < size; i++)
         {
             for (int j = 0; j < size; j++)
             {
                 progressBar.Invoke((Action)(() => { progressBar.Value++; }));
                 if (board[i, j] == '-' && LookAround(board, i, j))
                 {
                     board[i, j] = computer;
                     int moveValue = MinimaxAB(board, 2, int.MinValue, int.MaxValue, true);
                     board[i, j] = '-';
                     if (moveValue > score)
                     {
                         score = moveValue;
                     }
                     if (moveValue < bestValue)
                     {
                         move.row    = i;
                         move.column = j;
                         bestValue   = moveValue;
                     }
                 }
             }
         }
         playerWin = score;
         progressBar.Invoke((Action)(() => { progressBar.Value = 0; }));
         return(move);
     }
 }