-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject_4.java
201 lines (186 loc) · 6.47 KB
/
Project_4.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
*
*/
import java.util.*;
import java.io.*;
/** A working hangman game using random words from a dictionary file.
* @author Logan Murphy
* @version 1.0
*/
public class Project_4 {
public static void main(String[] args) throws FileNotFoundException {
Scanner scnr = new Scanner(System.in);
boolean gameEnd = false;
//while gameEnd is false the game loops
while(!gameEnd) {
//reads the dictionary file, chooses a random word, and creates a pound(#) for that random word
String[] words = readFile("/Users/logan/Downloads/Project_4 packet/dictionary.txt");
String randomWord = chooseRandomWord(words);
String pound = createPound(randomWord);
int incorrectGuesses = 0;
//while the number of incorrect guesses is less than 7, loop the guessing prompt
while(incorrectGuesses < 7) {
//prompt the user to guess a letter
System.out.println("Guess any letter in the word");
System.out.println(pound);
//take a guess from the user
char guess = scnr.next().charAt(0);
//if the guess is correct
if(randomWord.indexOf(guess) != -1) {
pound = guessWord(guess, randomWord, pound);
//check to see if the user has won, break the loop if so
if(pound.indexOf('#') == -1) {
System.out.println("Correct! you have won this game. The word is: " + pound);
break;
//if the guess is incorrect
} else {
System.out.println(pound);
}
}
else {
//increment the number of incorrect guesses and generate the appropriate hangman image
incorrectGuesses++;
hangmanImage(incorrectGuesses, randomWord);
}
}
//ask the user whether they would like to continue playing
System.out.println("Do you still want to play?");
String keepPlaying = scnr.next();
//if the user answers no, gameEnd is set to true, loop ends
if(keepPlaying.equals("no") || keepPlaying.equals("No")) {
gameEnd = true;
}
}
}
//method to read a file and turn it into an array of strings
//exception is thrown if given file not found
public static String[] readFile(String fileName) throws FileNotFoundException {
Scanner fileInput = new Scanner(new File(fileName));
//find the amount of lines with data in the file
int fileLength = 0;
while(fileInput.hasNext()) {
fileInput.next();
fileLength++;
}
fileInput.close();
//parse each line of the file and put each string into array
String[] fileArray = new String[fileLength];
fileInput = new Scanner(new File(fileName));
int i = 0;
while(fileInput.hasNext()) {
fileArray[i] = fileInput.next();
i++;
}
fileInput.close();
//return array of strings
return fileArray;
}
//method to take a word and create a string of pounds(#) for the length of said word
public static String createPound(String word) {
String pound = "";
for(int i = 0; i < word.length(); i++) {
pound += "#";
}
return pound;
}
//method to take a guess and a word and see if the user has guessed right, and if they have, update pound with letter in all locations in word
public static String guessWord(char guess, String word, String pound) {
String updatedPound = pound;
int i = 0;
while(word.indexOf(guess, i) != -1) {
updatedPound = updatedPound.substring(0, word.indexOf(guess,i)) + guess + updatedPound.substring(word.indexOf(guess,i)+1, pound.length());
i++;
}
return updatedPound;
}
//method to take an array of words and use Math.Random to choose a random word within the bounds of the list
public static String chooseRandomWord(String[] words) {
Random rnd = new Random();
int randomInt = rnd.nextInt((words.length));
return words[randomInt];
}
//method to generate the appropriate hangman image given a count of incorrect guesses
public static void hangmanImage(int count, String word) {
if (count == 1) {
System.out.println("Wrong guess, try again");
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("___|___");
System.out.println();
}
if (count == 2) {
System.out.println("Wrong guess, try again");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println("___|___");
}
if (count == 3) {
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" | ");
System.out.println("___|___");
}
if (count == 4) {
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println("___|___");
}
if (count == 5) {
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" |");
System.out.println("___|___");
}
if (count == 6) {
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | / \\ ");
System.out.println("___|___ / \\");
}
if (count == 7) {
System.out.println("GAME OVER!");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | _|_");
System.out.println(" | / | \\");
System.out.println(" | / \\ ");
System.out.println("___|___ / \\");
System.out.println("GAME OVER! The word was " + word);
}
}
}