Adding prior code
This commit is contained in:
parent
2342a208d0
commit
36a53ba1e7
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
10
Cargo.toml
Normal file
10
Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "bingame-rs"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
|
||||
rand = "0.8.4"
|
81
src/main.rs
Normal file
81
src/main.rs
Normal file
@ -0,0 +1,81 @@
|
||||
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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user