コード例 #1
0
        public IActionResult NextMove(IFormCollection formCollection)
        {
            TicTacToe        ticTacToe        = TicTacToe.Instance;
            TicTacToeChecker ticTacToeChecker = new TicTacToeChecker();
            TicTacToeBot     bot  = TicTacToeBot.Instance;
            TicTacToeUser    user = TicTacToeUser.Instance;

            // setting level
            ticTacToe.Level = int.Parse(formCollection["Level"]);

            string place = formCollection["Button"];
            int    userX = int.Parse(place[0].ToString());
            int    userY = int.Parse(place[1].ToString());

            // user
            user.MakeMove(userX, userY);
            //
            ticTacToeChecker.CheckGameStatus(ticTacToe);

            if (ticTacToe.GameStatus == GameStatus.InProgress)
            {
                // bot
                AlphaBetaPruning alphaBetaPruning = new AlphaBetaPruning(ticTacToe.ticTacToeBoard, ticTacToe.Level);
                Tuple <int, int> xy = alphaBetaPruning.BestMove();
                bot.MakeMove(xy.Item1, xy.Item2);
                //
                ticTacToeChecker.CheckGameStatus(ticTacToe);

                if (ticTacToe.GameStatus != GameStatus.InProgress)
                {
                    ticTacToeChecker.CheckGameStatusAndGivePoint(ticTacToe);
                }
            }
            else
            {
                ticTacToeChecker.CheckGameStatusAndGivePoint(ticTacToe);
            }

            return(View("Index", ticTacToe));
        }
コード例 #2
0
        // MiniMax algorithm
        private int MiniMaxAlgorithm(int depth, bool isMaximizing)
        {
            TicTacToeChecker ticTacToeChecker = new TicTacToeChecker();

            // checking board state
            if (ticTacToeChecker.CheckIfSymbolWon(TicTacToeSymbol.Circle, _boardBeforeMove))
            {
                return(10);
            }
            else if (ticTacToeChecker.CheckIfSymbolWon(TicTacToeSymbol.Cross, _boardBeforeMove))
            {
                return(-10);
            }
            else if (ticTacToeChecker.GameEnded(_boardBeforeMove))
            {
                return(0);
            }

            if (depth >= _maxDepth)
            {
                return(0);
            }

            if (isMaximizing) // circle should be the best
            {
                int bestScore = -int.MaxValue;
                for (int i = 0; i < 3; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        if (_boardBeforeMove[i, j] == TicTacToeSymbol.Empty)
                        {
                            _boardBeforeMove[i, j] = TicTacToeSymbol.Circle;
                            int scoreMax = MiniMaxAlgorithm(depth + 1, false);
                            _boardBeforeMove[i, j] = TicTacToeSymbol.Empty;

                            bestScore = Math.Max(scoreMax, bestScore);
                        }
                    }
                }
                return(bestScore);
            }
            else // cross should be the worst
            {
                int bestScore = int.MaxValue;
                for (int i = 0; i < 3; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        if (_boardBeforeMove[i, j] == TicTacToeSymbol.Empty)
                        {
                            _boardBeforeMove[i, j] = TicTacToeSymbol.Cross;
                            int scoreMin = MiniMaxAlgorithm(depth + 1, true);
                            _boardBeforeMove[i, j] = TicTacToeSymbol.Empty;

                            bestScore = Math.Min(scoreMin, bestScore);
                        }
                    }
                }
                return(bestScore);
            }
        }