Example #1
0
        private void StartGame()
        {
            GameMain?.Dispose();
            GameConfig.RandomSeed = _random.Next();
            var random = new SpecialRand((uint)GameConfig.RandomSeed);

            var fileNames = new[] { _aiProperties[0].FileName, _aiProperties[1].FileName };

            GameMain = GameMain.Run(GameConfig, fileNames, false, GameLogger, AiLoggers, random, GameControl,
                                    GameControl, GameControl.GraphicsDevice);
            _bgm = GameMain.Content.Load <Song>("music");
            MediaPlayer.IsRepeating = true;
            MediaPlayer.Play(_bgm);
        }
        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();
            }));
        }