/** * Battleships Agent - C# Implementation * Implements the full protocol with stateful ship placement. */ using System; using System.Collections.Generic; class BattleshipsAgent { // ========================================================================= // State // ========================================================================= static HashSet alreadyFired = new HashSet(); static readonly string[] shipsToPlace = { "PLACE C A1 N", // Carrier (5 cells) "PLACE B C1 N", // Battleship 1 (4 cells) "PLACE B E1 N", // Battleship 2 (4 cells) "PLACE D G1 N", // Destroyer 1 (3 cells) "PLACE D I1 N", // Destroyer 2 (3 cells) "PLACE P A7 D", // Patrol 1 (2 cells) "PLACE P A9 D", // Patrol 2 (2 cells) "PLACE P H8 N", // Patrol 3 (2 cells) }; static int currentShipIndex = 0; static bool preparationDone = false; static Random rng = new Random(); // ========================================================================= // Command Detection // ========================================================================= static bool StartsWith(string line, string prefix) { return line.StartsWith(prefix); } // ========================================================================= // Actions // ========================================================================= static void Send(string message) { Console.WriteLine(message); } static void PlaceNextShip() { if (currentShipIndex < shipsToPlace.Length) { Send(shipsToPlace[currentShipIndex]); } } static void FireRandom() { string pos; do { int row = rng.Next(1, 11); // 1-10 char rowChar = (char)('A' + row - 1); int col = rng.Next(1, 11); // 1-10 pos = $"{rowChar}{col}"; } while (alreadyFired.Contains(pos)); Send($"FIRE {pos}"); alreadyFired.Add(pos); } // ========================================================================= // Command Handlers // ========================================================================= static void HandleGameStarted() { currentShipIndex = 0; preparationDone = false; PlaceNextShip(); } static void HandleOk() { if (preparationDone) { return; } currentShipIndex++; if (currentShipIndex < shipsToPlace.Length) { PlaceNextShip(); } else if (currentShipIndex == shipsToPlace.Length) { Send("READY"); } else { preparationDone = true; } } static void HandleYourTurn() { FireRandom(); } static void HandleGameOver() { Environment.Exit(0); } static void HandleYouWon() { Environment.Exit(0); } static void HandleKo(string line) { Console.Error.WriteLine($"Error: {line}"); } // ========================================================================= // Main Loop // ========================================================================= static void ProcessLine(string line) { if (StartsWith(line, "GAME STARTED")) { HandleGameStarted(); } else if (StartsWith(line, "YOUR TURN")) { HandleYourTurn(); } else if (StartsWith(line, "GAME OVER")) { HandleGameOver(); } else if (StartsWith(line, "YOU WON")) { HandleYouWon(); } else if (StartsWith(line, "OK")) { HandleOk(); } else if (StartsWith(line, "KO")) { HandleKo(line); } } static void Main() { string? line; while ((line = Console.ReadLine()) != null) { ProcessLine(line); } } }