Exemplo n.º 1
0
        public FileVerificationResult Verify(string progressText, int maxMoves)
        {
            var movesAsString = ParseProgressText.FromFileText(progressText);

            if (!movesAsString.Any())
            {
                return(FileVerificationResult.EmptyOrInvalidFile);
            }

            var moves = movesAsString.Select(MoveParser.GetMove);

            if ((int)Math.Ceiling(moves.Count() / 2.0) >= maxMoves)
            {
                return(FileVerificationResult.FileContainsMoreMovesThanAllowed);
            }

            var topPlayer    = new Player(PlayerType.TopPlayer);
            var bottomPlayer = new Player(PlayerType.BottomPlayer);

            var boardState = BoardStateTransition.CreateInitialBoadState(topPlayer, bottomPlayer);

            foreach (var move in moves)
            {
                if (move is Capitulation)
                {
                    return(FileVerificationResult.FileContainsTerminatedGame);
                }

                if (!GameAnalysis.IsMoveLegal(boardState, move))
                {
                    return(FileVerificationResult.FileContainsInvalidMove);
                }

                boardState = boardState.ApplyMove(move);

                var winner = GameAnalysis.CheckWinningCondition(boardState);
                if (winner != null)
                {
                    return(FileVerificationResult.FileContainsTerminatedGame);
                }
            }

            return(FileVerificationResult.ValidFile);
        }
Exemplo n.º 2
0
        internal LocalGameBvB(IQuoridorBot uninitializedTopPlayerBot,
                              IQuoridorBot uninitializedBottomPlayerBot,
                              GameConstraints gameConstraints)
        {
            var topPlayer    = new Player(PlayerType.TopPlayer);
            var bottomPlayer = new Player(PlayerType.BottomPlayer);

            topPlayerBot = uninitializedTopPlayerBot;
            topPlayerBot.Init(topPlayer.PlayerType, gameConstraints);
            topPlayerBot.DebugMessageAvailable += OnTopPlayerBotDebugMessageAvailable;

            bottomPlayerBot = uninitializedBottomPlayerBot;
            bottomPlayerBot.Init(bottomPlayer.PlayerType, gameConstraints);
            bottomPlayerBot.DebugMessageAvailable += OnBottomPlayerBotDebugMessageAvailable;

            var initialBoadState = BoardStateTransition.CreateInitialBoadState(topPlayer, bottomPlayer);

            gameLoopThreadBvB = new GameLoopThreadBvB(bottomPlayerBot, topPlayerBot, initialBoadState, gameConstraints);

            gameLoopThreadBvB.NewBoardStateAvailable += OnNewBoardStateAvailable;
            gameLoopThreadBvB.WinnerAvailable        += OnWinnerAvailable;

            new Thread(gameLoopThreadBvB.Run).Start();
        }
Exemplo n.º 3
0
        private void BuildReplayStates(IEnumerable <string> allMoves)
        {
            allReplayStates = new List <BoardState>();


            var topPlayer    = new Player(PlayerType.TopPlayer);
            var bottomPlayer = new Player(PlayerType.BottomPlayer);

            var boardState = BoardStateTransition.CreateInitialBoadState(topPlayer, bottomPlayer);

            allReplayStates.Add(boardState);

            var playerAtMove = bottomPlayer;

            foreach (var move in allMoves)
            {
                var nextMove = MoveParser.GetMove(move);

                boardState = boardState.ApplyMove(nextMove);
                allReplayStates.Add(boardState);

                playerAtMove = playerAtMove == topPlayer ? bottomPlayer : topPlayer;
            }
        }
Exemplo n.º 4
0
        public void Run()
        {
            IsRunning = true;

            computerPlayer = new Player(PlayerType.TopPlayer, botName);
            humanPlayer    = new Player(PlayerType.BottomPlayer);

            bot.Init(computerPlayer.PlayerType, gameConstraints);

            currentBoardState = BoardStateTransition.CreateInitialBoadState(computerPlayer, humanPlayer);
            NewBoardStateAvailable?.Invoke(currentBoardState);

            var moveCounter = 0;

            if (initialProgress != null)
            {
                var moves = ParseProgressText.FromFileText(initialProgress)
                            .Select(MoveParser.GetMove);

                foreach (var move in moves)
                {
                    currentBoardState = currentBoardState.ApplyMove(move);
                    NewBoardStateAvailable?.Invoke(currentBoardState);
                }

                if (moves.Count() % 2 == 1)
                {
                    var succeedGame = DoBotMove();

                    if (!succeedGame)
                    {
                        IsRunning              = false;
                        bot.NextMoveAvailable -= OnNextBotMoveAvailable;
                        return;
                    }
                }

                moveCounter = (int)Math.Ceiling(moves.Count() / 2.0);
            }

            while (!stopRunning)
            {
                if (moveCounter >= gameConstraints.MaximalMovesPerPlayer)
                {
                    WinnerAvailable?.Invoke(computerPlayer, WinningReason.ExceedanceOfMaxMoves, null);
                }

                bool succeedGame;

                succeedGame = DoHumanMove();
                if (!succeedGame)
                {
                    break;
                }


                succeedGame = DoBotMove();
                if (!succeedGame)
                {
                    break;
                }

                moveCounter++;
            }

            IsRunning              = false;
            bot.NextMoveAvailable -= OnNextBotMoveAvailable;
        }