-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHangman.java
More file actions
190 lines (143 loc) · 4.41 KB
/
Hangman.java
File metadata and controls
190 lines (143 loc) · 4.41 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import java.util.Scanner;
public class Hangman {
static Scanner scan = new Scanner(System.in);
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"};
public static String[] gallows = {"+---+\n" +
"| |\n" +
" |\n" +
" |\n" +
" |\n" +
" |\n" +
"=========\n",
"+---+\n" +
"| |\n" +
"O |\n" +
" |\n" +
" |\n" +
" |\n" +
"=========\n",
"+---+\n" +
"| |\n" +
"O |\n" +
"| |\n" +
" |\n" +
" |\n" +
"=========\n",
" +---+\n" +
" | |\n" +
" O |\n" +
"/| |\n" +
" |\n" +
" |\n" +
" =========\n",
" +---+\n" +
" | |\n" +
" O |\n" +
"/|\\ |\n" + //if you were wondering, the only way to print '\' is with a trailing escape character, which also happens to be '\'
" |\n" +
" |\n" +
" =========\n",
" +---+\n" +
" | |\n" +
" O |\n" +
"/|\\ |\n" +
"/ |\n" +
" |\n" +
" =========\n",
" +---+\n" +
" | |\n" +
" O |\n" +
"/|\\ |\n" +
"/ \\ |\n" +
" |\n" +
" =========\n"};
public static void main(String[] args) {
System.out.println("Let's play hangman! Press any key to continue");
scan.nextLine();
int misses = 0;
String word = getRandomWord();
char[] wordArray = new char[word.length()];
for(int i = 0; i < word.length(); i++){
wordArray[i] = word.charAt(i);
}
char[] board = new char[word.length()];
for(int i = 0; i < word.length(); i++){
board[i] = '_';
}
char[] guesses = new char[6];
while(misses < 6){
printGallows(misses, board, guesses);
char guess = askUser();
int num = 0;
for(int i = 0; i < wordArray.length; i++){
if(guess == wordArray[i]){
board[i] = guess;
num++;
}
}
if(num == 0){
misses++;
guesses[misses - 1] = guess;
}
int correct = checkWin(board, wordArray);
if(correct == wordArray.length){
printGallows(misses, board, guesses);
System.out.println("\n\nCongratulations, you win!");
break;
}
}
if(misses == 6){
printGallows(misses, board, guesses);
System.out.println("\nSorry, you lose.");
}
scan.close();
};
/**
* Function name - getRandomWord
* @return word (String)
*
* Inside the function:
* 1.
*/
public static String getRandomWord(){
// get a random number and choose from the list based on that number.
int randomNumber = (int)(Math.random() * words.length);
String word = words[randomNumber];
return word;
}
public static void printGallows(int misses, char[] board, char[]guesses){
System.out.println("\n\n" + gallows[misses]);
System.out.print("Word: ");
for(int i = 0; i < board.length; i++){
System.out.print(board[i] + " ");
}
System.out.print("\n\nMisses: ");
for(int i = 0; i < guesses.length; i++){
if(guesses != null){
System.out.print(guesses[i]);
}
}
}
public static char askUser(){
System.out.print("\n\nGuess: ");
char guess = scan.next().charAt(0);
return guess;
}
public static int checkWin(char[] board, char[]wordArray){
int numCorrect = 0;
for(int i = 0; i < board.length; i++){
if(board[i] == wordArray[i]){
numCorrect++;
}
}
return numCorrect;
}
}