A Python implementation of the classic Battleship game, demonstrating different architectural approaches. This project features:
- A command-line interface.
- Two distinct game logic implementations:
- A direct, synchronous version.
- An event-driven version showcasing a more decoupled architecture.
- A simple AI opponent.
- Configurable board sizes and ship fleets.
- Unit tests for core components and game logic.
src/: Contains the main source code.battleship.py: Entry point for the direct (synchronous) version of the game.battleship_events.py: Entry point for the event-driven version of the game.common/: Core data structures likeGrid,Pos, andShipused by both game versions.sea_battle/: Modules specific to the direct version of the game (Board, Player, Enums).sea_battle_event/: Modules specific to the event-driven version (Board, Player, Config, EventManager, GameManager, Enums, Protocols).
tests/: Contains unit tests for the project, organized to mirror thesrc/structure.
- Classic Battleship Gameplay: Place your ships and try to sink your opponent's fleet.
- Two Game Modes:
- Direct Mode (
battleship.py): A straightforward implementation where game components interact directly. - Event-Driven Mode (
battleship_events.py): A more advanced implementation using an event manager to decouple game logic, making components more modular and reactive.
- Direct Mode (
- Simple AI: Play against a basic AI that makes random valid guesses, with some logic to hunt for ships after a hit.
- Configurable Games:
- The direct version (
battleship.py) can be configured by modifying parameters directly in itsmain()function (e.g., board size, ship list). - The event-driven version (
battleship_events.py) uses aGameConfigobject for setup.
- The direct version (
- Tested Core Logic: Includes a suite of
pytestunit tests to ensure the reliability of game mechanics.
-
Clone the repository:
git clone https://github.com/NightSoma/battleship/ cd battleship -
Set up a virtual environment (recommended):
python3 -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate
-
Install dependencies:
pip install .If you also want to run tests or use development tools:
pip install ".[test,dev]" -
Run the game:
-
Direct Version:
python src/battleship.py
-
Event-Driven Version:
python src/battleship_events.py
(Note: Both versions currently print game state and results to the console. The direct version simulates a large game by default for performance observation, while the event-driven version runs a smaller, standard game.)
-
To run the unit tests:
pytestTo include coverage reporting (ensure pytest-cov is installed):
pytest --cov=src- Object-Oriented Programming (OOP) in Python.
- Implementation of core game logic for Battleship.
- Comparison of direct vs. event-driven software architectures.
- Basic AI development for game opponents.
- Unit testing with
pytest. - Project setup with
pyproject.tomland dependency management. - Use of Python data classes and enums for clarity and structure.