This is a simple implementation of the classic Battleship game in Rust, showcasing the use of Rust's standard library for I/O handling and random number generation. The game features a player and an opponent, each with their own game board, and allows for basic gameplay including placing ships, firing at the opponent's board, and checking for game over conditions.
- Random ship placement ensuring no overlap or out-of-bounds positioning.
- Basic user input for firing at coordinates.
- Display of game boards with different symbols for hits, misses, and ships.
- Simple game loop with turn-based gameplay.
- Detection of game over conditions.
- Ensure you have Rust installed. If not, download and install it from rust-lang.org.
- Clone this repository:
- Each player has a 10x10 board.
- Ships of different sizes (5, 4, 3, 3, 2) are placed randomly on the board.
- Players take turns firing at each other's board by entering coordinates.
- A hit is marked with a red dot (โ) and a miss with a blue dot (ยท).
- The game continues until all ships of one player are sunk.
main.rs
: Contains the main game logic and functions.Cargo.toml
: Project configuration file.
git clone
cd battleship-rust
cargo build
cargo run
new() -> Self
: Initializes a new game board with all cells empty.place_ship(&mut self, size: usize)
: Randomly places a ship of the given size on the board.can_place_ship(&self, row: usize, col: usize, size: usize, direction: bool) -> bool
: Checks if a ship can be placed at the specified location.fire(&mut self, row: usize, col: usize) -> CellState
: Fires at the specified cell, updating its state.display(&self, hide_ships: bool)
: Displays the game board, optionally hiding the ships.is_game_over(&self) -> bool
: Checks if all ships have been hit.
- Initializes player and opponent boards.
- Places ships on both boards.
- Handles the game loop, including player and opponent turns and checking for game over conditions.
- Prompts the player for input coordinates.
- Validates and returns the coordinates.
- Generates random coordinates for the opponent's move.
Contributions are welcome! Please open an issue or submit a pull request for any improvements or additions.
This project is licensed under the MIT License. See the LICENSE file for details.