From 9bc066d31ea5d456fc6d49fb771f414f7a1403f7 Mon Sep 17 00:00:00 2001 From: Jadi Date: Tue, 1 Aug 2023 20:59:04 +0330 Subject: [PATCH] Added Rust version of 57_Literature_Quiz --- 57_Literature_Quiz/rust/Cargo.toml | 8 ++ 57_Literature_Quiz/rust/README.md | 3 + 57_Literature_Quiz/rust/src/main.rs | 127 ++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 57_Literature_Quiz/rust/Cargo.toml create mode 100644 57_Literature_Quiz/rust/README.md create mode 100644 57_Literature_Quiz/rust/src/main.rs diff --git a/57_Literature_Quiz/rust/Cargo.toml b/57_Literature_Quiz/rust/Cargo.toml new file mode 100644 index 00000000..1ec69633 --- /dev/null +++ b/57_Literature_Quiz/rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "rust" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/57_Literature_Quiz/rust/README.md b/57_Literature_Quiz/rust/README.md new file mode 100644 index 00000000..fc6468b9 --- /dev/null +++ b/57_Literature_Quiz/rust/README.md @@ -0,0 +1,3 @@ +Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html) + +Conversion to [Rust](https://www.rust-lang.org/) diff --git a/57_Literature_Quiz/rust/src/main.rs b/57_Literature_Quiz/rust/src/main.rs new file mode 100644 index 00000000..a9e3923e --- /dev/null +++ b/57_Literature_Quiz/rust/src/main.rs @@ -0,0 +1,127 @@ +use std::io; + + +fn print_instructions() { + println!("TEST YOUR KNOWLEDGE OF CHILDREN'S LITERATURE."); + println!(); + println!("THIS IS A MULTIPLE-CHOICE QUIZ."); + println!("TYPE A 1, 2, 3, OR 4 AFTER THE QUESTION MARK."); + println!(); + println!("GOOD LUCK!"); + println!(); + println!(); +} + + +fn print_center(text: String, width: usize) { + let pad_size; + if width > text.len() { + pad_size = (width - text.len()) / 2; + } else { + pad_size = 0; + } + println!("{}{}", " ".repeat(pad_size), text); +} + + +fn print_results(score: usize, number_of_questions: usize) { + if score == number_of_questions { + println!("WOW! THAT'S SUPER! YOU REALLY KNOW YOUR NURSERY"); + println!("YOUR NEXT QUIZ WILL BE ON 2ND CENTURY CHINESE"); + println!("LITERATURE (HA, HA, HA)"); + } else if score < number_of_questions / 2 { + println!("UGH. THAT WAS DEFINITELY NOT TOO SWIFT. BACK TO"); + println!("NURSERY SCHOOL FOR YOU, MY FRIEND."); + } else { + println!("NOT BAD, BUT YOU MIGHT SPEND A LITTLE MORE TIME"); + println!("READING THE NURSERY GREATS."); + } +} + +fn main() { + let page_width: usize = 64; + + struct Question<'a> { + question: &'a str, + choices: Vec<&'a str>, + answer: u8, + correct_response: &'a str, + wrong_response: &'a str, + } + + impl Question<'_>{ + fn ask(&self) -> bool { + println!("{}", self.question); + for i in 0..4 { + print!("{}){}", i+1, self.choices[i]); + if i != 3 { print!(", ")}; + } + println!(""); + let mut user_input: String = String::new(); + io::stdin() + .read_line(&mut user_input) + .expect("Failed to read the line"); + + if user_input.starts_with(&self.answer.to_string()) { + println!("{}", self.correct_response); + true + } else { + println!("{}", self.wrong_response); + false + } + } + } + + let questions: Vec = vec![ + Question{ + question: "IN PINOCCHIO, WHAT WAS THE NAME OF THE CAT?", + choices: vec!["TIGGER", "CICERO", "FIGARO", "GUIPETTO"], + answer: 3, + wrong_response: "SORRY...FIGARO WAS HIS NAME.", + correct_response: "VERY GOOD! HERE'S ANOTHER.", + }, + Question{ + question: "FROM WHOSE GARDEN DID BUGS BUNNY STEAL THE CARROTS?", + choices: vec!["MR. NIXON'S", "ELMER FUDD'S", "CLEM JUDD'S", "STROMBOLI'S"], + answer: 2, + wrong_response: "TOO BAD...IT WAS ELMER FUDD'S GARDEN.", + correct_response: "PRETTY GOOD!", + }, + Question{ + question: "IN THE WIZARD OF OS, DOROTHY'S DOG WAS NAMED?", + choices: vec!["CICERO", "TRIXIA", "KING", "TOTO"], + answer: 4, + wrong_response: "BACK TO THE BOOKS,...TOTO WAS HIS NAME.", + correct_response: "YEA! YOU'RE A REAL LITERATURE GIANT.", + }, + Question{ + question: "WHO WAS THE FAIR MAIDEN WHO ATE THE POISON APPLE?", + choices: vec!["SLEEPING BEAUTY", "CINDERELLA", "SNOW WHITE", "WENDY"], + answer: 3, + wrong_response: "OH, COME ON NOW...IT WAS SNOW WHITE.", + correct_response: "GOOD MEMORY!", + }, + ]; + let number_of_questions: usize = questions.len(); + + print_center("LITERATURE QUIZ".to_string(), page_width); + print_center("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY".to_string(), page_width); + println!(); + println!(); + println!(); + print_instructions(); + + let mut score = 0; + for question in questions { + if question.ask() { + score += 1; + } + println!(); + } + + print_results(score, number_of_questions); + +} + + +