fixed main.rs

This commit is contained in:
Emma Tebibyte 2022-04-28 15:36:36 -04:00
parent 36a53ba1e7
commit f6337a13d0
2 changed files with 52 additions and 64 deletions

View File

@ -633,8 +633,8 @@ the "copyright" line and a pointer to where the full notice is found.
Copyright (C) <year> <name of author>
This program 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
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.
This program is distributed in the hope that it will be useful,

View File

@ -1,4 +1,3 @@
use std::io;
use rand::Rng;
use std::cmp::Ordering;
@ -6,76 +5,65 @@ 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>();
let mut liminput = String::new();
println!("Provide an upward limit for generated numbers: ");
std::io::stdin()
.read_line(&mut liminput)
.unwrap();
let limit = liminput
.trim()
.parse::<u32>()
.unwrap();
// generate random number
let mut 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
while nombre > 0 {
let mut numero = nombre % 2;
nombre / 2;
let mut binvec = vec![];
binvec.push(numero);
let mut countdown = nombre;
while countdown > 0 {
binvec.push(countdown % 2);
countdown /= 2;
}
let mut binnery = String::new();
for i in binvec {
let mut binnery = "";
binnery = i.to_string() + 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 binary = binnery
.parse::<u32>()
.unwrap();
let mut guess = std::io::stdin("Type {} in binary", nombre)
.read_line(&mut guess)
println!("Type {} in binary", nombre);
let mut input = String::new();
std::io::stdin()
.read_line(&mut input)
.unwrap();
let guess = input
.trim()
.parse::<u32>();
.parse::<u32>()
.unwrap();
match guess.cmp(&nombre) {
match guess.cmp(&binary) {
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;
//}
Ordering::Greater | Ordering::Less => {
println!("Nope! {} in binary is {}!", nombre, binnery);
}
}
// println!("That's correct! {} in binary is {}!", nombre, binnery);
}