예제 #1
0
        public void FiveStepsAwayFromMaxWinning_MaxTurn_MaxWin(int degreeOfParallelism, ParallelismMode parallelismMode, CacheMode cacheMode)
        {
            var startState = Connect4TestUtils.GetMaxFiveMovesAwayFromWinningState();

            var engine     = Connect4TestUtils.GetSearchEngine(degreeOfParallelism, parallelismMode, 1, CacheMode.ReuseCache);
            var evaluation = engine.Search(startState, 5);

            Assert.AreEqual(BoardEvaluator.MaxEvaluation, evaluation.Evaluation);
            Assert.IsTrue(BoardEvaluator.IsWin(((Connect4State)evaluation.StateSequence.Last()).Board, Player.Max), "Should have found a wining state");
            Assert.IsTrue(evaluation.AllChildrenAreDeadEnds, "All children should be dead ends");
        }
예제 #2
0
        public IEnumerable <Tuple <double, IEnumerable <IState> > > GetNeighbors()
        {
            if (BoardEvaluator.IsWin(connect4State.Board, Turn.GetReversePlayer()))
            {
                return(new List <Tuple <double, IEnumerable <IState> > >());
            }

            var result = new List <Tuple <double, IEnumerable <IState> > >();

            for (int i = 0; i < Connect4State.BoardSize; i++)
            {
                result.Add(new Tuple <double, IEnumerable <IState> >(1.0 / Connect4State.BoardSize, GetNeighborsFrom(i)));
            }

            return(result);
        }
예제 #3
0
        public void ThreeStepsAwayFromMaxWinning_MaxTurn_MaxWin(int degreeOfParallelism, ParallelismMode parallelismMode)
        {
            var startState = new Connect4State(new[, ]
            {
                { Player.Empty, Player.Empty, Player.Empty, Player.Max, Player.Max, Player.Empty },
                { Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty },
                { Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty },
                { Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty },
                { Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty },
                { Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty },
            }, Player.Max);

            var engine     = Connect4TestUtils.GetSearchEngine(degreeOfParallelism, parallelismMode);
            var evaluation = engine.Search(startState, 3);

            Assert.IsTrue(BoardEvaluator.IsWin(((Connect4State)evaluation.StateSequence.Last()).Board, Player.Max), "Should have found a wining state");
        }
예제 #4
0
        public void OneStepAwayFromMaxWinning_MaxTurn_MaxWin(int degreeOfParallelism, ParallelismMode parallelismMode)
        {
            var startState = new Connect4State(new[, ]
            {
                { Player.Empty, Player.Max, Player.Empty, Player.Empty, Player.Empty, Player.Empty },
                { Player.Empty, Player.Max, Player.Empty, Player.Empty, Player.Empty, Player.Empty },
                { Player.Empty, Player.Max, Player.Empty, Player.Empty, Player.Empty, Player.Empty },
                { Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty },
                { Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty },
                { Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty, Player.Empty },
            }, Player.Max);

            var engine     = Connect4TestUtils.GetSearchEngine(degreeOfParallelism, parallelismMode);
            var evaluation = engine.Search(startState, 2);

            Assert.IsTrue(BoardEvaluator.IsWin(((Connect4State)evaluation.NextMove).Board, Player.Max),
                          "Should have found a wining state");
            Assert.AreEqual(1, evaluation.StateSequence.Count, "StateSequence should only have one state in it");
        }
예제 #5
0
        public IEnumerable <IState> GetNeighbors()
        {
            if (BoardEvaluator.IsWin(Board, Turn.GetReversePlayer()))
            {
                return(new List <IDeterministicState>());
            }

            var result = new List <Connect4State>();

            for (int i = 0; i < BoardSize; i++)
            {
                var newState = AddPieceTo(i);
                if (newState != null)
                {
                    result.Add(newState);
                }
            }

            return(result);
        }
예제 #6
0
        public void NewGame_NoOneCanWin(int degreeOfParallelism, ParallelismMode parallelismMode)
        {
            var startState = new Connect4State(Connect4TestUtils.GetEmptyBoard(), Player.Max);

            var engine     = Connect4TestUtils.GetSearchEngine(degreeOfParallelism, parallelismMode);
            var evaluation = engine.Search(startState, 7);

            Assert.IsFalse(BoardEvaluator.IsWin(((Connect4State)evaluation.StateSequence.Last()).Board, Player.Max));
            Assert.IsFalse(evaluation.FullTreeSearchedOrPruned);
            Assert.IsFalse(evaluation.AllChildrenAreDeadEnds);

            if (degreeOfParallelism == 1)
            {
                //Check that the our optimizations are working
                Assert.IsTrue(evaluation.Leaves < 26000, "Too many leaves in search. Leaves = " + evaluation.Leaves);
                Assert.IsTrue(evaluation.InternalNodes < 10000,
                              "Too many intarnal nodes in search. Nodes = " + evaluation.InternalNodes);
            }
            // Too few leaves or internal nodes means that something went wrong
            Assert.IsTrue(evaluation.Leaves > 1000, "Too few leaves in search. Leaves = " + evaluation.Leaves);
            Assert.IsTrue(evaluation.InternalNodes > 1000, "Too few intarnal nodes in search. Nodes = " + evaluation.InternalNodes);
        }