示例#1
0
        public void testMinmaxValueCalculation()
        {
            MinimaxSearch <TicTacToeState, XYLocation, string> search = MinimaxSearch <TicTacToeState, XYLocation, string> .createFor(game);

            Assert.IsTrue(epsilon > System.Math.Abs(search.maxValue(state, TicTacToeState.X) - 0.5));
            Assert.IsTrue(epsilon > System.Math.Abs(search.minValue(state, TicTacToeState.O) - 0.5));

            // x o x
            // o o x
            // - - -
            // next move: x
            state.mark(0, 0); // x
            state.mark(1, 0); // o
            state.mark(2, 0); // x

            state.mark(0, 1); // o
            state.mark(2, 1); // x
            state.mark(1, 1); // o

            Assert.IsTrue(epsilon > System.Math.Abs(search.maxValue(state, TicTacToeState.X) - 1));
            Assert.IsTrue(epsilon > System.Math.Abs(search.minValue(state, TicTacToeState.O)));
            XYLocation action = search.makeDecision(state);

            Assert.AreEqual(new XYLocation(2, 2), action);
        }
示例#2
0
        public void testMinmaxDecision()
        {
            MinimaxSearch <TicTacToeState, XYLocation, string> search = MinimaxSearch <TicTacToeState, XYLocation, string> .createFor(game);

            search.makeDecision(state);
            int expandedNodes = search.getMetrics().getInt(MinimaxSearch <TicTacToeState, XYLocation, string> .METRICS_NODES_EXPANDED);

            Assert.AreEqual(549945, expandedNodes);
        }
示例#3
0
    private void Start()
    {
        Nim game = new Nim(Matches);
        MinimaxSearch <NimState, int, int> minimaxSearch = MinimaxSearch <NimState, int, int> .createFor(game);

        AlphaBetaSearch <NimState, int, int> alphabetaSearch = AlphaBetaSearch <NimState, int, int> .createFor(game);

        NimState state   = game.getInitialState();
        int      action1 = -1;
        int      action2 = -1;

        action1 = minimaxSearch.makeDecision(state);
        action2 = alphabetaSearch.makeDecision(state);

        Debug.Log("Chosen action is " + action1 + " and node minimax " + minimaxSearch.getMetrics());
        Debug.Log("Chosen action is " + action2 + " and node alphabeta " + alphabetaSearch.getMetrics());
    }
示例#4
0
    private void Start()
    {
        TTT game = new TTT();
        MinimaxSearch <StateTTT, int, int> minimaxSearch = MinimaxSearch <StateTTT, int, int> .createFor(game);

        AlphaBetaSearch <StateTTT, int, int> alphabetaSearch = AlphaBetaSearch <StateTTT, int, int> .createFor(game);

        StateTTT state = game.getInitialState();

        int action1 = -100000;
        int action2 = -100000;

        action1 = minimaxSearch.makeDecision(state);
        action2 = alphabetaSearch.makeDecision(state);

        Debug.Log("Chosen action is " + action1 + " and node minimax " + minimaxSearch.getMetrics());
        Debug.Log("Chosen action is " + action2 + " and node alphabeta " + alphabetaSearch.getMetrics());
    }
示例#5
0
        static void Main(string[] args)
        {
            int maximumThinkingMilliseconds = -1; //Set to -1 to have it unlimited

            Console.WriteLine("Tic-Tac-Toe Artificial Intelligence\n");

            Game  game  = new Game();
            State state = game.GetInitialState();

            AdversarialSearch <State, tictactoe.Action> minimaxSearch = new MinimaxSearch <State, tictactoe.Action, Player>(game);
            //AdversarialSearch<State, tictactoe.Action> minimaxSearch = new MinimaxSearchLimited<State, tictactoe.Action, Player>(game, 2);
            //AdversarialSearch<State, tictactoe.Action> minimaxSearch = new IterativeDeepening<State, tictactoe.Action, Player>(game, IterativeDeepening<State, tictactoe.Action, Player>.Algorithm.Minimax);
            //AdversarialSearch<State, tictactoe.Action> minimaxSearch =
            //    new IterativeDeepening<State, tictactoe.Action, Player>(game, IterativeDeepening<State, tictactoe.Action, Player>.Algorithm.Minimax, maximumThinkingMilliseconds);

            String input;
            bool   first;

            do
            {
                Console.Write("Do you want to play first? (y/n): ");
                input = Console.ReadLine();
            } while (input.ToLower() != "y" && input.ToLower() != "n");
            first = input.ToLower() == "y";

            Player humanPlayer       = first ? Player.Cross : Player.Circle;
            String humanPlayerString = first ? "X" : "O";


            while (game.IsTerminal(state) == false)
            {
                if (first)
                {
                    Console.WriteLine("\n" + state.ToString());
                    Console.Write($"You are {humanPlayerString}, select an action (02 means row=0, col=2): ");
                    input = Console.ReadLine();
                    int row = Convert.ToInt32(input.Substring(0, 1));
                    int col = Convert.ToInt32(input.Substring(1, 1));

                    state = game.GetResult(state, new tictactoe.Action(row, col));

                    if (game.IsTerminal(state))
                    {
                        break;
                    }
                }
                first = true;

                Console.WriteLine("\n" + state.ToString());
                Console.WriteLine("AI is thinking about its next move...");


                var watch = System.Diagnostics.Stopwatch.StartNew();
                tictactoe.Action action = minimaxSearch.makeDecision(state);
                watch.Stop();

                Console.WriteLine($"Selected action: Row={action.Row}, Col={action.Col}");
                Console.WriteLine($"Expanded nodes: {minimaxSearch.getMetrics().Get(MinimaxSearch<State, tictactoe.Action, Player>.METRICS_NODES_EXPANDED)}");
                Console.WriteLine($"Execution Time: {watch.ElapsedMilliseconds} ms\n");


                state = game.GetResult(state, action);
            }

            Console.WriteLine("\n" + state.ToString());

            if (game.HasWon(state, humanPlayer))
            {
                Console.WriteLine("\nYOU WON, CONGRATULATIONS!!!");
            }
            else if (game.IsTerminal(state))
            {
                Console.WriteLine("\nAI won, try again!");
            }
            else
            {
                Console.WriteLine("\nIt's a tie, not bad!");
            }
        }