A classic text-based Battleship game implementation in Python.
This is a single-player Battleship game where you try to find and sink randomly placed ships on a 5x5 grid. The game features:
- Board Size: 5x5 grid
- Number of Ships: 3 ships to find
- Maximum Guesses: 15 attempts to sink all ships
- Visual Feedback:
Xmarks hitsOmarks misses- Ships are hidden until revealed
- Random ship placement for replayability
- Input validation for coordinates
- Clear visual board representation
- Game statistics tracking (guesses remaining, ships hit)
- Win/loss conditions
- Final board reveal showing all ship locations
-
Run the script:
python battleship_text_mode.py
-
The game will display a 5x5 grid with coordinates (0-4 for both rows and columns)
-
Enter your guess as two numbers separated by a space:
Enter coordinates (row col): 2 3 -
The game will tell you if you:
- HIT 💥: Successfully found a ship
- MISS 💧: No ship at that location
- Already guessed that location
⚠️
-
Continue guessing until you:
- WIN 🎉: Find all 3 ships within 15 guesses
- LOSE 💀: Run out of guesses before finding all ships
=== BATTLESHIP TEXT MODE ===
Find and sink all enemy ships!
Board size: 5x5
Ships to find: 3
Maximum guesses: 15
Guesses remaining: 15
Ships hit: 0/3
0 1 2 3 4
0
1
2
3
4
Enter coordinates (row col): 2 3
💥 HIT! You sunk a battleship!
The game consists of several key functions:
create_board(size): Creates an empty game boardplace_ships(board, num_ships): Randomly places ships on the boardprint_board(board, hide_ships): Displays the board with optional ship hidingget_guess(): Gets and validates player inputplay_game(): Main game loop
- Python 3.6+
- No external dependencies required
- Lines of Code: 100 lines (fits project requirements)
- Standard Library Only: Uses only
randommodule - Input Validation: Handles invalid inputs gracefully
- Game State Management: Tracks guesses, hits, and board state
This script was created as part of the 100 Lines of Python Code project. Feel free to suggest improvements while keeping the 100-line constraint!
Part of the 100LinesOfPythonCode repository.