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,
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										94
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										94
									
								
								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;
 | 
					    let mut binvec = vec![];
 | 
				
			||||||
        nombre / 2;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let mut binvec = vec![];
 | 
					    let mut countdown = nombre;
 | 
				
			||||||
        binvec.push(numero);
 | 
					
 | 
				
			||||||
 | 
					    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();
 | 
				
			||||||
            .trim()
 | 
					 | 
				
			||||||
            .parse::<u32>();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        match guess.cmp(&nombre) {
 | 
					    std::io::stdin()
 | 
				
			||||||
            Ordering::Equal => {
 | 
					    .read_line(&mut input)
 | 
				
			||||||
                println!("That's correct! {} in binary is {}!", nombre, binnery);
 | 
					    .unwrap();
 | 
				
			||||||
                break;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            Ordering::Less | Greater => {
 | 
					 | 
				
			||||||
                println!("Nope! Try again!");
 | 
					 | 
				
			||||||
                continue;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // if guess == binnery {
 | 
					    let guess = input
 | 
				
			||||||
            //    break;
 | 
					    .trim()
 | 
				
			||||||
            //}
 | 
					    .parse::<u32>()
 | 
				
			||||||
 | 
					    .unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            //else {
 | 
					    match guess.cmp(&binary) {
 | 
				
			||||||
            //    println!("Nope! Try again!");
 | 
					        Ordering::Equal => {
 | 
				
			||||||
            //    continue;
 | 
					            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…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user