Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
        static void startMinimaxDemo()
        {
            System.Console.WriteLine("MINI MAX DEMO\n");
            TicTacToeGame  game      = new TicTacToeGame();
            TicTacToeState currState = game.GetInitialState();
            IAdversarialSearch <TicTacToeState, XYLocation>
            search = MinimaxSearch <TicTacToeState, XYLocation, string> .createFor(game);

            while (!(game.IsTerminal(currState)))
            {
                System.Console.WriteLine(game.GetPlayer(currState) + "  playing ... ");
                XYLocation action = search.makeDecision(currState);
                currState = game.GetResult(currState, action);
                System.Console.WriteLine(currState);
            }
            System.Console.WriteLine("MINI MAX DEMO done");
        }
Ejemplo n.º 4
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());
    }
Ejemplo n.º 5
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());
    }
Ejemplo n.º 6
0
    //Ran on loop to check if AI is ready to move
    IEnumerator startAI()
    {
        routineStarted = true;
        System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
        stopwatch.Start();
        Move m = null;

        if ((mainBoard.gameType == Board.GameType.tvs && mainBoard.turn == Board.Turn.black) ||
            (mainBoard.gameType == Board.GameType.hvs && mainBoard.turn == Board.Turn.black))
        {
            MinimaxSearch.board = mainBoard;
            m = MinimaxSearch.minimaxStart();
            mainBoard.makeMove(m.getPiece(), m.getCell(), m.getJumped());
            checkWin();
            stopwatch.Stop();
            Debug.Log("Time taken for search:  " + (stopwatch.Elapsed.Milliseconds));
            stopwatch.Reset();
        }
        else if ((mainBoard.gameType == Board.GameType.tvs && mainBoard.turn == Board.Turn.white) ||
                 (mainBoard.gameType == Board.GameType.hvt && mainBoard.turn == Board.Turn.white))
        {
            MinimaxTactical.board = mainBoard;
            m = MinimaxTactical.minimaxStart();
            mainBoard.makeMove(m.getPiece(), m.getCell(), m.getJumped());
            checkWin();
            stopwatch.Stop();
            Debug.Log("Time taken for tactical:  " + (stopwatch.Elapsed.Milliseconds));
            stopwatch.Reset();
        }

        Board[] mg = FindObjectsOfType <Board>();
        foreach (Board mgg in mg)
        {
            if (mgg.thisHeurusic == 0 && mgg != mainBoard)
            {
                Destroy(mgg.gameObject);
            }
        }
        yield return(new WaitForSeconds(0.3f));

        routineStarted = false;
    }
Ejemplo n.º 7
0
        public List <GameMoveResult> MakeMoves(Game objGame, object state, int movesNumber)
        {
            var toReturn = new List <GameMoveResult>();
            AdversarialSearch objGameStrategy;

            switch (StrategyType)
            {
            case GameStrategyType.AlphaBeta:
                objGameStrategy = AlphaBetaSearch.createFor(objGame);
                break;

            case GameStrategyType.IterativeDeepeningAlphaBeta:
                objGameStrategy = IterativeDeepeningAlphaBetaSearch.createFor(objGame, MinUtility, MaxUtility, MaxDurationSeconds);
                break;

            case GameStrategyType.ConnectFourIDAlphaBeta:
                objGameStrategy = new ConnectFourAIPlayer(objGame, MaxDurationSeconds);
                break;

            default:
                objGameStrategy = MinimaxSearch.createFor(objGame);
                break;
            }
            int counter = 0;

            while (!objGame.isTerminal(state) && counter < movesNumber)
            {
                var action  = objGameStrategy.makeDecision(state);
                var newMove = new GameMoveResult()
                {
                    Game = objGame, InitialState = state, Action = action, Metrics = objGameStrategy.getMetrics()
                };
                toReturn.Add(newMove);
                state = newMove.ResultState;
                counter++;
            }

            return(toReturn);
        }
Ejemplo n.º 8
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!");
            }
        }