Ejemplo n.º 1
0
        public void GetAgainDoesNotIncreaseCount()
        {
            BoardCache boardCache = new BoardCache(10);

            const int TestSize = 10;
            HexBoard[] usedBoards = new HexBoard[TestSize];

            // add some boards
            for (int i = 0; i < TestSize; i++)
            {
                usedBoards[i] = boardCache.GetBoard();
            }

            // remove them
            for (int i = 0; i < TestSize; i++)
            {
                boardCache.Release(usedBoards[i]);
            }

            // get again, count should not change since there are now boards ready to use
            for (int i = 0; i < TestSize; i++)
            {
                usedBoards[i] = boardCache.GetBoard();
                Assert.IsNotNull(usedBoards[i]);
                Assert.AreEqual(TestSize, boardCache.BoardCount);
            }
        }
Ejemplo n.º 2
0
        public void CreateIsEmptyCache()
        {
            BoardCache boardCache = new BoardCache(10);

            // empty cache
            Assert.IsNotNull(boardCache);
            Assert.AreEqual(0, boardCache.BoardCount);            
        }
Ejemplo n.º 3
0
        public Minimax(HexBoard board, GoodMoves goodMoves, ICandidateMoves candidateMovesFinder)
        {
            this.actualBoard          = board;
            this.goodMoves            = goodMoves;
            this.candidateMovesFinder = candidateMovesFinder;

            this.boardCache        = new BoardCache(board.Size);
            this.pathLengthFactory = new PathLengthAStarFactory();
        }
Ejemplo n.º 4
0
        public void AddBoardIncreasesCount()
        {
            BoardCache boardCache = new BoardCache(10);

            const int TestSize = 10;
            HexBoard[] usedBoards = new HexBoard[TestSize];

            // add some boards
            for (int i = 0; i < TestSize; i++)
            {
                usedBoards[i] = boardCache.GetBoard();
                Assert.IsNotNull(usedBoards[i]);
                Assert.AreEqual(i + 1, boardCache.BoardCount);
            }            
        }
Ejemplo n.º 5
0
        public void ReleaseBoardIncreasesCount()
        {
            BoardCache boardCache = new BoardCache(10);

            const int TestSize = 10;
            HexBoard[] usedBoards = new HexBoard[TestSize];

            // add some boards
            for (int i = 0; i < TestSize; i++)
            {
                usedBoards[i] = boardCache.GetBoard();
            }

            // remove them
            for (int i = 0; i < TestSize; i++)
            {
                boardCache.Release(usedBoards[i]);

                // count doesn't go down
                Assert.AreEqual(TestSize, boardCache.BoardCount);
                Assert.AreEqual(i + 1, boardCache.AvailableCount);
            }
        }
Ejemplo n.º 6
0
        private MinimaxResult MiniMaxAlg(int depth, bool isComputer, HexBoard board)
        {
            BoardCache    boardCache = new BoardCache(board.Size);
            MinimaxResult bestResult = null;
            Location      cutOffMove = Location.Null;
            // az ures cellakat tartalmazza
            var possibleMoves = candidateMovesFinder.CandidateMoves(board, depth);

            foreach (Location move in possibleMoves)
            {
                if (!move.IsNull())
                {
                    // nem az eredetit modositom meg, hanem letrehozok egyet a peldajara
                    HexBoard board1 = new HexBoard(board.Size);
                    board1.CopyStateFrom(board);
                    board1.PlayMove(move, isComputer);


                    // itt szamolja ki az allast
                    PathLengthBase staticAnalysis = this.pathLengthFactory.CreatePathLength(board1);
                    int            situationScore = staticAnalysis.SituationScore();
                    MinimaxResult  moveScore      = new MinimaxResult(situationScore);

                    if (depth <= 1 || MoveScoreConverter.IsWin(situationScore))
                    {
                        moveScore = new MinimaxResult(situationScore);
                    }
                    else
                    {
                        if (depth > 1)
                        {
                            // rekurzio
                            moveScore = MiniMaxAlg(depth--, !isComputer, board1);
                            moveScore.MoveWins();
                        }
                    }

                    moveScore.Move = move;
                    // Itt nezem meg, hogy a minimum kell nekunk, vagy a maximum
                    if (bestResult == null || MoveScoreConverter.MinOrMax(moveScore.Score, bestResult.Score, isComputer))
                    {
                        bestResult = new MinimaxResult(move, moveScore);
                    }

                    alpha = CheckAlpha(moveScore.Score, isComputer);
                    if (IsAlphaBetaCutoff(isComputer))
                    {
                        cutOffMove       = move;
                        bestResult.Score = alpha;
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
            if (bestResult != null)
            {
                GoodMoves.AddGoodMove(depth, bestResult.Move);
            }

            if (cutOffMove != Location.Null)
            {
                GoodMoves.AddGoodMove(depth, cutOffMove);
            }

            return(bestResult);
        }