refactored

This commit is contained in:
Emma Tebibyte 2022-07-13 16:39:10 -04:00
parent f6337a13d0
commit bf2b0c51ab
2 changed files with 131 additions and 28 deletions

75
Cargo.lock generated Normal file
View File

@ -0,0 +1,75 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "bingame-rs"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "getrandom"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9be70c98951c83b8d2f8f60d7065fa6d5146873094452a1008da8c2f1e4205ad"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.125"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5916d2ae698f6de9bfb891ad7a8d65c09d232dc58cc4ac433c7da3b2fd84bc2b"
[[package]]
name = "ppv-lite86"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
"getrandom",
]
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"

View File

@ -1,21 +1,42 @@
use rand::Rng; use rand::Rng;
use std::cmp::Ordering; use std::cmp::Ordering;
fn parse_input() -> Option<u32> {
loop {
let mut input = String::new();
std::io::stdin()
.read_line(&mut input)
.unwrap();
match input.trim().parse::<u32>() {
Ok(output) => return Some(output),
Err(why) => {
match input.trim() {
"quit" | ":q" | "q" | "stop" | "exit" => { println!("Stopping..."); return None; }
_ => { println!("{} is not a number!", input); }
}
}
};
}
}
fn main() { fn main() {
println!("Welcome to bingame! Type \"quit\" anytime to stop playing."); println!("Welcome to bingame! Type \"quit\" anytime to stop playing.");
let mut liminput = String::new();
println!("Provide an upward limit for generated numbers: "); println!("Provide an upward limit for generated numbers: ");
std::io::stdin() let limit = parse_input().unwrap();
.read_line(&mut liminput) game(limit)
.unwrap();
let limit = liminput }
.trim()
.parse::<u32>() fn numgen(limit: u32) -> Result<(u32, u32), bool> {
.unwrap();
// generate random number // generate random number
@ -42,28 +63,35 @@ fn main() {
} }
let binary = binnery let binnery = binnery.parse::<u32>();
.parse::<u32>()
.unwrap();
println!("Type {} in binary", nombre); let binary = match binnery {
let mut input = String::new();
std::io::stdin() Ok(binary) => { binary },
.read_line(&mut input) Err(why) => { std::process::abort(); }
.unwrap(); };
let guess = input return Ok((nombre, binary));
.trim() }
.parse::<u32>()
.unwrap();
match guess.cmp(&binary) { fn game(limit: u32) {
Ordering::Equal => {
println!("That's correct! {} in binary is {}!", nombre, binnery); loop {
}
Ordering::Greater | Ordering::Less => { let ans = numgen(limit).unwrap();
println!("Nope! {} in binary is {}!", nombre, binnery); let nombre = ans.0;
let binary = ans.1;
println!("Type {} in binary", nombre);
let guess = parse_input().unwrap();
match guess.cmp(&binary) {
Ordering::Equal => {
println!("That's correct! {} in binary is {}!", nombre, binary);
}
Ordering::Greater | Ordering::Less => {
println!("Nope! {} in binary is {}!", nombre, binary);
}
} }
} }
} }