Overview
This project implements a classic Rock Paper Scissors game as an interactive command-line application. The game allows players to compete against the computer in unlimited rounds, with the computer making random choices using therand crate.
This project demonstrates fundamental Rust concepts including user input handling, random number generation, pattern matching, and control flow.
What You’ll Learn
- Reading and parsing user input with
std::io - Generating random numbers with the
randcrate - Using loops for continuous gameplay
- Pattern matching with
matchexpressions - Error handling with
unwrap_or_else - String manipulation and comparison
Running the Game
Example Gameplay
Dependencies
The project uses therand crate for generating random computer choices:
Cargo.toml
Code Architecture
Main Game Loop
The game runs in an infinite loop, continuously prompting for user input until the player types “exit”:Understanding the Main Loop
Understanding the Main Loop
Key components:
loopcreates an infinite loop that continues untilbreakis calledrand::random_range(1..=3)generates a random number between 1 and 3 (inclusive)io::stdin().read_line()reads user input from the terminaltrim().to_lowercase()cleans the input by removing whitespace and converting to lowercaseparse().unwrap_or_else()attempts to parse the string as an integer, defaulting to 0 on error- The game checks for “exit” to break out of the loop
Checking Values and Displaying Results
Thecheck_values function determines the game outcome and formats the result message:
The
match expression is used to map numeric values to their corresponding emojis and names. The underscore _ pattern handles any unexpected values.Game Logic
Thecheck_game function implements the Rock Paper Scissors rules:
- Rock (1) beats Scissors (3)
- Scissors (3) beats Paper (2)
- Paper (2) beats Rock (1)
Rust Concepts Demonstrated
Key Takeaways
Infinite Loops with Exit Conditions
The
loop keyword creates an infinite loop, and the break statement exits when the user types “exit”.Input Validation
Using
unwrap_or_else provides graceful error handling when parsing user input, defaulting to 0 for invalid entries.Pattern Matching
The
match expression provides a clean way to handle multiple conditions and map values to results.Reference Borrowing
Functions use references (
&i8) to borrow values without taking ownership, allowing the main function to retain access.Possible Enhancements
Track Win/Loss Statistics
Track Win/Loss Statistics
Add counters to track the number of wins, losses, and draws throughout the game session.
Add Input Validation
Add Input Validation
Improve error handling to re-prompt the user when invalid input is entered instead of defaulting to 0.
Support Text Input
Support Text Input
Allow players to type “rock”, “paper”, or “scissors” instead of just numbers.
Add Best-of-N Rounds
Add Best-of-N Rounds
Implement a tournament mode where the player competes in a best-of-3 or best-of-5 series.
Next Steps
After completing this project, you can:- Add more complex game modes (best-of-3, tournaments)
- Implement score tracking and statistics
- Create a Rock Paper Scissors Lizard Spock variant
- Build a graphical interface using a crate like
icedoregui - Add multiplayer support over a network
This project provides a solid foundation in Rust basics. The concepts learned here—input handling, random generation, and pattern matching—are essential for more complex applications.