Esempio n. 1
0
        private void DoLoadGame()
        {
            if (string.IsNullOrWhiteSpace(ProgressFilePath))
            {
                MessageBox.Show("bevor das Replay gestartet werden kann muss eine Replay-Datei ausgewählt werden");
                return;
            }

            if (!File.Exists(ProgressFilePath))
            {
                MessageBox.Show($"die datei {ProgressFilePath} existiert nicht");
                return;
            }

            string fileText;

            try
            {
                fileText = File.ReadAllText(ProgressFilePath);
            }
            catch
            {
                MessageBox.Show($"die datei {ProgressFilePath} kann nicht als text geladen werden");
                return;
            }

            var splittedMoves = ParseProgressText.FromFileText(fileText);

            if (!splittedMoves.Any())
            {
                MessageBox.Show("die datei beschreibt keinen gültigen spielverlauf");
                return;
            }


            lastPlayedReplayService.SaveLastReplay(ProgressFilePath);

            ProgressRows.Clear();

            CreateProgressText.FromMoveList(splittedMoves.ToList())
            .Select(line => new ProgressRow(line))
            .Do(ProgressRows.Add);

            var moveCount = replayService.NewReplay(splittedMoves);

            moveIndex    = 0;
            MaxMoveIndex = moveCount - 1;

            PropertyChanged.Notify(this, nameof(MoveIndex));
        }
Esempio n. 2
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);
        }
Esempio n. 3
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;
        }