Giter VIP home page Giter VIP logo

rust-bship-game's Introduction

Battleship Game in Rust

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.

b

Table of Contents

Features

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

Installation

  1. Ensure you have Rust installed. If not, download and install it from rust-lang.org.
  2. Clone this repository:

Game Rules

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

File Structure

  • main.rs: Contains the main game logic and functions.
  • Cargo.toml: Project configuration file.

Methods

git clone 
cd battleship-rust
cargo build
cargo run

Board

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

main

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

get_player_input

  • Prompts the player for input coordinates.
  • Validates and returns the coordinates.

generate_opponent_move

  • Generates random coordinates for the opponent's move.

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any improvements or additions.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Thank you, guys and keep coding - stay safe and be well

rust-bship-game's People

Contributors

bekbrace avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.