public MoveDirection GetMove(Grid grid, CatBot bot, CancellationToken cancellation) { TreeNode root = new TreeNode(grid, bot, _searchDepth, cancellation); var result = root.Traverse(new[] { bot.Position }, null); return(result.Direction); }
public TorpedoStrategy(Random random, Grid grid, CatBot bot) { _random = random; _bot = bot; _grid = grid; }
public TreeNode(Grid grid, CatBot bot, int maxDepth, CancellationToken cancellation) { _cancellation = cancellation; _bot = bot; _grid = grid; _depth = maxDepth; }
public MoveDirection GetMove(Grid grid, CatBot bot, CancellationToken cancellation) { MoveDirection move; do { if (cancellation.IsCancellationRequested) { return(MoveDirection.Surface); } switch (_random.Next(0, 4)) { case 0: move = MoveDirection.North; break; case 1: move = MoveDirection.South; break; case 2: move = MoveDirection.East; break; case 3: move = MoveDirection.West; break; default: throw new InvalidOperationException("This is bug 1"); } Console.Error.WriteLine($"Trying move {move}"); } while (!bot.IsValidMove(grid, move)); Console.Error.WriteLine($"Choosing move {move}"); return(move); }
static void Main(string[] args) { string[] inputs; inputs = Console.ReadLine().Split(' '); int width = int.Parse(inputs[0]); int height = int.Parse(inputs[1]); int myId = int.Parse(inputs[2]); string[] gridInput = new string[height]; for (int i = 0; i < height; i++) { gridInput[i] = Console.ReadLine(); } Grid grid = new Grid(new GridInputToBytes(), gridInput); StartLocator startLocator = new StartLocator(); var startLocation = startLocator.FindLocation(grid); CatBot catBot = new CatBot(startLocation); // Write an action using Console.WriteLine() // To debug: Console.Error.WriteLine("Debug messages..."); Console.WriteLine($"{startLocation.X} {startLocation.Y}"); // var moveStrategy = new RandomMoveStrategy(random); var moveStrategy = new GreatestOptionsMoveStrategy(6); var torpedoStrategy = new TorpedoStrategy(Random, grid, catBot); var knownLocationTargettingStrategy = new KnownLocationsTorpedoStrategy(grid, Random); var locatorStrategy = new BasicEnemyLocatorStrategy(grid); List <EnemyMove> enemyMoves = new List <EnemyMove>(); // game loop while (true) { Stopwatch globalTimer = new Stopwatch(); try { inputs = Console.ReadLine().Split(' '); globalTimer.Start(); int x = int.Parse(inputs[0]); int y = int.Parse(inputs[1]); int myLife = int.Parse(inputs[2]); int oppLife = int.Parse(inputs[3]); int torpedoCooldown = int.Parse(inputs[4]); int sonarCooldown = int.Parse(inputs[5]); int silenceCooldown = int.Parse(inputs[6]); int mineCooldown = int.Parse(inputs[7]); Console.Error.WriteLine($"Reading data {globalTimer.ElapsedMilliseconds}"); string sonarResult = Console.ReadLine(); string opponentOrders = Console.ReadLine(); Console.Error.WriteLine($"Starting turn at {globalTimer.ElapsedMilliseconds}"); var currentMove = new EnemyMove(opponentOrders); Console.Error.WriteLine($"Parsed move at {globalTimer.ElapsedMilliseconds}"); enemyMoves.Add(currentMove); if (currentMove.IsSilence) { Console.Error.WriteLine($"SILENCED {opponentOrders}"); enemyMoves.Clear(); } Console.Error.WriteLine($"Spotted moves: {string.Join(" ", enemyMoves.Where(m => m.IsMovement).Select(e => (char)e.Movement))} at {globalTimer.ElapsedMilliseconds}"); if (currentMove.HasSector) { Console.Error.WriteLine($"Enemy in sector {currentMove.Sector}"); } // Write an action using Console.WriteLine() // To debug: Console.Error.WriteLine("Debug messages...");s var timer = new Stopwatch(); timer.Start(); CancellationTokenSource locationCancellation = new CancellationTokenSource(10); var enemyLocations = locatorStrategy.LocateEnemy(locationCancellation.Token, enemyMoves.ToArray()); timer.Stop(); Console.Error.WriteLine($"Enemy locator took {timer.ElapsedMilliseconds}ms with {enemyLocations.Count()} possibilities at {globalTimer.ElapsedMilliseconds}"); if (enemyLocations.Count() == 1) { Console.Error.WriteLine($"ENEMY LOCATED AT {enemyLocations.Single()}"); } CancellationTokenSource cancellation = new CancellationTokenSource(5); Console.Error.WriteLine($"Starting movement at {globalTimer.ElapsedMilliseconds}"); var move = moveStrategy.GetMove(grid, catBot, cancellation.Token); List <string> orders = new List <string>(); if (silenceCooldown == 0 && move != MoveDirection.Surface) { orders.Add(move.ToSilence()); catBot.Move(grid, move); move = moveStrategy.GetMove(grid, catBot, cancellation.Token); } Console.Error.WriteLine($"Finishing movement at {globalTimer.ElapsedMilliseconds}"); catBot.Move(grid, move); string moveString = move.ToMove(); Console.Error.WriteLine($"TORP {torpedoCooldown}"); if (torpedoCooldown > 0) { moveString = $"{moveString} TORPEDO"; } else { var target = knownLocationTargettingStrategy.GetTarget(catBot.Position, enemyLocations); if (target != null) { orders.Add($"TORPEDO {target.Value.X} {target.Value.Y}"); } else if (silenceCooldown > 0) { moveString = $"{moveString} SILENCE"; } // var target = knownLocationTargettingStrategy.GetTarget(catBot.Position, enemyLocations) ?? // torpedoStrategy.GetTarget(); // torpedo = $"|TORPEDO {target.X} {target.Y}"; } // Console.Error.WriteLine($"Torpedo is {torpedo}"); orders.Add(moveString); Console.WriteLine(string.Join("|", orders)); } finally { Console.Error.WriteLine($"TIMER {globalTimer.ElapsedMilliseconds}"); } } }