Esempio 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);
        }
Esempio 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);
        }
Esempio 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");
        }
Esempio 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());
    }
Esempio 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());
    }
Esempio n. 6
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);
        }