Skip to content

Instantly share code, notes, and snippets.

@tonis2
Last active June 8, 2018 12:48
Show Gist options
  • Save tonis2/3b07ea8b0a1f30d0e30c3a0bb4112349 to your computer and use it in GitHub Desktop.
Save tonis2/3b07ea8b0a1f30d0e30c3a0bb4112349 to your computer and use it in GitHub Desktop.
rust rock, scissors, papers.
extern crate rand;
use rand::prelude::*;
use std::collections::HashMap;
use std::io::{stdin, stdout, Write};
fn main() {
let mut game_values: HashMap<&str, i32> = HashMap::new();
game_values.insert("kivi", 1);
game_values.insert("paber", 2);
game_values.insert("kaarid", 3);
println!("Viska kivi paber voi käärid ");
stdout().flush();
let mut valik = String::new();
stdin().read_line(&mut valik).expect("error");
let player_choice = game_values.get(valik.trim()).unwrap().to_owned();
let mut rng = thread_rng();
let computer_choice: i32 = rng.gen_range(1, 3);
let mut computer_choice_name: String = "tere".to_string();
for (key, value) in game_values {
if value == computer_choice {
computer_choice_name = key.to_string();
break;
}
}
let winner = match computer_choice {
1 => if player_choice == 1 {
"Viik"
} else if player_choice == 2 {
"Kaotasid"
} else {
"Voitsid"
},
2 => if player_choice == 1 {
"Voitsid"
} else if player_choice == 2 {
"Viik"
} else {
"Kaotasid"
},
3 => if player_choice == 1 {
"Kaotasid"
} else if player_choice == 2 {
"Voitsid"
} else {
"Viik"
},
_ => "Proovi uuesti",
};
println!("Tulemus {:?} {}", winner, player_choice);
println!("Arvuti viskas {:?} {}", computer_choice_name, computer_choice);
loop {
main();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment