Welcome to the Battleship game implemented in Rust for the terminal! This game features a pass-and-play multiplayer mode and a single-player mode against a computer opponent. The computer opponent comes with three difficulty levels: easy, medium, and hard. The difficulty levels vary in their attack strategies, from random guessing to more advanced probabilistic calculations.
-
Clone the Repository:
git clone https://github.com/cqb13/terminal-battleship.git
-
Navigate to the Project Directory:
cd terminal-battleship
-
Compile and Run:
cargo run
-
Follow the On-screen Instructions:
- Choose between single-player and pass-and-play multiplayer.
- Select the difficulty level for the computer opponent (if playing single-player).
- Enjoy the classic Battleship game experience!
Note: Might not work on windows
⊕
: Targeted⊗
: Already Attacked🅇
: Hit⓪
: Miss•
: Unknown▧
: Carrier#
: Battleship▭
: Cruiser or Submarine△
: Destroyer
- Single Player:
- Face off against the computer with different difficulty levels.
- Multiplayer:
- Pass and play with a friend.
-
Easy Difficulty:
- Computer makes random guesses.
-
Medium Difficulty:
- Computer uses a hunt-and-target strategy to find and sink ships.
-
Hard Difficulty:
- Computer employs probability-based attacks to target the most likely spaces.
display
: Module for displaying game-related information.game
: Module containing game logic and player/computer interactions.utils
: Utility module for common functionalities.
-
Game Board:
- The game board is represented as a 2D array of
Tile
enums within theGameBoard
struct. - Each
Tile
enum represents different states of a cell, such asTargeted
,AlreadyAttacked
,Hit
,Miss
,Unknown
, orShip
.
- The game board is represented as a 2D array of
-
Attack Strategies:
- The computer's attack strategy is encapsulated within the
Computer
struct, utilizing various attack strategies based on the difficulty level. - The attack strategies include:
RandomAttackStrategy
: Randomly selects attack positions.HuntAndTargetAttackStrategy
: Seeks out nearby ship positions after a hit.ProbabilityAttackStrategy
: Uses probability calculations for optimal attacks.
- The computer's attack strategy is encapsulated within the
-
Player and Turns:
- Players are represented by the
Player
enum, with valuesPlayerOne
andPlayerTwo
. - Turn management is implemented in the
multiplayer_game
function, where players take alternating turns.
- Players are represented by the
-
Game Logic:
- The core game logic is implemented in the
singleplayer_game
andmultiplayer_game
functions. - These functions handle the setup, turns, and win conditions for both single-player and multiplayer modes.
- The core game logic is implemented in the
-
Ships and Ship Types:
- Ships are represented by the
Ship
struct, containing aShipType
enum and orientation information. - The
ShipType
enum defines ship names, sizes, and symbols for display.
- Ships are represented by the
-
Position and Board Management:
- The
Position
struct represents a 2D position on the game board. - The
GameBoard
struct manages the overall game board, providing methods for placing markers and checking game state.
- The
-
Constants:
GRID_SIZE
: A constant indicating the size of the game board.GRID_ARRAY_SIZE
: A constant representing the array size for the game board.
-
Game Modes and Difficulty Levels:
- The
GameMode
enum distinguishes between single-player and multiplayer modes. - The
Difficulty
enum defines difficulty levels for the single-player game.
- The
Feel free to contribute to the project by opening issues, suggesting improvements, or adding new features. Pull requests are welcome!
Enjoy playing Battleship in Rust! 🚢🔥