Hangman game
HangmanGame.java
1 package hangmangame;
2
3 import java.util.Random;
4 import java.util.Scanner;
5
6 public class HangmanGame {
7
8 public static void main(String[] args) {
9
10 Random rand = new Random();
11 Scanner scan = new Scanner(System.in);
12
13 String word, guess, convertDashArray;
14 char check;
15 int random = 0, counter = 0;
16 boolean hanged = false;
17 boolean matched = false;
18
19 String[] alphabetArray = {" A ", " B ", " C ", " D ", " E ", " F ", " G ", " H ", " I ", " J ", " K ", " L ", " M ",
20 " N ", " 0 ", " P ", " Q ", " R ", " S ", " T ", " U ", " V ", " W ", " X ", " Y ", " Z "};
21
22 String[] wordsArray = {"elephant", "star", " candy", "wings", "shopping", "steak", "couch", "toy", "cat", "mouse",
23 "elevator", "taxi", "airplane", "bus", "car", "flower", "cheese", "apple", "orange", "coat",
24 "running", "sweater", "blanket", "chocolate", "chicken", "computer", "rice", "dance", "doll",
25 "duck", "alligator", "snake", "calculator", "castle", "balloon", "laptop", "turtle", "pencil",
26 "fridge", "police", "antartica", "dinner", "fire", "cookie", "pizza", "church", "village",
27 "pistachio", "reptile", "rose", "apartment", "random", "words", "dancing", "soccer", "hockey",
28 "panther", "christmas", "bookworm", "buffalo", "glowworm", "ivory", "microwave", "oryx", "antelope",
29 "arabian", "knight", "rickshaw", "january", "february", "march", "april", "may", "june", "july",
30 "august", "september", "october", "november", "december", "dragon", "panda", "kungfu", "karate",
31 "shark", "dolphin", "seagulls", "salmon", "crab", "whale", "leopard", "lion", "cheetah", "hippopotamus",
32 "calendar", "racoon", "google", "yahoo", "amazon", "lamborghini", "ferrari", "lotus", "toyota", "honda",
33 "eskimo", "igloo", "eagle", "helicopter", "globe", "jamaica", "korea", "indonesia", "japan", "nigeria",
34 "india", "pakistan", "america", "brazil", "canada", "mexico", "switzerland", "ghana", "bangladesh", "pen",
35 "iceland", "greenland", "cranberry", "honey", "money", "california", "texas", "virginia", "Illinois",
36 "chicago", "massachusetts", "pennsylvania", "alaska", "florida", "louisiana", "arizona", "kentucky",
37 "maryland", "colorado", "georgia", "tennessee", "iowa", "nebraska", "missouri", "burger", "brownie",
38 "machine", "mechanic", "teacher", "student", "student", "office", "glass", "friend", "chair", "bed", "pug",
39 "beagle", "bulldog", "chihuahua", "boxer", "pomeranian", "poodle", "dachshund", "bible", "easter", "button",
40 "gymnast", "clown", "ventriloquist", "piano", "violin", "guitar", "blueberry", "envelope", "tiger",
41 "passport", "navigate", "swimming", "dinosaur", "marshmallow", "pink", "fish", "vegetable", "potato",
42 "plant", "bench", "window", "curtain", "plate", "yogurt", "spinach", "grass", "tomato", "smart", "parrot",
43 "sparrow", "cloud", "jeep", "bicycle", "account", "salad", "onion", "book", "manager", "vacation", "turkey",
44 "afghanistan", "iran", "iraq", "libya", "morocco", "spain", "germany", "scotland", "argentina", "australia",
45 "bahamas", "austria", "bahrain", "cambodia", "nigeria", "kuwait", "croatia", "denmark", "fiji", "france",
46 "greece", "indonesia", "ireland", "italy", "israel", "jordan", "kazakhstan", "kenya", "madagascar", "micronesia",
47 "mauritius", "nepal", "oman", "philippines", "seychelles", "zinbabwe", "vietnam", "venezuela", "qatar", "oman",
48 "excavator", "truck", "hedgehog", "deer", "bear", "rabbit", "tractor", "bulldozer", "treadmill", "wine"};
49
50 char[] dashArray;
51
52 random = rand.nextInt(wordsArray.length);
53 word = wordsArray[random];
54
55 dashArray = new char[word.length()];
56
57 System.out.println("------------------: HANGMAN GAME INSTRUCTIONS :------------------");
58 System.out.println("1. You can enter either a letter or try to guess the word any time.");
59 System.out.println("2. You have six chances before the hangman is hanged (you fail).");
60 System.out.println("3. If you try to guess the word, but the guess is wrong, program will read the first letter only.");
61 System.out.println("4. If you guess the word correctly, you win.");
62
63 System.out.println();
64
65 System.out.print("Letters available: ");
66
67 for(int c=0; c<alphabetArray.length; c++)
68 {
69 System.out.print(alphabetArray[c]);
70 }
71
72 System.out.println();
73
74 for(int a=0; a<dashArray.length; a++)
75 {
76 dashArray[a] = '-';
77 System.out.print(dashArray[a]);
78 }
79
80 System.out.print("\t" + word.length() + " letters!");
81
82 while(hanged == false)
83 {
84 System.out.println();
85
86 System.out.print("Enter a guess (letter or word): ");
87 guess = scan.nextLine();
88 check = guess.charAt(0);
89
90 for(int b=0; b<dashArray.length; b++)
91 {
92 if(check == word.charAt(b))
93 {
94 alphabetArray[(int) Character.toUpperCase(check) - 65] = " ";
95
96 dashArray[b] = guess.charAt(0);
97
98 matched = true;
99 }
100
101 else
102 {
103 alphabetArray[(int) Character.toUpperCase(check) - 65] = " ";
104 }
105
106 System.out.print(dashArray[b]);
107 }
108
109 if(matched == false)
110 {
111 counter++;
112 }
113
114 matched = false;
115
116 convertDashArray = new String(dashArray);
117
118 if(word.equals(guess) || convertDashArray.equals(word))
119 {
120 System.out.println("\tGood guess, you win!");
121
122 hanged = true;
123
124 break;
125 }
126
127 System.out.println();
128
129 if(counter == 0)
130 {
131 System.out.println("\t\t\t\t\t _____");
132 System.out.println("\t\t\t\t\t | |");
133 System.out.println("\t\t\t\t\t |");
134 System.out.println("\t\t\t\t\t |");
135 System.out.println("\t\t\t\t\t |");
136 System.out.println("\t\t\t\t\t_____|");
137 }
138
139 else if(counter == 1)
140 {
141 System.out.println("\t\t\t\t\t _____");
142 System.out.println("\t\t\t\t\t | |");
143 System.out.println("\t\t\t\t\t 0 |");
144 System.out.println("\t\t\t\t\t |");
145 System.out.println("\t\t\t\t\t |");
146 System.out.println("\t\t\t\t\t_____|");
147 hanged = false;
148 }
149
150 else if(counter == 2)
151 {
152 System.out.println("\t\t\t\t\t _____");
153 System.out.println("\t\t\t\t\t | |");
154 System.out.println("\t\t\t\t\t 0 |");
155 System.out.println("\t\t\t\t\t | |");
156 System.out.println("\t\t\t\t\t |");
157 System.out.println("\t\t\t\t\t_____|");
158 hanged = false;
159 }
160
161 else if(counter == 3)
162 {
163 System.out.println("\t\t\t\t\t _____");
164 System.out.println("\t\t\t\t\t | |");
165 System.out.println("\t\t\t\t\t 0 |");
166 System.out.println("\t\t\t\t\t/| |");
167 System.out.println("\t\t\t\t\t |");
168 System.out.println("\t\t\t\t\t_____|");
169 hanged = false;
170 }
171
172 else if(counter == 4)
173 {
174 System.out.println("\t\t\t\t\t _____");
175 System.out.println("\t\t\t\t\t | |");
176 System.out.println("\t\t\t\t\t 0 |");
177 System.out.println("\t\t\t\t\t/|\\ |");
178 System.out.println("\t\t\t\t\t |");
179 System.out.println("\t\t\t\t\t_____|");
180 hanged = false;
181
182 }
183
184 else if(counter == 5)
185 {
186 System.out.println("\t\t\t\t\t _____");
187 System.out.println("\t\t\t\t\t | |");
188 System.out.println("\t\t\t\t\t 0 |");
189 System.out.println("\t\t\t\t\t/|\\ |");
190 System.out.println("\t\t\t\t\t/ |");
191 System.out.println("\t\t\t\t\t_____|");
192 hanged = false;
193 }
194
195 else if(counter == 6)
196 {
197 System.out.println("\t\t\t\t\t _____");
198 System.out.println("\t\t\t\t\t | |");
199 System.out.println("\t\t\t\t\t 0 |");
200 System.out.println("\t\t\t\t\t/|\\ |");
201 System.out.println("\t\t\t\t\t/ \\ |");
202 System.out.println("\t\t\t\t\t_____|");
203 System.out.println("\tSorry you lost, the word was: " + word);
204 hanged = true;
205 }
206
207 System.out.println();
208
209 System.out.print("Letters available: ");
210
211 for(int d=0; d<alphabetArray.length; d++)
212 {
213 switch (guess)
214 {
215 case "a":
216 alphabetArray[0] = " ";
217 break;
218 case "b":
219 alphabetArray[1] = " ";
220 break;
221 case "c":
222 alphabetArray[2] = " ";
223 break;
224 case "d":
225 alphabetArray[3] = " ";
226 break;
227 case "e":
228 alphabetArray[4] = " ";
229 break;
230 case "f":
231 alphabetArray[5] = " ";
232 break;
233 case "g":
234 alphabetArray[6] = " ";
235 break;
236 case "h":
237 alphabetArray[7] = " ";
238 break;
239 case "i":
240 alphabetArray[8] = " ";
241 break;
242 case "j":
243 alphabetArray[9] = " ";
244 break;
245 case "k":
246 alphabetArray[10] = " ";
247 break;
248 case "l":
249 alphabetArray[11] = " ";
250 break;
251 case "m":
252 alphabetArray[12] = " ";
253 break;
254 case "n":
255 alphabetArray[13] = " ";
256 break;
257 case "o":
258 alphabetArray[14] = " ";
259 break;
260 case "p":
261 alphabetArray[15] = " ";
262 break;
263 case "q":
264 alphabetArray[16] = " ";
265 break;
266 case "r":
267 alphabetArray[17] = " ";
268 break;
269 case "s":
270 alphabetArray[18] = " ";
271 break;
272 case "t":
273 alphabetArray[19] = " ";
274 break;
275 case "u":
276 alphabetArray[20] = " ";
277 break;
278 case "v":
279 alphabetArray[21] = " ";
280 break;
281 case "w":
282 alphabetArray[22] = " ";
283 break;
284 case "x":
285 alphabetArray[23] = " ";
286 break;
287 case "y":
288 alphabetArray[24] = " ";
289 break;
290 case "z":
291 alphabetArray[25] = " ";
292 break;
293 }
294
295 System.out.print(alphabetArray[d]);
296 }
297 }
298 }
299 }
See the video here:
where is the package
ReplyDelete