Skip to content

Instantly share code, notes, and snippets.

@barrybtw
Created September 26, 2022 09:46
Show Gist options
  • Save barrybtw/54c5e8612d6c2c404dfda358cae3747f to your computer and use it in GitHub Desktop.
Save barrybtw/54c5e8612d6c2c404dfda358cae3747f to your computer and use it in GitHub Desktop.
use rand::Rng;
use std::io;
fn main() {
let secret_number: i32 = rand::thread_rng().gen_range(1..=3);
println!("The secret number is: {}", secret_number);
let computer_choice: &str = if secret_number == 1 {
"rock"
} else if secret_number == 2 {
"paper"
} else {
"scissors"
};
println!("The computer chose: {}", computer_choice);
println!("What is your choice? (rock, paper, scissors)");
let mut user_choice: String = String::new();
io::stdin().read_line(&mut user_choice).expect("Failed to read line");
let user_choice: &str = user_choice.trim();
println!("You chose: {}", user_choice);
if user_choice == computer_choice {
println!("You tied!");
} else if user_choice == "rock" && computer_choice == "scissors" {
println!("You won!");
} else if user_choice == "paper" && computer_choice == "rock" {
println!("You won!");
} else if user_choice == "scissors" && computer_choice == "paper" {
println!("You won!");
} else {
println!("You lost!");
}
println!("Thanks for playing!");
// don't close the window
let mut close: String = String::new();
io::stdin().read_line(&mut close).expect("Failed to read line");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment