rock-paper-scissors
rock-paper-scissors is an open-source Rust library and interactive game designed for developers to create or customize implementations of the classic "Rock, Paper, Scissors" game. It adheres to clean design principles, offering modular functionality, safe initialization, and robust error handling.
Features
Library Highlights
- Customizable Game Logic:
- Enums and structs encapsulate game logic for modularity and easy customization.
- New
MoveType::Nonevariant ensures safe initialization for moves. - Methods like
MoveType::random_movesimplify randomization, whileMoveType::from_user_inputhandles inputs gracefully.
- Winner Determination:
- Built-in logic determines the winner of each round with clear rules (
PlayerMoves::check_who_wins_round).
- Built-in logic determines the winner of each round with clear rules (
- Score Management:
- Tracks user and enemy wins during the game with a
Scoresstruct. - Allows checking the game's end (
Scores::check_for_winner) or resetting (Scores::reset).
- Tracks user and enemy wins during the game with a
- Safe Initialization:
- The
PlayerMoves::newmethod initializes moves (user_moveandenemy_move) asMoveType::None, reducing errors during uninitialized states.
- The
- Game Settings:
- The
GameSettingsstruct allows developers to configure customizable game-winning conditions, such as "first to X wins." - A prebuilt configuration
GameSettings::first_to_3()is included for quick usage.
- The
- Friendly Output Utilities:
- Human-readable string conversion for enums like
MoveTypeandWinnerprovides better readability and interaction.
- Human-readable string conversion for enums like
Game Highlights
- Interactive gameplay through
MoveType::from_user_input()for user moves. - Head-to-head matches with a randomly generated opponent.
- Input validation ensures smooth, error-free gameplay.
Getting Started
Prerequisites
To use or play the rock-paper-scissors library, ensure you have the following installed:
- Rust (v1.64 or higher)
- Cargo, for building and running the library or game.
Installation
To use the rock-paper-scissors library in your project, include it in your Cargo.toml:
[]
= "0.4.1"
For the interactive game:
- Clone the repository:
- Run the game using Cargo:
Library Overview
Core Components
Enums
- Winner
- Represents round outcomes:
User,Enemy, orTie. - Includes
Winner::convert_to_stringfor clear textual output.
Example output:"You win!"or"You lose!".
- MoveType
- Represents available moves (
Rock,Paper,Scissors) and includes a new variant:MoveType::None: Represents an uninitialized state or invalid move, ensuring safe initialization.
- Key methods:
MoveType::random_move: Generates a random move for non-interactive gameplay.MoveType::from_user_input: Validates and converts user input to move types.MoveType::convert_to_string: Converts moves (likeMoveType::Rock) into a human-readable format ("Rock").
Structs
- PlayerMoves
- Bundles the user's and the enemy's moves for a single round.
- Features:
PlayerMoves::new: Initializes moves asMoveType::Nonefor safe usage.PlayerMoves::check_who_wins_round: Determines the round winner.PlayerMoves::build: Completes initialization with user input and a random enemy move.
- Scores
- Tracks the cumulative wins for both the user and the enemy.
- Features:
Scores::check_for_winner: Determines if a player has reached the game's win condition (e.g., first to a certain number of wins).Scores::reset: Resets scores for a new game.
- GameSettings
- Facilitates customizable game-winning conditions.
- Features:
first_tofield: Specifies the number of wins required to end the game.GameSettings::new: Creates aGameSettingsinstance with default configurable values.GameSettings::first_to_3: Predefined configuration for a game set to "first to 3 wins."
Example:
use ;
let game_settings = first_to_3;
let mut scores = new;
// Simulate some rounds
scores.user_wins = 3;
// Check for game winner
let winner = scores.check_for_winner;
assert_eq!;
Playing the Game
Here’s an interactive example of how to play a game of Rock-Paper-Scissors using the library:
use ;
Error Handling
Key Error Scenarios
- Invalid user input:
- If the player enters invalid input (e.g., letters, out-of-range numbers), the game reprompts them via
MoveType::from_user_input.
- If the player enters invalid input (e.g., letters, out-of-range numbers), the game reprompts them via
- Uninitialized states:
- The
MoveType::Nonevariant prevents invalid states during custom logic or gameplay setup.
- The
Contributing
We welcome contributions! To contribute:
- Fork this repository.
- Create a new branch for your feature or bug fix.
- Push your changes and open a Pull Request.
For feature requests or issues, please open a GitHub issue.
License
This project is distributed under the MIT License. See the LICENSE file for details.