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 {
let mut input = String::new();
std::io::stdin()
.read_line(&mut input)
.unwrap();
loop { match input.trim().parse::<u32>() {
Ok(output) => return Some(output),
let mut input = String::new(); Err(_) => {
std::io::stdin() match input.trim() {
.read_line(&mut input) "quit" | ":q" | "q" | "stop" | "exit" => {
.unwrap(); println!("Stopping...");
return None;
match input.trim().parse::<u32>() { },
_ => eprintln!("{} is not a number!", input),
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.
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
let nombre: u32 = rand::thread_rng().gen_range(0..limit);
// generate random number // convert number from decimal to binary notation
let mut binvec = vec![];
let mut countdown = nombre;
while countdown > 0 {
binvec.push(countdown % 2);
countdown /= 2;
}
let nombre: u32 = rand::thread_rng().gen_range(0..limit); let mut binnery = String::new();
for i in binvec { binnery = i.to_string() + &binnery; }
// convert number from decimal to binary notation let binnery = binnery.parse::<u32>();
let mut binvec = vec![]; let binary = match binnery {
Ok(binary) => binary,
let mut countdown = nombre; Err(_) => std::process::abort(),
};
while countdown > 0 { return Ok((nombre, binary));
binvec.push(countdown % 2);
countdown /= 2;
}
let mut binnery = String::new();
for i in binvec {
binnery = i.to_string() + &binnery;
}
let binnery = binnery.parse::<u32>();
let binary = match binnery {
Ok(binary) => { binary },
Err(why) => { std::process::abort(); }
};
return Ok((nombre, binary));
} }
fn game(limit: u32) { fn game(limit: u32) {
loop {
let ans = numgen(limit).unwrap();
println!("Type {} in binary", ans.0);
let guess = match parse_input() {
Some(guess) => guess,
None => std::process::exit(0),
};
loop { match guess.cmp(&ans.1) {
Ordering::Equal => {
let ans = numgen(limit).unwrap(); println!("That's correct! {} in binary is {}!", ans.0, ans.1);
let nombre = ans.0; },
let binary = ans.1; Ordering::Greater | Ordering::Less => {
println!("Nope! {} in binary is {}!", ans.0, 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);
}
}
}
}