public async Task<RoundResult> PlayBotMovesAsync(int delayTime, int roundNumber) { var result = new RoundResult() { FinalResult = null, IsFinished = false, History = new List<RoundPartialHistory>() }; foreach (var dynaBlasterBot in _field.Bots.Where(bot => !bot.IsDead)) { BotMove move; try { move = await dynaBlasterBot.NextMoveAsync(GetBotBattlefieldInfo(dynaBlasterBot, roundNumber)); } catch (Exception e) { move = new BotMove() { Action = BotAction.None, Direction = null }; result.OutputText += string.Format("Bot {0} threw exception:\n{1}\n", dynaBlasterBot.Name, e.Message); } if (IsMoveValid(dynaBlasterBot, move)) { result.History.Add(PerformMove(dynaBlasterBot, move, roundNumber)); _field.OnArenaChanged(); } await DelayHelper.DelayAsync(delayTime); } return result; }
private RoundPartialHistory PerformMove(TankBlasterBot bot, BotMove move, int roundNumber) { var actionDescription = move.Direction != null ? "move " + move.Direction.Value : "stay"; if (move.Action == BotAction.DropBomb) { _field.Bombs.Add(new Bomb { Location = bot.Location, RoundsUntilExplodes = 5, ExplosionRadius = CurrentBombBlastRadius(roundNumber) }); actionDescription += " & drop bomb"; } bot.Location = _locationService.GetNewLocation(bot.Location, move.Direction); bot.LastDirection = move.Direction ?? bot.LastDirection; if (move.Action == BotAction.FireMissile) { if (IsMissileAvailable(bot, roundNumber) && _locationService.IsLocationAvailableForMissile(_locationService.GetNewLocation(bot.Location, move.FireDirection))) { bot.LastMissileFiredRound = roundNumber; _field.Missiles.Add(new Missile { ExplosionRadius = CurrentMissileBlastRadius(roundNumber), MoveDirection = move.FireDirection, Location = _locationService.GetNewLocation(bot.Location, move.FireDirection) }); actionDescription += " & fire " + move.FireDirection; } else { actionDescription += " & can't fire " + move.FireDirection; } } return new RoundPartialHistory { Caption = string.Format("Round {0} {1}: {2}", roundNumber, bot.Name, actionDescription), BoardState = _field.ExportState() }; }
private bool IsMoveValid(TankBlasterBot bot, BotMove move) { var newLocation = _locationService.GetNewLocation(bot.Location, move.Direction); return _locationService.IsLocationValid(newLocation) && _field.Board[newLocation.X, newLocation.Y] == BoardTile.Empty && !_field.Bots.Any(blasterBot => blasterBot.Id != bot.Id && blasterBot.Location == newLocation); }