Beispiel #1
0
        public virtual void Visualize(string replayFileName)
        {
            Activate();
            GameMain?.Dispose();

            _playingReplayGameData = new ReplayGameData();
            _playingReplayGameData.Load(replayFileName);

            var player1ReplayData = _playingReplayGameData.Matches[0].Player1ReplayData;
            var player1Name       = player1ReplayData.OutputLines[0];

            Player1NameTextBlock.Text = player1Name;

            var player2ReplayData = _playingReplayGameData.Matches[0].Player2ReplayData;
            var player2Name       = player2ReplayData.OutputLines[0];

            Player2NameTextBlock.Text = player2Name;

            GameConfig = _playingReplayGameData.Matches[0].GameConfig;

            var temp = new List <int>();

            for (var i = 0; i < _playingReplayGameData.MatchesCount; ++i)
            {
                temp.Add(i);
            }
            _isUser = false;
            GameComboBox.ItemsSource = temp;

            _currentMatch = 0;
            BuildGame(0);

            GameLogger = new Logger {
                EnableWritingConsole = true
            };

            AiLoggers[0] = new AiLogger();

            AiLoggers[1] = new AiLogger();

            StartGame(0);
        }
        private void RunGame()
        {
            var replayGameData = new ReplayGameData();

            var currentRandom = new SpecialRand((uint)GameConfig.RandomSeed);

            GameConfig.Player1WonCount = 0;
            GameConfig.Player2WonCount = 0;

            for (var i = 0; i < MatchesCount; ++i)
            {
                while (true)
                {
                    Thread.Sleep(30);
                    if (_token.IsCancellationRequested)
                    {
                        return;
                    }
                    _gameMain.Update();

                    if (_gameMain.CurrentState != GameMain.GameStateEnum.Running)
                    {
                        _gameMain.GameLogger.WriteLine(_gameMain.CurrentState.ToString());

                        _ai1AiLogger.WaitEvents();
                        _ai2AiLogger.WaitEvents();
                        _gameLogger.WaitEvent();

                        var aiPlayer1 = _gameMain.GetPlayer(0) as AiPlayer;
                        var aiPlayer2 = _gameMain.GetPlayer(1) as AiPlayer;

                        var player1Replay = new ReplayPlayerData {
                            LeftTimeOnLaunched = aiPlayer1.LeftTimeOnLaunched,
                            OutputLines        = aiPlayer1.AiOutputs,
                            LeftThinkTimes     = aiPlayer1.LeftThinkTimes
                        };

                        var player2Replay = new ReplayPlayerData {
                            LeftTimeOnLaunched = aiPlayer2.LeftTimeOnLaunched,
                            OutputLines        = aiPlayer2.AiOutputs,
                            LeftThinkTimes     = aiPlayer2.LeftThinkTimes
                        };

                        var match = new ReplayMatchData {
                            Player1ReplayData = player1Replay,
                            Player2ReplayData = player2Replay,
                            GameRandom        = currentRandom,
                            GameConfig        = new GameConfig(GameConfig)
                        };

                        replayGameData.Matches.Add(match);
                        currentRandom = new SpecialRand(_gameMain.FixedRandom);

                        Window.Dispatcher.BeginInvoke(new Action(() => {
                            System.Windows.Forms.Application.DoEvents();

                            if (_gameMain.CurrentState == GameMain.GameStateEnum.Player1Won)
                            {
                                GameConfig.Player1WonCount++;
                            }
                            else if (_gameMain.CurrentState == GameMain.GameStateEnum.Player2Won)
                            {
                                GameConfig.Player2WonCount++;
                            }

                            BattleProgressTextBox.AppendText("Player1 " + GameConfig.Player1WonCount +
                                                             "-" +
                                                             GameConfig.Player2WonCount +
                                                             " Player2\n");
                            BattleProgressTextBox.ScrollToEnd();
                        })).Wait();

                        _gameMain.Dispose();

                        if (i == MatchesCount - 1)
                        {
                            break;
                        }

                        var fileNames = new[]
                        { Player1FileName, Player2FileName };

                        _gameMain = GameMain.Run(GameConfig, fileNames, true, _gameLogger,
                                                 new[] { _ai1AiLogger, _ai2AiLogger }, _gameMain.FixedRandom);

                        break;
                    }
                }
            }

            var currentTime = DateTime.Now;

            var replayFileName = new StringBuilder();

            replayFileName.Append(_replayFileFirectory);
            replayFileName.Append(currentTime.Year);
            replayFileName.Append($"{currentTime.Month:D2}");
            replayFileName.Append($"{currentTime.Day:D2}_");
            replayFileName.Append($"{currentTime.Hour:D2}");
            replayFileName.Append($"{currentTime.Minute:D2}");
            replayFileName.Append($"{currentTime.Second:D2}_");
            replayFileName.Append(GameConfig.Player1WonCount + "-" + GameConfig.Player2WonCount +
                                  ".txt");

            replayGameData.Save(replayFileName.ToString());

            Window.Dispatcher.BeginInvoke(new Action(() => {
                if (_visualizeWindow == null)
                {
                    _visualizeWindow = new VisualizeWindow();
                }
                else
                {
                    _visualizeWindow.Close();
                }
                _visualizeWindow.Show();
                _visualizeWindow.Visualize(replayFileName.ToString());

                StopGame();
            }));
        }