simple_battleship

Crates.iosimple_battleship
lib.rssimple_battleship
version0.1.1
created_at2024-12-26 18:51:01.733721+00
updated_at2025-10-25 16:57:10.090419+00
descriptionA simple Battleship game implemented in Rust.
homepage
repositoryhttps://github.com/mzums/battleship
max_upload_size
id1495888
size64,284
(mzums)

documentation

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

  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) or use arrows in TUI mode.
    • 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

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 or simple_battleship --tui

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

Commit count: 14

cargo fmt