A classic two-player naval strategy game implemented in Python where you play against an AI opponent. Place your ships strategically and try to sink the AI's fleet before it sinks yours!
- Command-line interface with clear visual representation
- Intelligent AI opponent that:
- Uses strategic targeting after successful hits
- Never repeats moves
- Properly tracks ship damage
- Complete ship placement system
- Real-time game status updates
- Input validation and error handling
- Python 3.x
- NumPy library
- Clone the repository:
git clone https://github.com/yourusername/battleship.git
cd battleship- Install required dependencies:
pip install numpy- Run the game:
python main.py- Carrier (5 spaces)
- Battleship (4 spaces)
- Destroyer (3 spaces)
- Submarine (3 spaces)
- Patrol Boat (2 spaces)
-
Start by placing your ships on the 10x10 grid
- Choose ship type (1-5)
- Select orientation (1 for horizontal, 2 for vertical)
- Enter x,y coordinates (0-9)
-
Taking Turns
- Players alternate turns
- Enter coordinates to attack (x,y)
- The game shows if it's a hit or miss
- Ships can't be hit twice in the same spot
.: Empty spaceS: Your shipH: HitM: Miss
battleship/
├── main.py # Game initialization and main loop
├── board.py # Board class and display logic
├── ship.py # Ship class and ship creation
├── player.py # Human player logic
└── computer_player.py # AI opponent logic
- Random targeting for initial attacks
- Smart targeting system that focuses on adjacent cells after a hit
- Tracks all previous moves to avoid repetition
- Maintains a queue of potential target coordinates
- Ensures all coordinates are within bounds
- Prevents overlapping ship placement
- Validates user input format
Feel free to fork the repository and submit pull requests. You can also open issues for bugs or feature suggestions.
Planned features:
- Multiple difficulty levels for AI
- Scoring system
- Game statistics tracking
- Save/Load game functionality
- Sound effects
- Graphical user interface
- Inspired by the classic Battleship board game
- Thanks to the Python and NumPy communities