forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyCode.java
123 lines (108 loc) · 3.59 KB
/
MyCode.java
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.util.Scanner;
import java.util.Random;
class Hangman
{
public static void main(String[] args)
{
System.out.println(" @@@@@@@@@@@@@ PLAY HANGMAN @@@@@@@@@@@@@" );
String[] s={"DELHI","KARACHI","NEWYORK","LONDON","TOKYO","SURAT","KABUL","DUBAI","JAIPUR","PUNE"};
Random r=new Random();
System.out.println("GUESS THE WORLD'S LARGEST CITY");
System.out.println("Rule: You have 10 chances for correct guess");
for(;;)
{
int randomNumber=r.nextInt(s.length);
String sw=s[randomNumber];
String incorrect="";
String guess="";
Scanner sc=new Scanner(System.in);
for(int x=0;x<sw.length();x++)
{
guess+="_";
}
int numLives=0;
for(;;)
{
System.out.println("YOUR CORRECT GUESSES");
for(int x=0;x<sw.length();x++)
{
char p=guess.charAt(x);
System.out.print(p+" ");
}
System.out.println();
System.out.println("GUESS A LETTER");
String l=sc.nextLine().toUpperCase();
char c=l.charAt(0);
String ssw="";
if(incorrect.indexOf(c)!=-1)
continue;
else if(guess.indexOf(c)!=-1)
continue;
else if(incorrect.indexOf(c)==-1)
{
if(sw.indexOf(c)!=-1 && guess.indexOf(c)==-1)
{
for(int x=0;x<sw.length();x++)
{
char a=sw.charAt(x);
char b=guess.charAt(x);
if(a==c)
{
ssw+=c;
}
else
{
ssw+=b;
}
}
guess=ssw;
}
else
{
incorrect+=c;
System.out.println("MISSES = "+incorrect);
numLives++;
}
}
if(guess.indexOf('_')==-1 )
break;
if(numLives==10)
break;
}
System.out.println();
if(guess.indexOf('_')!=-1)
{System.out.println(sw);
System.out.println("@@ YOU HAVE LOST THE GAME @@ ");}
else
{System.out.print(sw);
System.out.println("@@ WON! CONGRATULATION GET YOUR PRICE @@");
}
String Q;
char P=0;
System.out.println("DO YOU WANT TO CONTINUE PLAY AGAIN IF YES THEN CLICK Y OR IF NOT THEN CLICK SOMETHING ELSE");
boolean repeat=false;
do
{
repeat=false;
try
{
Q=sc.nextLine();
P=Q.charAt(0);
if(!Character.isLetter(P))
throw new Exception();
}
catch (Exception e)
{
repeat=true;
}
}
while(repeat==true);
if(P=='y'||P=='Y')
continue;
else
{
break;
}
}
}
}