82 lines
2.0 KiB
Rust
82 lines
2.0 KiB
Rust
use std::io;
|
|
use rand::Rng;
|
|
use std::cmp::Ordering;
|
|
|
|
fn main() {
|
|
|
|
println!("Welcome to bingame! Type \"quit\" anytime to stop playing.");
|
|
|
|
let mut limit = "";
|
|
println!("Supply an upper limit for generating the number: ");
|
|
io::stdin()
|
|
.read_line(&mut limit)
|
|
.parse::<u32>();
|
|
|
|
// generate random number
|
|
let mut nombre: u32 = rand::thread_rng().gen_range(0..limit);
|
|
|
|
// convert number from decimal to binary notation
|
|
while nombre > 0 {
|
|
|
|
let mut numero = nombre % 2;
|
|
nombre / 2;
|
|
|
|
let mut binvec = vec![];
|
|
binvec.push(numero);
|
|
|
|
}
|
|
|
|
for i in binvec {
|
|
|
|
let mut binnery = "";
|
|
|
|
binnery = i.to_string() + binnery;
|
|
|
|
}
|
|
|
|
loop {
|
|
// take user input and parse it as an unsigned integer
|
|
//let mut guess = std::io::stdin("Type {} in binary: ", nombre)
|
|
// .read_line(&mut guess)
|
|
// .trim()
|
|
// .parse()::<u32> {
|
|
// Ok(num) => num,
|
|
// Err(_) => {
|
|
// if guess == "quit" {
|
|
// break;
|
|
// }
|
|
//
|
|
// else {
|
|
// continue;
|
|
// }
|
|
// }
|
|
// };
|
|
|
|
let mut guess = std::io::stdin("Type {} in binary", nombre)
|
|
.read_line(&mut guess)
|
|
.trim()
|
|
.parse::<u32>();
|
|
|
|
match guess.cmp(&nombre) {
|
|
Ordering::Equal => {
|
|
println!("That's correct! {} in binary is {}!", nombre, binnery);
|
|
break;
|
|
}
|
|
Ordering::Less | Greater => {
|
|
println!("Nope! Try again!");
|
|
continue;
|
|
}
|
|
|
|
// if guess == binnery {
|
|
// break;
|
|
//}
|
|
|
|
//else {
|
|
// println!("Nope! Try again!");
|
|
// continue;
|
|
//}
|
|
}
|
|
}
|
|
// println!("That's correct! {} in binary is {}!", nombre, binnery);
|
|
}
|