diff --git a/74_Rock_Scissors_Paper/rust/Cargo.toml b/74_Rock_Scissors_Paper/rust/Cargo.toml new file mode 100644 index 00000000..5f4b4b85 --- /dev/null +++ b/74_Rock_Scissors_Paper/rust/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "rock_scissors_paper" +version = "1.0.0" +edition = "2021" + +[dependencies] +text_io = "0.1.10" +#num-integer = "0.1" +rand = "0.8.5" \ No newline at end of file diff --git a/74_Rock_Scissors_Paper/rust/README.md b/74_Rock_Scissors_Paper/rust/README.md new file mode 100644 index 00000000..50e88090 --- /dev/null +++ b/74_Rock_Scissors_Paper/rust/README.md @@ -0,0 +1,4 @@ +Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html) + +Conversion to [Rust](https://www.rust-lang.org) + diff --git a/74_Rock_Scissors_Paper/rust/src/main.rs b/74_Rock_Scissors_Paper/rust/src/main.rs new file mode 100644 index 00000000..e32f373e --- /dev/null +++ b/74_Rock_Scissors_Paper/rust/src/main.rs @@ -0,0 +1,96 @@ +/* + * Rock paper scissors + * Port from book _Basic Computer Games_ + * By David Lotts +*/ +use rand::Rng; +use std::io::{self, Write}; +use text_io::{try_read}; +fn main() { + let mut computer_wins = 0; + let mut human_wins = 0; + let mut rng = rand::thread_rng(); + println!("{:>21}", "GAME OF ROCK, SCISSORS, PAPER"); + println!("{:>15}", "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); + print!("\n\n\n"); + // pass by reference in rust! input() modifies num_games_q. + let mut qty_games = 0; + loop { + input_int("HOW MANY GAMES", &mut qty_games); + if qty_games < 11 { + break; + } + println!("SORRY, BUT WE AREN'T ALLOWED TO PLAY THAT MANY."); + } + for game_number in 1..=qty_games { + println!(); + println!("GAME NUMBER {}", game_number); + let my_choice: i32 = rng.gen_range(1..=3); // basic: X=INT(RND(1)*3+1) + let mut your_choice = 0; + loop { + println!("3=ROCK...2=SCISSORS...1=PAPER"); + input_int("1...2...3...WHAT'S YOUR CHOICE", &mut your_choice); + // interesting validation in original BASIC: IF (K-1)*(K-2)*(K-3)==0 + if (1..=3).contains(&your_choice) { + break; + } + println!("INVALID."); + } + println!("THIS IS MY CHOICE..."); + println!( + "...{}", + match my_choice { + 1 => "PAPER", + 2 => "SCISSORS", + 3 => "ROCK", + _ => "um, what?", + } + ); + if my_choice == your_choice { + //THEN 250 + println!("TIE GAME. NO WINNER."); + } else { + //if (my_choice == 1 && your_choice == 3) + // || (my_choice > your_choice) + if 1 == (3 + my_choice - your_choice) % 3 + { + println!("WOW! I WIN!!!"); + computer_wins = computer_wins + 1; + } else { + println!("YOU WIN!!!"); + human_wins = human_wins + 1; + } + } + } + println!(); + println!("HERE IS THE FINAL GAME SCORE:"); + println!("I HAVE WON {} GAME(S).", computer_wins); + println!("YOU HAVE WON {} GAME(S).", human_wins); + println!("AND {} GAME(S) ENDED IN A TIE.", qty_games - (computer_wins + human_wins)); + println!(); + println!("THANKS FOR PLAYING!!"); +} + +fn input_int(prompt: &str, number: &mut i32) { + loop { + print!("{} ? ", prompt); + io::stdout().flush().unwrap(); + match try_read!() { + Ok(n) => { + *number = n; + return; + } + Err(_) => println!("{}", prompt), + } + } +} + + +// TODO break out the winner decider and test it. +#[cfg(test)] +mod tests { + #[test] + fn winner_test() { + assert_eq!(true,true); + } +} \ No newline at end of file