-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
189 lines (152 loc) · 7.64 KB
/
Main.java
File metadata and controls
189 lines (152 loc) · 7.64 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
186
187
188
189
import java.io.BufferedReader;
import java.io.FileReader;
import java.lang.Math;
import java.io.Console;
import java.util.ArrayList;
class HangmanJavaGame {
public static void main (String[] args) throws Exception{
String gamePhrase = pickPhrase();
int incorrectGuesses = 0;
ArrayList<String> incorrectlyGuessed = new ArrayList<String>();
while (incorrectGuesses != 12) {
hangman(incorrectGuesses);
phraseUI(gamePhrase);
System.out.println();
//Print lives remaining
if (incorrectGuesses == 6) {
String resetTextColor = "\033[0m";
String redTextColor = "\033[0;31m";
System.out.println(redTextColor+"One man is hanged, one life remaining."+resetTextColor);
} else {
int numberOfGuesses;
if (incorrectGuesses < 6) {
numberOfGuesses = 6 - incorrectGuesses;
} else {
numberOfGuesses = 12 - incorrectGuesses;
}
System.out.println("You have " + numberOfGuesses + " guesses remaining.");
}
//Print the incorrect guesses
if (incorrectGuesses > 0) {
printIncorrectlyGuessed(incorrectlyGuessed);
}
//Index: 0-Updated gamePhrase, 1-incorrectGuesses, 2-Guess
String[] roundResults = gameRound(gamePhrase, incorrectGuesses, incorrectlyGuessed);
//Update the variables after the round
gamePhrase = roundResults[0];
incorrectGuesses = Integer.parseInt(roundResults[1]);
if (roundResults[2] != null) {
incorrectlyGuessed.add(roundResults[2]);
}
}
//If the hangman is incomplete
hangman(incorrectGuesses);
System.out.println("You lost. You couldn't guess the phrase in time...");
System.out.println("The phrase was: " + gamePhrase.toUpperCase());
}
public static String pickPhrase() throws Exception{
BufferedReader fileReader = new BufferedReader(new FileReader("phrases.txt"));
int numberOfPhrases = 20;
int randomNumber = (int)((Math.random() * (numberOfPhrases - 1)) + 1);
String phrase = "";
//Pick a random phrase to use
for (int i = 1; i <= randomNumber; i++) {
phrase = fileReader.readLine();
}
fileReader.close();
phrase = phrase.toLowerCase();
return phrase;
}
public static void phraseUI(String phrase) {
String phraseText = "Phrase: ";
for (String character : phrase.split("")) {
if (Character.isUpperCase(character.charAt(0))) {
phraseText += character;
}
else if (character.equals(" ")) {
phraseText += " ";
} else {
phraseText += "_";
}
phraseText += " ";
}
System.out.println(phraseText);
if (!phraseText.contains("_")) {
System.out.println("You won the game of Hangman!!!");
System.exit(0);
}
}
public static void hangman(int incorrect) {
String textColorReset = "\u001B[0m";
String textColorRed = "\u001B[31m";
String textColorOrange = "\fafu001b[38;2;253;182;0m";
String textColorYellow = "\u001B[33m";
String textColorGreen = "\u001B[32m";
String textColorBlue = "\u001B[34m";
String textColorPurple = "\u001B[35m";
String[][] hangmanStages = {{" +---+", " | |", " |", " |", " |", " |", "========="}, {" +---+", " | |", " "+textColorRed+"O"+textColorReset+" |", " |", " |", " |", "========="}, {" +---+", " | |", " "+textColorRed+"O"+textColorReset+" |", " "+textColorOrange+"|"+textColorReset+" |", " |", " |", "========="}, {" +---+", " | |", " "+textColorRed+"O"+textColorReset+" |", " "+textColorYellow+"/"+textColorOrange+"|"+textColorReset+" |", " |", " |", "========="}, {" +---+", " | |", " "+textColorRed+"O"+textColorReset+" |", " "+textColorYellow+"/"+textColorOrange+"|"+textColorGreen+"\\"+textColorReset+" |", " |", " |", "========="}, {" +---+", " | |", " "+textColorRed+"O"+textColorReset+" |", " "+textColorYellow+"/"+textColorOrange+"|"+textColorGreen+"\\"+textColorReset+" |", " "+textColorBlue+"/"+textColorReset+" |", " |", "========="}, {" +---+", " | |", " "+textColorRed+"O"+textColorReset+" |", " "+textColorYellow+"/"+textColorOrange+"|"+textColorGreen+"\\"+textColorReset+" |", " "+textColorBlue+"/ "+textColorPurple+"\\"+textColorReset+" |", " |", "========="}};
if (incorrect > 6) {
incorrect -= 6;
}
String[] hangmanCurrentStage = hangmanStages[incorrect];
for (String part : hangmanCurrentStage) {
System.out.println(part);
}
}
public static String[] gameRound(String phrase, Integer incorrectTries, ArrayList<String> incorrectGuesses) throws Exception{
Console consoleInputScanner = System.console();
String[] splitPhrase = phrase.split(" ");
//Ask user to guess
String guess = "";
//Ask user to guess, if an already guessed, ask again
while (true) {
System.out.println();
System.out.print("Guess a letter or word: ");
guess = consoleInputScanner.readLine().trim().toLowerCase();
if (phrase.contains(guess.toUpperCase())) {
System.out.println("You already guessed this...");
} else if (!incorrectGuesses.contains(guess)) {
break;
} else {
System.out.println("You already incorrectly guessed this...");
}
}
String uppercaseGuess = guess.toUpperCase();
//If in the phrase, change it
String newPhrase = "";
boolean correctGuess = false;
for (String word : splitPhrase) {
//Check if the the word is equal to the guess, if the guess is a word itself
if (guess.length() > 1) {
if (word.toLowerCase().equals(guess)) {
word = word.toUpperCase();
correctGuess = true;
}
//Check if the guess is a letter in the word, if the guess is a letter itself
} else if (word.toLowerCase().contains(guess)) {
word = word.replace(guess, uppercaseGuess);
correctGuess = true;
}
newPhrase += word + " ";
}
newPhrase = newPhrase.trim();
//Look if the user is right or not
if (!correctGuess) {
incorrectTries ++;
} else {
guess = null;
}
System.out.print("\033[H\033[2J");
System.out.flush();
String[] results = {newPhrase, Integer.toString(incorrectTries), guess};
return results;
}
public static void printIncorrectlyGuessed(ArrayList<String> incorrectGuessesList) {
String printGuesses = "Incorrect Guesses: ";
for (int i = 0; i < incorrectGuessesList.size()-1; i++) {
printGuesses += incorrectGuessesList.get(i) + ", ";
}
printGuesses += incorrectGuessesList.get(incorrectGuessesList.size()-1);
System.out.println(printGuesses);
}
}