private void BuildGame(int gameIndex) { _isBuilding = true; GameComboBox.IsEnabled = false; TurnComboBox.IsEnabled = false; PauseButton.IsEnabled = false; PreviousGameButton.IsEnabled = false; PreviousTurnButton.IsEnabled = false; NextGameButton.IsEnabled = false; NextTurnButton.IsEnabled = false; _persistentGameMain?.Dispose(); var random = _playingReplayGameData.Matches[gameIndex].GameRandom; _persistentGameMain = GameMain.Run(GameConfig, _playingReplayGameData.Matches[gameIndex], new Logger(), new[] { new AiLogger(), new AiLogger() }, true, GameControl, GameControl, GameControl.GraphicsDevice, true, new SpecialRand(random)); _persistentGameMain.BuildPersistentFieldCaches(); _isUser = false; var temp = new List <int>(); Console.Error.WriteLine(_persistentGameMain.TurnsCount + "built!"); Console.WriteLine(1); for (var i = 0; i < _persistentGameMain.TurnsCount; ++i) { temp.Add(i); } TurnComboBox.ItemsSource = temp; _isUser = true; }
private void Window_Closing(object sender, CancelEventArgs e) { try { _tokenSource?.Cancel(); _gameTask?.Wait(); _gameMain?.Dispose(); _visualizeWindow?.Dispose(); } catch (Exception exception) { Console.WriteLine(exception); } }
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); }
protected virtual void VisualizeWindow_OnClosing(object sender, CancelEventArgs e) { _persistentGameMain?.Dispose(); _persistentGameMain = null; GameMain?.Dispose(); GameMain = null; AiLoggers[0]?.Dispose(); AiLoggers[1]?.Dispose(); GameLogger?.Dispose(); if (_isForceClosing) { return; } e.Cancel = true; Hide(); }
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 StartGame(int gameIndex) { GameMain?.Dispose(); _paused = false; _currentMatch = gameIndex; var random = _playingReplayGameData.Matches[gameIndex].GameRandom; GameMain = GameMain.Run(GameConfig, _playingReplayGameData.Matches[gameIndex], GameLogger, AiLoggers, false, GameControl, GameControl, GameControl.GraphicsDevice, false, new SpecialRand(random)); GameComboBox.IsEnabled = true; TurnComboBox.IsEnabled = true; PauseButton.IsEnabled = true; PreviousGameButton.IsEnabled = true; PreviousTurnButton.IsEnabled = true; NextGameButton.IsEnabled = true; NextTurnButton.IsEnabled = true; _isBuilding = false; }
private void StopButton_Click(object sender, RoutedEventArgs e) { _gameMain?.Dispose(); _gameMain = null; }
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(); })); }