Expand description
§Rock-Paper-Scissors Game Library
Welcome to the Rock-Paper-Scissors game library, a simple and flexible Rust-based crate for implementing the classic “Rock, Paper, Scissors” game. This crate is designed to simplify the development of both console-based and programmatic versions of the game, featuring customizable rules, user input handling, game settings, and score tracking utilities.
§Key Features
- Move Management: Enum-based moves (
Rock,Paper,Scissors) for safe and clear gameplay logic. - Game Logic: Easily determine winners for rounds and track scores across an entire game session.
- Settings and Customization: Includes prebuilt and customizable configurations, such as “first to X wins”.
- Error Handling: Built-in validations to ensure valid moves and game states.
- Randomization: Utilities for generating random enemy moves.
§Core Components
-
MoveType Enum Represents the possible game moves:
Rock,Paper, orScissors. Includes utilities for generating randomized moves and representing moves as strings. -
Winner Enum Encapsulates the result of each round, allowing easily distinguishable states:
User,Enemy, orTie. -
PlayerMoves Struct Captures the moves made by the user and the opponent in a single round. Provides functionality to determine the winner of that round.
-
Scores Struct Tracks the cumulative scores of a session and provides methods to check if there’s an overall winner based on predefined game settings.
-
GameSettings Struct Offers customizable configurations for game-winning conditions, such as “first to 3 wins” or other scenarios.
§Example Usage
Create and run a short game loop of “Rock, Paper, Scissors”:
use rock_paper_scissors::{PlayerMoves, Scores, Winner, GameSettings};
fn main() {
let mut scores = Scores::new();
let game_settings = GameSettings::from_first_to(3);
while scores.check_for_winner(&game_settings).is_err() {
let player_moves = PlayerMoves::build_from_input();
let round_winner = player_moves.check_who_wins_round();
match round_winner {
Winner::User => scores.user_wins += 1,
Winner::Enemy => scores.enemy_wins += 1,
Winner::Tie => println!("It's a tie!"),
}
println!("Scores -> User: {}, Enemy: {}", scores.user_wins, scores.enemy_wins);
}
let game_winner = scores.check_for_winner(&game_settings).unwrap();
println!("Game Winner: {}", game_winner.convert_to_string());
}§Crate Philosophy
This library focuses on simplicity and flexibility, making it perfect for both beginner and intermediate Rust developers learning game development. It provides clear interfaces and utilities while ensuring essential safeguards to avoid runtime errors.
§Contributing
Contributions such as bug fixing, feature additions, and code improvements are welcome! Please read the contribution guidelines for more details.
Structs§
- Game
Settings - GameSettings Struct
- Player
Moves - PlayerMoves Struct
- Scores
- Scores struct