コード例 #1
0
        public void TestProvided_BENreturned()
        {
            var expected  = "ben";
            var evaluator = new TestCase1.Evaluator();
            var generator = new TestCase1.Generator();
            var applier   = new TestCase1.Applier();
            var algorithm = new AlphaBetaAlgorithm <TestCase1.State, TestCase1.Move>(evaluator, generator, applier);

            algorithm.MaxDepth = int.MaxValue;
            var sb = new StringBuilder();

            var state = new TestCase1.State(1, 0);
            var move  = algorithm.Calculate(state);

            sb.Append(move.Label);

            state = applier.Apply(state, move);
            move  = algorithm.Calculate(state);
            sb.Append(move.Label);

            state = applier.Apply(state, move);
            move  = algorithm.Calculate(state);
            sb.Append(move.Label);

            var actual = sb.ToString();

            Assert.Equal(expected, actual);
        }
コード例 #2
0
ファイル: EqualityTests.cs プロジェクト: poseen/ChessBotArena
        public void MaxLevel1(int depth)
        {
            var evaluator = new TestCase1.Evaluator();
            var generator = new TestCase1.Generator();
            var applier   = new TestCase1.Applier();

            var algorithm1 = new MinimaxAlgorithm <TestCase1.State, TestCase1.Move>(evaluator, generator, applier)
            {
                MaxDepth = depth
            };
            var algorithm2 = new MinimaxAverageAlgorithm <TestCase1.State, TestCase1.Move>(evaluator, generator, applier)
            {
                MaxDepth             = depth,
                MaxLevelAverageDepth = 1,
                MinLevelAverageDepth = 1
            };
            var algorithm3 = new AlphaBetaAlgorithm <TestCase1.State, TestCase1.Move>(evaluator, generator, applier)
            {
                MaxDepth = 1
            };

            var state = new TestCase1.State(0, 0);

            var move1 = algorithm1.Calculate(state);
            var move2 = algorithm2.Calculate(state);
            var move3 = algorithm3.Calculate(state);

            Assert.Equal(move1, move2);
            Assert.Equal(move2, move3);
            Assert.Equal(move3, move1);
        }
コード例 #3
0
ファイル: MainForm.cs プロジェクト: poseen/ChessBotArena
        private IAlgorithm <ChessRepresentation, BaseMove> GetAlgorithm(Type algorithmType, Type evaluatorType)
        {
            var generator = new MoveGenerator(_mechanism);
            var applier   = new MoveApplier(_mechanism);
            var evaluator = (IEvaluator <ChessRepresentation>)Activator.CreateInstance(evaluatorType, new[] { _mechanism });

            // TODO : A bit hacky, refactor later!
            if (algorithmType == typeof(MinimaxAlgorithm <ChessRepresentation, BaseMove>))
            {
                var minimax = new MinimaxAlgorithm <ChessRepresentation, BaseMove>(evaluator, generator, applier)
                {
                    MaxDepth = (int)numericUpDown1.Value
                };
                return(minimax);
            }

            if (algorithmType == typeof(AlphaBetaAlgorithm <ChessRepresentation, BaseMove>))
            {
                var ab = new AlphaBetaAlgorithm <ChessRepresentation, BaseMove>(evaluator, generator, applier)
                {
                    MaxDepth = (int)numericUpDown1.Value
                };
                return(ab);
            }

            if (algorithmType == typeof(MinimaxAverageAlgorithm <ChessRepresentation, BaseMove>))
            {
                var minimaxAvg = new MinimaxAverageAlgorithm <ChessRepresentation, BaseMove>(evaluator, generator, applier)
                {
                    MaxDepth = (int)numericUpDown1.Value
                };
                return(minimaxAvg);
            }

            if (algorithmType == typeof(GreedyAlgorithm <ChessRepresentation, BaseMove>))
            {
                var greedy = new GreedyAlgorithm <ChessRepresentation, BaseMove>(evaluator, generator, applier);
                return(greedy);
            }

            if (algorithmType == typeof(RandomAlgorithm <ChessRepresentation, BaseMove>))
            {
                var randomAlgorithm = new RandomAlgorithm <ChessRepresentation, BaseMove>(generator);
                return(randomAlgorithm);
            }

            throw new ArgumentOutOfRangeException(nameof(algorithmType));
        }
コード例 #4
0
    public void startGame(int boardSize, int depth, int player1, int player2, int algorithm1, int algorithm2, int boardHeuristic1, int boardHeuristic2, int nodeHeuristic1, int nodeHeuristic2)
    {
        this.boardSize       = boardSize;
        gameBoard            = new int[boardSize, boardSize];
        this.depth           = depth;
        currentPlayer        = 0;
        isPlayer1AI          = player1 == 1;
        isPlayer2AI          = player2 == 1;
        proceedAutomatically = !isPlayer1AI || !isPlayer2AI;

        if (isPlayer1AI)
        {
            Algorithm player1Algorithm;
            if (algorithm1 == 0)
            {
                player1Algorithm = new MinimaxAlgorithm();
            }
            else
            {
                player1Algorithm = new AlphaBetaAlgorithm();
            }
            BoardStateEvaluationHeuristic player1BoardStateEvaluationHeuristic;
            if (boardHeuristic1 == 0)
            {
                player1BoardStateEvaluationHeuristic = new OwnAdjacentSymbolsBoardStateEvaluationHeuristic();
            }
            else if (boardHeuristic1 == 1)
            {
                player1BoardStateEvaluationHeuristic = new NoAdjacentOpponentSymbolsBoardStateEvaluationHeuristic();
            }
            else
            {
                player1BoardStateEvaluationHeuristic = new OwnSymbolsInCompletedLinesBoardStateEvaluationHeuristic();
            }
            NodeSelectionHeuristic player1NodeSelectionHeuristic;
            if (nodeHeuristic1 == 0)
            {
                player1NodeSelectionHeuristic = null;
            }
            else if (nodeHeuristic1 == 1)
            {
                player1NodeSelectionHeuristic = new CentralNodeSelectionHeuristic();
            }
            else
            {
                player1NodeSelectionHeuristic = new BorderNodeSelectionHeuristic();
            }
            player1Strategy = new Strategy(player1Algorithm, player1BoardStateEvaluationHeuristic, player1NodeSelectionHeuristic);
        }
        if (isPlayer2AI)
        {
            Algorithm player2Algorithm;
            if (algorithm2 == 0)
            {
                player2Algorithm = new MinimaxAlgorithm();
            }
            else
            {
                player2Algorithm = new AlphaBetaAlgorithm();
            }
            BoardStateEvaluationHeuristic player2BoardStateEvaluationHeuristic;
            if (boardHeuristic2 == 0)
            {
                player2BoardStateEvaluationHeuristic = new OwnAdjacentSymbolsBoardStateEvaluationHeuristic();
            }
            else if (boardHeuristic2 == 1)
            {
                player2BoardStateEvaluationHeuristic = new NoAdjacentOpponentSymbolsBoardStateEvaluationHeuristic();
            }
            else
            {
                player2BoardStateEvaluationHeuristic = new OwnSymbolsInCompletedLinesBoardStateEvaluationHeuristic();
            }
            NodeSelectionHeuristic player2NodeSelectionHeuristic;
            if (nodeHeuristic2 == 0)
            {
                player2NodeSelectionHeuristic = null;
            }
            else if (nodeHeuristic2 == 1)
            {
                player2NodeSelectionHeuristic = new CentralNodeSelectionHeuristic();
            }
            else
            {
                player2NodeSelectionHeuristic = new BorderNodeSelectionHeuristic();
            }
            player2Strategy = new Strategy(player2Algorithm, player2BoardStateEvaluationHeuristic, player2NodeSelectionHeuristic);
        }
        player1Score = 0;
        player2Score = 0;
        freeFields   = boardSize * boardSize;
        nextMove     = null;
        stopwatch    = new Stopwatch();
        turnNumber   = 0;
        startNewTurn();
        UIController.instance.setCooldownOnRestartButton();
    }