fixed main.rs
This commit is contained in:
parent
36a53ba1e7
commit
f6337a13d0
4
LICENSE
4
LICENSE
@ -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,
|
||||||
|
112
src/main.rs
112
src/main.rs
@ -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 {
|
|
||||||
// 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 {
|
let binary = binnery
|
||||||
// break;
|
.parse::<u32>()
|
||||||
//}
|
.unwrap();
|
||||||
|
|
||||||
//else {
|
println!("Type {} in binary", nombre);
|
||||||
// println!("Nope! Try again!");
|
let mut input = String::new();
|
||||||
// continue;
|
|
||||||
//}
|
std::io::stdin()
|
||||||
|
.read_line(&mut input)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let guess = input
|
||||||
|
.trim()
|
||||||
|
.parse::<u32>()
|
||||||
|
.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);
|
}
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user