diff --git a/LICENSE b/LICENSE index 0ad25db..be3f7b2 100644 --- a/LICENSE +++ b/LICENSE @@ -633,8 +633,8 @@ the "copyright" line and a pointer to where the full notice is found. Copyright (C) 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, diff --git a/src/main.rs b/src/main.rs index 53fadb7..c0d62c8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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::(); - + 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::() + .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 binvec = vec![]; + + 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():: { - // 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::(); - - 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; - //} + let binary = binnery + .parse::() + .unwrap(); - //else { - // println!("Nope! Try again!"); - // continue; - //} + println!("Type {} in binary", nombre); + let mut input = String::new(); + + std::io::stdin() + .read_line(&mut input) + .unwrap(); + + let guess = input + .trim() + .parse::() + .unwrap(); + + match guess.cmp(&binary) { + Ordering::Equal => { + println!("That's correct! {} in binary is {}!", nombre, binnery); + } + Ordering::Greater | Ordering::Less => { + println!("Nope! {} in binary is {}!", nombre, binnery); } } -// println!("That's correct! {} in binary is {}!", nombre, binnery); -} +} \ No newline at end of file