3.4.9 - Battleships
Third, and most critically for the user experience, is the . The elegance of 3.4.9 lies in its termination logic. The loop must continue switching turns (or alternating between human and AI attacks) until either player’s fleet has zero remaining unsunk cells. Implementing this requires a global variable or a method all_ships_sunk() that scans the board for any remaining "S" . A common student mistake is to check only for hits, not misses—leading to a game that never ends. Properly done, the loop demonstrates the power of boolean flags and state machines. Each turn displays the current board (hiding the opponent’s ship positions), prompts for input, validates it, executes the attack, and checks for a winner. The break condition upon victory ensures that the program exits cleanly with a congratulatory message.
In the landscape of introductory computer science exercises, few are as deceptively simple—or as instructionally rich—as building a Battleship game. The problem labeled “3.4.9 Battleships” is more than just a digital adaptation of a classic board game; it is a rigorous exercise in state management, user interaction, and algorithmic thinking. To successfully implement this project is to move beyond the abstract concept of a “grid” and to grapple with the concrete challenges of representing hidden information, validating user input, and constructing a logical flow for a two-player (or player-vs-AI) engagement. The essence of the 3.4.9 assignment lies in mastering three core pillars: the architecture of the board, the mechanics of attack validation, and the loop of game state until a win condition is met. 3.4.9 battleships
Second, the logical crux of the exercise is . In a physical game of Battleship, calling out “B4” is trivial. In code, the program must parse that input, convert column letters to indices (e.g., 'A' to 0), check bounds, and determine if that coordinate has been guessed before. The 3.4.9 specification often adds a further constraint: preventing the user from attacking the same cell twice. This forces the implementation of a secondary tracking mechanism, such as a guesses set or a separate hit_map . Moreover, when a ship is hit, the game must not only mark the cell as "X" but also check whether all cells of that specific ship are destroyed. This introduces the concept of aggregate state—tracking a ship’s health across multiple coordinates. Writing a function is_ship_sunk(row, col) that traces the connected components of a ship is a classic recursion or iteration challenge that distinguishes a passing grade from an excellent one. Third, and most critically for the user experience, is the
