Installing the command-line executable
Assuming you have Rust/Cargo installed , run this command in a terminal:
cargo install simple_battleship
It will make the simple_battleship command available in your PATH if you've allowed the PATH to be modified when installing Rust . cargo uninstall simple_battleship uninstalls.
Back to the crate overview .
Readme
Battleship Game in Rust
Welcome to the Battleship Game implemented in Rust! Engage in a thrilling game of strategy and skill against an AI opponent on the high seas. Sink your opponent's fleet before they sink yours!
Features
Interactive Gameplay : Command your fleet and take turns attacking your opponent's ships.
Dynamic Opponent : The AI makes decisions based on a heatmap and hit tracking.
Visual Feedback : Colored output to indicate hits, misses, and your ships' status.
Automatic Ship Placement : Ships are randomly placed on the grid, ensuring a unique experience each time.
Player-Friendly Input : Intuitive position input system using coordinates (e.g., A1, J10).
CLI or TUI : Choose between two UI modes
How to Play
Objective : Destroy all enemy ships before they destroy yours.
Turn-Based Gameplay :
You make your move by selecting a coordinate to attack (e.g., A5) or use arrows in TUI mode.
The AI then makes its move, with a smart strategy for targeting ships.
Win Condition : The game ends when all ships of either player are sunk.
Board Representation
The board is a 10x10 grid labeled with letters (A-J) for columns and numbers (0-9) for rows.
Your board and the opponent's board are displayed side by side:
<style>
r { color: Red }
b { color: Blue }
g { color: Green }
</style>
Symbols:
: Empty cell
#: Missed shot
#: Hit shot
X : Visible ship (your ship or ship sunk by you)
Install from crates.io
Run in your shell:
cargo install simple_battleship
Once installed you can play the game by running:
simple_battleship
or simple_battleship - - tui
Setup Instructions
Install Rust from Rust's official website .
Clone this repository:
git clone https://github.com/mzums/battleship
cd battleship
Build and run the game:
cargo run
tui mode:
cargo run -- --tui
Game Logic
Ship Placement :
Ships of varying lengths (5, 4, 3, 3, 2) are randomly placed on the board without overlap or adjacency.
AI Strategy :
The AI utilizes a heatmap to determine optimal moves and targets ships intelligently by continuing hits on partially discovered ships.
Player Input :
Input coordinates in the format [ Letter] [ Digit] (e.g., A1). Invalid inputs prompt re-entry.
Future Enhancements
Multiplayer support.
Customizable board sizes and ship configurations.
Dependencies
rand : For generating random numbers.
regex : For validating player input.
colored : For colorful terminal output.
Contributing
Contributions are welcome! Feel free to submit issues or pull requests to improve the game.
License
This project is licensed under the MIT License. See the LICENSE file for details.