public Task SlowPlayTurn(GameEventHub eventHub) { Debug.Assert(_tcs == null, "Starting a new turn while there's an existing TCS"); _tcs = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously); eventHub.State = GameEventState.TurnInProgress; return(_tcs.Task); }
public GameBoardRenderer(GameInstance gameInstance, GameBoardController gameBoardController, GameEventHub eventHub) { _gameInstance = gameInstance; _gameBoardController = gameBoardController; _eventHub = eventHub; _camera = Camera2D.Instance; }
/// <summary> /// Runs a playout with two given controllers and reports the result. /// </summary> public static PlayoutResult Playout(GameInstance game, IMobController ai1, IMobController ai2) { var hub = new GameEventHub(game); game.MobManager.Teams[TeamColor.Red] = ai1; game.MobManager.Teams[TeamColor.Blue] = ai2; const int maxIterations = 100; int i = 0; for (; i < maxIterations && !game.IsFinished; i++) { game.CurrentController.FastPlayTurn(hub); ActionEvaluator.FNoCopy(game, UctAction.EndTurnAction()); } float totalMaxHp = 0; float totalCurrentHp = 0; foreach (var mobId in game.MobManager.Mobs) { totalMaxHp += game.MobManager.MobInfos[mobId].MaxHp; totalCurrentHp += Math.Max(0, game.State.MobInstances[mobId].Hp); } int red = 0; int blue = 0; Utils.Log(LogSeverity.Error, nameof(GameEvaluator), $"Playout time limit reached at {maxIterations} rounds"); if (i < maxIterations && game.VictoryTeam.HasValue) { if (game.VictoryTeam.Value == TeamColor.Red) { red++; } else { blue++; } Accounting.IncrementWinner(game.VictoryController); } var gamePercentage = totalCurrentHp / totalMaxHp; Debug.Assert(gamePercentage >= 0); var mobsCount = game.MobManager.Mobs.Count; var dis = new Normal(mobsCount * 2, mobsCount); dis.Density(mobsCount * 2); return(new PlayoutResult(i, gamePercentage, game.State.AllPlayed, i == maxIterations, red, blue)); }
public void PlayerEndedTurn(GameEventHub eventHub) { Debug.Assert(_tcs != null, "PlayerController.TaskCompletionSource wasn't properly initialized."); var tcs = _tcs; eventHub.State = GameEventState.SettingUpTurn; _tcs = null; tcs.SetResult(true); }
public GameBoardController(GameInstance game, Entity crosshairCursor, ArenaScene arenaScene, Replay replay = null) { _game = game; _arenaScene = arenaScene; _crosshairCursor = crosshairCursor; _replay = replay; _eventHub = new GameEventHub(game); _eventHub.AddSubscriber(this); }
private static void PlayFirstTricksGuessesAndCards(GameEventHub originalHub) { originalHub.MoveToNextRound(); originalHub.StartCurrentRound(); originalHub.TryGiveGuess(0, 2); originalHub.TryGiveGuess(1, 3); originalHub.TryGiveGuess(2, 4); originalHub.TryPlayCard(0, ((KnownHand)originalHub.State.Players[0].Hand).First()); originalHub.TryPlayCard(1, ((KnownHand)originalHub.State.Players[1].Hand).First()); originalHub.TryPlayCard(2, ((KnownHand)originalHub.State.Players[2].Hand).First()); }
public async Task SlowPlayTurn(GameEventHub eventHub) { var result = await Task.Run(() => new UctAlgorithm(_thinkTime).UctSearch(_gameInstance)); foreach (var action in result.Actions) { Debug.Assert(action.Type != UctActionType.EndTurn, "node.Action.Type != UctActionType.EndTurn"); await eventHub.SlowPlayAction(_gameInstance, action); } LogActions(result); }
public void FastPlayTurn(GameEventHub eventHub) { do { var possibleActions = ActionGenerator.PossibleActions(_gameInstance, null, true, true); var chosenAction = possibleActions[Generator.Random.Next(possibleActions.Count)]; if (chosenAction.Type == UctActionType.EndTurn) { break; } ActionEvaluator.FNoCopy(_gameInstance, chosenAction); } while (!_gameInstance.IsFinished); }
public async Task SlowPlayTurn(GameEventHub eventHub) { do { var possibleActions = ActionGenerator.PossibleActions(_gameInstance, null, true, true); var chosenAction = possibleActions[Generator.Random.Next(possibleActions.Count)]; if (chosenAction.Type == UctActionType.EndTurn) { break; } await eventHub.SlowPlayAction(_gameInstance, chosenAction); } while (!_gameInstance.IsFinished); }
public void FastPlayTurn(GameEventHub eventHub) { var uct = new UctAlgorithm(_thinkTime, _expoExplo); var result = uct.UctSearch(_gameInstance); foreach (var action in result.Actions) { Debug.Assert(action.Type != UctActionType.EndTurn, "node.Action.Type != UctActionType.EndTurn"); ActionEvaluator.FNoCopy(_gameInstance, action); } ExponentialMovingAverage.Instance.Average(result.MillisecondsPerIteration); LogActions(result); }
public async Task SlowPlayTurn(GameEventHub eventHub) { do { var action = ActionGenerator.RuleBasedAction(_gameInstance); if (action.Type == UctActionType.EndTurn) { break; } await eventHub.SlowPlayAction(_gameInstance, action); if (action.Type == UctActionType.DefensiveMove) { break; } } while (!_gameInstance.IsFinished); }
public void FastPlayTurn(GameEventHub eventHub) { do { var action = ActionGenerator.RuleBasedAction(_gameInstance); if (action.Type == UctActionType.EndTurn) { break; } ActionEvaluator.FNoCopy(_gameInstance, action); if (action.Type == UctActionType.DefensiveMove) { break; } } while (!_gameInstance.IsFinished); }
public void ResumingTheFirstEventsLeadsToSameState() { var gameRoomDef = GenGameRoomDefinitionFor3Players(); var originalHub = new GameEventHub(gameRoomDef, GameProgress.NotStarted); // var forwardedHub = new GameEventHub("test", plan, 3); var originalHubObserver = new TransparentObserver(); PlayFirstTricksGuessesAndCards(originalHub); // First trick over! originalHub.MoveToNextRound(); originalHub.StartCurrentRound(); originalHub.TryGiveGuess(1, 3); originalHub.SubscribeWithPreviousEvents(originalHubObserver); var progress = new GameProgress( new GameSeriesEvent[] { new RoundStarted("", null), new RoundStarted("", null) }.ToImmutableList().WithValueSemantics(), originalHubObserver.ObservedEvents.ToImmutableList().WithValueSemantics()); var resumingHub = new GameEventHub(gameRoomDef, progress); Assert.Equal(originalHub.State, resumingHub.State); }
/// <summary> /// Runs a playout with the given encounter defined by a DNA pair and both controllers. /// </summary> public static int Playout(GameInstance game, DNA d1, DNA d2, IMobController c1, IMobController c2) { GameSetup.OverrideGameDna(game, d1, d2); game.AssignAiControllers(c1, c2); int iterations = Constants.MaxPlayoutEvaluationIterations; var hub = new GameEventHub(game); while (!game.IsFinished && iterations-- > 0) { game.CurrentController.FastPlayTurn(hub); ActionEvaluator.FNoCopy(game, UctAction.EndTurnAction()); } if (Constants.GetLogBuffer().ToString().Length != 0) { Console.WriteLine(Constants.GetLogBuffer()); } Constants.ResetLogBuffer(); return(Constants.MaxPlayoutEvaluationIterations - iterations); }
public Task SlowPlayTurn(GameEventHub eventHub) { throw new NotImplementedException(ErrorMessage); }
public Task <bool> PlayTurn(GameEventHub eventHub) { Debug.Assert(_tcs == null); _tcs = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously); return(_tcs.Task); }
public void FastPlayTurn(GameEventHub eventHub) { Debug.Assert(_tcs == null); _tcs = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously); _tcs.Task.Wait(Program.CancellationToken); }
public void FastPlayTurn(GameEventHub eventHub) { throw new NotImplementedException(ErrorMessage); }