forked from abhishekdoifode1/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessing_Game_Human_vs_CPU.java
More file actions
77 lines (68 loc) · 2.54 KB
/
Guessing_Game_Human_vs_CPU.java
File metadata and controls
77 lines (68 loc) · 2.54 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.util.*;
import java.util.*;
public class Guessing_Game_Human_vs_CPU
{
public static int computerGuess(int lower,int upper,int randnum,int count)
{
int guess=(lower+upper)/2;
if(guess==randnum)
return count;
if(guess<randnum){
count++;
return computerGuess(guess,upper,randnum,count);
}
if(guess>randnum){
count++;
return computerGuess(lower,guess,randnum,count);
}
return -1;
}
public static void main(String[] args)
{
final Scanner sc=new Scanner(System.in);
char ch='Y';
//the game resets if select Y else exits
while(ch=='Y')
{
//Generating a random integer number Between 1 to 100
int randnum=(int)(Math.random()*100+1);
//Math.random gives adouble between 0 and 1 so multiply by 100 so gives between 0 to 99 and 1 for 0 to 100
int chances=10;
while(chances>0) //while attempts are less than 10
{
System.out.print("Guesss A Number between 1 to 100:");
int guess=sc.nextInt();
//input the guessed Number
if(guess==randnum){
System.out.println("Congrats you guessed the number !!\n");
break;
}
else if(guess>randnum){
System.out.println("Your guess is higher than the number.\n");
}
else{
System.out.println("Your guess is lower than the number.\n");
}
chances--;
}
if(chances!=0){
System.out.println("\nYou guessed the Number in "+(10-chances)+" attempts.");
System.out.println("You scored "+(chances*10)+" points.");
}
else{
System.out.println("\nYou were not able to guess the number in 10 attempts.\n Better Luck next Time");
System.out.println("You scored 0 points.");
}
int steps=computerGuess(0,100,randnum,0);
System.out.println("\nComputer solved it in "+steps+" attempts.");
if((10-chances)<steps)
System.out.println("\nYou Beat the Computer");
else
System.out.println("\nYou Lost to the Computer");
//check if want to continue the game or exit
System.out.println("\nDo You want to TRY AGAIN:(Y-YES/N-NO)");
ch=sc.next().charAt(0);
}
System.out.println("\nThank you Playing!!");
}
}