#game #battleship #cli #turn

app simple_battleship

A simple Battleship game implemented in Rust

1 unstable release

new 0.1.0 Dec 26, 2024

#34 in Games

Download history 129/week @ 2024-12-23

129 downloads per month

MIT license

19KB
287 lines

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).

🎮 How to Play

  1. Objective: Destroy all enemy ships before they destroy yours.
  2. Turn-Based Gameplay:
    • You make your move by selecting a coordinate to attack (e.g., A5).
    • The AI then makes its move, with a smart strategy for targeting ships.
  3. 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:

alt text

<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

  1. Run in your shell:
    cargo install simple_battleship
  2. Once installed you can play the game by running:
    simple_battleship

🛠️ Setup Instructions

  1. Install Rust from Rust's official website.
  2. Clone this repository:
    git clone https://github.com/mzums/battleship
    cd battleship
    
  3. Build and run the game:
    cargo run
    

🎮 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.

Install dependencies with:

cargo build

🚒 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.

Dependencies

~2.5–9.5MB
~94K SLoC