fixed formatting and reduced size

This commit is contained in:
Emma Tebibyte 2023-01-03 20:36:05 -05:00
parent bf2b0c51ab
commit e059bd667f
4 changed files with 78 additions and 81 deletions

View File

2
Cargo.lock generated
View File

@ -3,7 +3,7 @@
version = 3 version = 3
[[package]] [[package]]
name = "bingame-rs" name = "bingame"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"rand", "rand",

View File

@ -1,10 +1,7 @@
[package] [package]
name = "bingame-rs" name = "bingame"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
rand = "0.8.4" rand = "0.8.4"

View File

@ -1,97 +1,97 @@
// Copyright (c) 2022 Emma Tebibyte
// SPDX-License-Identifier: AGPL-3.0-or-later
/* Bingame is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Bingame is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
use rand::Rng; use rand::Rng;
use std::cmp::Ordering; use std::cmp::Ordering;
fn parse_input() -> Option<u32> { fn parse_input() -> Option<u32> {
loop { loop {
let mut input = String::new(); let mut input = String::new();
std::io::stdin() std::io::stdin()
.read_line(&mut input) .read_line(&mut input)
.unwrap(); .unwrap();
match input.trim().parse::<u32>() { match input.trim().parse::<u32>() {
Ok(output) => return Some(output), Ok(output) => return Some(output),
Err(_) => {
Err(why) => {
match input.trim() { match input.trim() {
"quit" | ":q" | "q" | "stop" | "exit" => {
"quit" | ":q" | "q" | "stop" | "exit" => { println!("Stopping..."); return None; } println!("Stopping...");
_ => { println!("{} is not a number!", input); } return None;
},
} _ => eprintln!("{} is not a number!", input),
} };
},
}; };
} }
} }
fn main() { fn main() {
println!("Welcome to bingame! Type \"quit\" anytime to stop playing.
Provide an upward limit for generated numbers:");
println!("Welcome to bingame! Type \"quit\" anytime to stop playing."); let limit = match parse_input() {
println!("Provide an upward limit for generated numbers: "); Some(limit) => limit,
let limit = parse_input().unwrap(); None => std::process::exit(0),
};
game(limit) game(limit)
} }
fn numgen(limit: u32) -> Result<(u32, u32), bool> { fn numgen(limit: u32) -> Result<(u32, u32), bool> {
// generate random number // generate random number
let nombre: u32 = rand::thread_rng().gen_range(0..limit); let nombre: u32 = rand::thread_rng().gen_range(0..limit);
// convert number from decimal to binary notation // convert number from decimal to binary notation
let mut binvec = vec![]; let mut binvec = vec![];
let mut countdown = nombre; let mut countdown = nombre;
while countdown > 0 { while countdown > 0 {
binvec.push(countdown % 2); binvec.push(countdown % 2);
countdown /= 2; countdown /= 2;
} }
let mut binnery = String::new(); let mut binnery = String::new();
for i in binvec { binnery = i.to_string() + &binnery; }
for i in binvec {
binnery = i.to_string() + &binnery;
}
let binnery = binnery.parse::<u32>(); let binnery = binnery.parse::<u32>();
let binary = match binnery { let binary = match binnery {
Ok(binary) => binary,
Ok(binary) => { binary }, Err(_) => std::process::abort(),
Err(why) => { std::process::abort(); }
}; };
return Ok((nombre, binary)); return Ok((nombre, binary));
} }
fn game(limit: u32) { fn game(limit: u32) {
loop { loop {
let ans = numgen(limit).unwrap(); let ans = numgen(limit).unwrap();
let nombre = ans.0; println!("Type {} in binary", ans.0);
let binary = ans.1;
println!("Type {} in binary", nombre); let guess = match parse_input() {
let guess = parse_input().unwrap(); Some(guess) => guess,
None => std::process::exit(0),
};
match guess.cmp(&binary) { match guess.cmp(&ans.1) {
Ordering::Equal => { Ordering::Equal => {
println!("That's correct! {} in binary is {}!", nombre, binary); println!("That's correct! {} in binary is {}!", ans.0, ans.1);
} },
Ordering::Greater | Ordering::Less => { Ordering::Greater | Ordering::Less => {
println!("Nope! {} in binary is {}!", nombre, binary); println!("Nope! {} in binary is {}!", ans.0, ans.1);
} },
} };
} }
} }