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> Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by
by the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, 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 rand::Rng;
use std::cmp::Ordering; use std::cmp::Ordering;
@ -6,76 +5,65 @@ fn main() {
println!("Welcome to bingame! Type \"quit\" anytime to stop playing."); println!("Welcome to bingame! Type \"quit\" anytime to stop playing.");
let mut limit = ""; let mut liminput = String::new();
println!("Supply an upper limit for generating the number: ");
io::stdin() println!("Provide an upward limit for generated numbers: ");
.read_line(&mut limit) std::io::stdin()
.parse::<u32>(); .read_line(&mut liminput)
.unwrap();
let limit = liminput
.trim()
.parse::<u32>()
.unwrap();
// generate random number // 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 // convert number from decimal to binary notation
while nombre > 0 {
let mut numero = nombre % 2;
nombre / 2;
let mut binvec = vec![]; 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 { for i in binvec {
let mut binnery = ""; binnery = i.to_string() + &binnery;
binnery = i.to_string() + binnery;
} }
loop { let binary = binnery
// take user input and parse it as an unsigned integer .parse::<u32>()
//let mut guess = std::io::stdin("Type {} in binary: ", nombre) .unwrap();
// .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) println!("Type {} in binary", nombre);
.read_line(&mut guess) let mut input = String::new();
std::io::stdin()
.read_line(&mut input)
.unwrap();
let guess = input
.trim() .trim()
.parse::<u32>(); .parse::<u32>()
.unwrap();
match guess.cmp(&nombre) { match guess.cmp(&binary) {
Ordering::Equal => { Ordering::Equal => {
println!("That's correct! {} in binary is {}!", nombre, binnery); println!("That's correct! {} in binary is {}!", nombre, binnery);
break;
} }
Ordering::Less | Greater => { Ordering::Greater | Ordering::Less => {
println!("Nope! Try again!"); println!("Nope! {} in binary is {}!", nombre, binnery);
continue;
}
// if guess == binnery {
// break;
//}
//else {
// println!("Nope! Try again!");
// continue;
//}
} }
} }
// println!("That's correct! {} in binary is {}!", nombre, binnery);
} }