Ejemplo n.º 1
0
        public int Minimax(bool isMax, int depth)
        {
            if (TicTacToeBoard.IsGameOver())
            {
                return(TicTacToeBoard.EvaluateWinningScore(depth));
            }

            var Scores = new List <int>();

            var availableMoves = TicTacToeBoard.GetAvailablePositions();

            //Calculate MIN and MAX scores
            if (isMax)
            {
                foreach (var move in availableMoves)
                {
                    // Make a move from the available
                    // user made move, now compo makes
                    TicTacToeBoard.Board[move.Item1, move.Item2] = MaxPlayer;

                    // Add the score of the that board
                    Scores.Add(Minimax(!isMax, depth++));
                    TicTacToeBoard.Board[move.Item1, move.Item2] = TicTacToeBoard.Player.E;
                }

                int maxScore = Scores.Max();

                return(maxScore);
            }
            else
            {
                foreach (var move in availableMoves)
                {
                    // Make a move from the available
                    // user made move, now compo makes
                    TicTacToeBoard.Board[move.Item1, move.Item2] = MinPlayer;
                    // Add the score of the that board
                    Scores.Add(Minimax(!isMax, depth++));

                    TicTacToeBoard.Board[move.Item1, move.Item2] = TicTacToeBoard.Player.E;
                }

                int minScore = Scores.Min();

                return(minScore);
            }
        }