-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHangman.java
More file actions
51 lines (51 loc) · 1.91 KB
/
Copy pathHangman.java
File metadata and controls
51 lines (51 loc) · 1.91 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
//Nancy
import java.util.*;
public class Hangman
{
public static void main (String [] args){
Scanner scan = new Scanner (System.in);
System.out.println("type the word");
String word = scan.nextLine();
for(int i = 0; i < 100; i++){System.out.println();}
word = word.toLowerCase();
boolean [] arr = new boolean [word.length()];
for(int i = 0; i < word.length(); i++){arr[i] = false;}
final int lives = 10;
int wrongs = 0;
boolean won = false;
String guesses = "";
while (!won && wrongs < 10){
for (int i = 0; i < word.length(); i++){
if (arr[i] == false){ System.out.print("_ ");}
else{System.out.print(word.charAt(i) + " ");}
}
System.out.println();
System.out.println("guess a letter");
String guess = scan.nextLine();
guess = guess.toLowerCase();
if (guesses.indexOf(guess) == -1){
guesses = guesses.concat(guess);
if (word.indexOf(guess) != -1){
for (int i = 0; i < word.length(); i++){
if (word.substring(i, i+1).equals(guess)){ arr[i] = true;}
}
}
else {
wrongs ++;
System.out.println("No " + guess + ". " + (lives-wrongs) + " left.");
}
int left = word.length();
for (int i = 0; i < word.length(); i++){
if (arr[i]){ left --;}
}
if(left == 0){won = true;}
}
else {
System.out.println("You have already guessed that." +
"Restarting round.");
}
}
if(won){System.out.println("Congrajulations! " + word + " is correct!");}
else{System.out.println("You have been hanged.");}
}
}