-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuess The Game.rs
More file actions
62 lines (50 loc) · 1.86 KB
/
Guess The Game.rs
File metadata and controls
62 lines (50 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
Making a "Guess" a game to know how many try it takes for you to guess the number
Motive behind this code is to know Inputs and how &mut(Mutable Data type) works
also some conditional statements
*/
use std::io;
fn main() {
let mut number = String::new();
let mut guess = String::new();
let mut count:i32 = 0;
let mut num:i32;
let mut num2:i32;
// Hello World with new line
println!("Player 1 Please Enter a number(1 - 100) to be guessed: ");
io::stdin().read_line(&mut number).expect("Failed to read line");
number.pop();
num = number.parse().expect("Reason");
println!(" Number you choose is : {} ",num);
while num < 0 || num > 100 {
number = String::new();
println!("Please Enter a Valid Number in between (1 -100) inclusive :");
io::stdin().read_line(&mut number).expect("Failed to read line");
number.pop();
num = number.parse().expect("Reason");
println!(" Number you choose is : {} ",num);
}
println!(" Player 2 Please Make your guess :");
io::stdin().read_line(&mut guess).expect("Failed to read line");
guess.pop();
num2 = guess.parse().expect("Reason");
println!(" Your Guess was : {} ",num2);
count += 1;
while num2 != num {
guess = String::new();
print!(" Try Another Guess ");
if num2 > num {
println!( " (Last Number was Too High) : ");
}
else{
println!( " (Last Number was Too Low) : ");
}
io::stdin().read_line(&mut guess).expect("Failed to read line");
guess.pop();
num2 = guess.parse().expect("Reason");
println!(" Your Guess was : {} ",num2);
count+=1;
}
println!(" Yayhh ! you guess it correct number was : {} ", num);
println!("Total Tries u took till now is : {}", count);
}