Exemplo n.º 1
0
        public IActionResult Index()
        {
            TicTacToe ticTacToe = TicTacToe.Instance;

            ticTacToe.ClearBoard();

            TicTacToeBot  bot  = TicTacToeBot.Instance;
            TicTacToeUser user = TicTacToeUser.Instance;

            bot.SetTicTacToe(ticTacToe);
            user.SetTicTacToe(ticTacToe);

            // switch starting
            if (ticTacToe.IsPlayerStarting == true)
            {
                ticTacToe.IsPlayerStarting = false;
                // bot first move
                AlphaBetaPruning alphaBetaPruning = new AlphaBetaPruning(ticTacToe.ticTacToeBoard, ticTacToe.Level);
                Tuple <int, int> xy = alphaBetaPruning.BestMove();
                bot.MakeMove(xy.Item1, xy.Item2);
            }
            else
            {
                ticTacToe.IsPlayerStarting = true;
            }

            return(View(ticTacToe));
        }
Exemplo n.º 2
0
        public static void Main()
        {
            var board = new TicTacToeBoard();

            var minimax = new MinimaxDecision <ProblemState, ProblemAction>();

            var alphaBeta = new AlphaBetaPruning <ProblemState, ProblemAction>();

            var agent = new ProblemAgent(alphaBeta);

            ProblemAction action;

            while ((action = agent.Work(board)) != default)
            {
                var player = agent.Problem.Player(agent.State);

                if (player == ProblemDefinition.PlayerX)
                {
                    Console.WriteLine($"Enter Positions (e.g. {action.PutPosition.x} {action.PutPosition.y}): ");
                    action = ProblemStateExtensions.FromInput(player, Console.ReadLine());
                }

                board = agent.Problem.Result(agent.State, action).FromProblemState();

                WriteBoard(board);
            }
        }
Exemplo n.º 3
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));
        }