fixed formatting and reduced size
This commit is contained in:
parent
bf2b0c51ab
commit
e059bd667f
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -3,7 +3,7 @@
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "bingame-rs"
|
||||
name = "bingame"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"rand",
|
||||
|
@ -1,10 +1,7 @@
|
||||
[package]
|
||||
name = "bingame-rs"
|
||||
name = "bingame"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
|
||||
rand = "0.8.4"
|
||||
|
152
src/main.rs
152
src/main.rs
@ -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 std::cmp::Ordering;
|
||||
|
||||
fn parse_input() -> Option<u32> {
|
||||
loop {
|
||||
let mut input = String::new();
|
||||
std::io::stdin()
|
||||
.read_line(&mut input)
|
||||
.unwrap();
|
||||
|
||||
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); }
|
||||
|
||||
}
|
||||
}
|
||||
match input.trim().parse::<u32>() {
|
||||
Ok(output) => return Some(output),
|
||||
Err(_) => {
|
||||
match input.trim() {
|
||||
"quit" | ":q" | "q" | "stop" | "exit" => {
|
||||
println!("Stopping...");
|
||||
return None;
|
||||
},
|
||||
_ => eprintln!("{} is not a number!", input),
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
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.");
|
||||
println!("Provide an upward limit for generated numbers: ");
|
||||
let limit = parse_input().unwrap();
|
||||
game(limit)
|
||||
|
||||
let limit = match parse_input() {
|
||||
Some(limit) => limit,
|
||||
None => std::process::exit(0),
|
||||
};
|
||||
game(limit)
|
||||
}
|
||||
|
||||
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 mut countdown = nombre;
|
||||
|
||||
while countdown > 0 {
|
||||
|
||||
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));
|
||||
let binary = match binnery {
|
||||
Ok(binary) => binary,
|
||||
Err(_) => std::process::abort(),
|
||||
};
|
||||
return Ok((nombre, binary));
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
let ans = numgen(limit).unwrap();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
match guess.cmp(&ans.1) {
|
||||
Ordering::Equal => {
|
||||
println!("That's correct! {} in binary is {}!", ans.0, ans.1);
|
||||
},
|
||||
Ordering::Greater | Ordering::Less => {
|
||||
println!("Nope! {} in binary is {}!", ans.0, ans.1);
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user