-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHangman.java
168 lines (136 loc) · 4.38 KB
/
Hangman.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import java.util.Scanner;
public class Hangman {
public static String[] words = { "ant", "baboon", "badger", "bat", "bear", "beaver", "camel", "cat", "clam", "cobra",
"cougar", "coyote", "crow", "deer", "dog", "donkey", "duck", "eagle", "ferret", "fox", "frog", "goat", "goose",
"hawk", "lion", "lizard", "llama", "mole", "monkey", "moose", "mouse", "mule", "newt", "otter", "owl", "panda",
"parrot", "pigeon", "python", "rabbit", "ram", "rat", "raven", "rhino", "salmon", "seal", "shark", "sheep",
"skunk", "sloth", "snake", "spider", "stork", "swan", "tiger", "toad", "trout", "turkey", "turtle", "weasel",
"whale", "wolf", "wombat", "zebra" }; // 64
private static Scanner scanner = new Scanner(System.in);
private static boolean lose = true;
private static char[] missed = new char[10];
private static int numOfMisses = 0;
public static void main(String[] args) {
int random = randomString();
String randomWord = words[random];
int turnFalseOff = 0;
int turnTrueOff = 0;
boolean plusNumOfMisse = true;
System.out.println(randomWord);
char[] faceWord = new char[randomWord.length()];
for (int i = 0; i < faceWord.length; i++) {
faceWord[i] = '_';
}
while (lose) {
System.out.print(gallows[numOfMisses]);
if (numOfMisses == 6) {
System.out.println("\nYou lose!");
System.exit(0);
}
if (turnFalseOff == randomWord.length()) {
lose = false;
} else {
lose = true;
}
System.out.print("word: ");
for (int i = 0; i < randomWord.length(); i++) {
System.out.print(faceWord[i] + " ");
}
for (int i = 0; i < faceWord.length; i++) {
if (faceWord[i] == randomWord.charAt(i)) {
turnTrueOff++;
}
}
if (turnTrueOff == randomWord.length()) {
System.out.println("\nYou win!");
System.exit(0);
} else {
turnTrueOff = 0;
}
System.out.println("\n");
System.out.print("Misses: ");
for (int i = 0; i < missed.length; i++) {
System.out.print(missed[i]);
}
System.out.print("\nGuess: ");
char guess = scanner.nextLine().charAt(0);
System.out.println(" Guess: " + guess + "\n");
for (int i = 0; i < randomWord.length(); i++) {
if (guess == randomWord.charAt(i)) {
faceWord[i] = guess;
}
}
for (int i = 0; i < randomWord.length(); i++) {
if (guess == randomWord.charAt(i)) {
plusNumOfMisse = false;
break;
} else if (guess != randomWord.charAt(i)) {
plusNumOfMisse = true;
}
}
if (plusNumOfMisse == true) {
missed[numOfMisses] = guess;
numOfMisses++;
}
for (int i = 0; i < faceWord.length; i++) {
if (faceWord[i] == randomWord.charAt(i)) {
turnFalseOff++;
}
}
}
}
private static int randomString() {
int randomNum = (int) (Math.random() * 64);
return randomNum;
}
public static String[] gallows = { // 7 length
"+---+\n" +
"| |\n" +
" |\n" +
" |\n" + // if the user didn't miss any guesses.
" |\n" +
" |\n" +
"=========\n",
"+---+\n" +
"| |\n" +
"O |\n" + // if the user missed one guess.
" |\n" +
" |\n" +
" |\n" +
"=========\n",
"+---+\n" +
"| |\n" +
"O |\n" + // if the user missed two guesses.
"| |\n" +
" |\n" +
" |\n" +
"=========\n",
" +---+\n" +
" | |\n" +
" O |\n" + // if the user missed three guesses.
"/| |\n" +
" |\n" +
" |\n" +
" =========\n",
" +---+\n" +
" | |\n" +
" O |\n" +
"/|\\ |\n" + // if the user missed four guesses.
" |\n" +
" |\n" +
" =========\n",
" +---+\n" +
" | |\n" +
" O |\n" +
"/|\\ |\n" + // if the user missed five guesses.
"/ |\n" +
" |\n" +
" =========\n",
" +---+\n" +
" | |\n" +
" O |\n" +
"/|\\ |\n" + // if the user missed six guesses.
"/ \\ |\n" +
" |\n" +
" =========\n" };
}