-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHangman.java
More file actions
142 lines (118 loc) · 3.66 KB
/
Hangman.java
File metadata and controls
142 lines (118 loc) · 3.66 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
public class Hangman {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] choices = {"washington", "adams", "jefferson", "madison", "monroe", "jackson", "harrison", "tyler", "polk", "taylor", "fillmore", "pierce", "buchanan", "lincoln", "johnson", "grant", "hayes", "garfield", "arthur", "cleveland", "mckinley", "roosevelt", "taft", "wilson", "harding", "coolidge", "hoover", "truman", "eisenhower", "kennedy", "nixon", "ford", "carter", "reagan", "bush", "clinton", "obama", "trump"};
int guessesLeft = 6;
int correctGuesses = 0;
String word = "";
String display = "";
Word wordObj;
public void chooseWord(String[] choices){
Random rand = new Random();
int randomIndex = rand.nextInt(38);
word = choices[randomIndex];
}
public void initializeGame(Hangman obj) throws IOException{
obj.chooseWord(obj.choices);
wordObj = new Word(obj.word);
wordObj.populateArr();
display = wordObj.buildDisplay();
System.out.println("Your word is: " + display);
promptGuess();
}
public void displayResult(String a){
if (a.equals("win")){
System.out.println("Good job, you won!");
}
if (a.equals("lose")){
System.out.println("Sorry, you lost");
System.out.println("The correct word was " + word);
}
}
public void promptGuess() throws IOException {
if (display.indexOf("_") == -1){
displayResult("win");
} else if (guessesLeft == 0){
displayResult("lose");
} else {
System.out.print("Guess a letter: ");
String userGuess = reader.readLine();
if (userGuess.length() != 1){
System.out.println();
System.out.println("Invalid Guess - try again");
System.out.println();
promptGuess();
} else {
System.out.println();
wordObj.guess(userGuess.toLowerCase());
String newDisplay = wordObj.buildDisplay();
checkScore(newDisplay, display);
System.out.println("Your word is: " + display);
promptGuess();
}
}
}
public void checkScore(String newStr, String oldStr){
int oldDashCount = 0;
int newDashCount = 0;
for (int i = 0; i < oldStr.length(); i++){
if(Character.toString(oldStr.charAt(i)).equals("_")){
oldDashCount++;
}
}
for (int i = 0; i < newStr.length(); i++){
if(Character.toString(newStr.charAt(i)).equals("_")){
newDashCount++;
}
}
if(newDashCount < oldDashCount){
display = newStr;
} else {
guessesLeft--;
System.out.println("Wrong, remaining guesses: "+ guessesLeft);
}
}
public static void main(String[] args) throws IOException {
var game = new Hangman();
game.initializeGame(game);
}
}
class Word {
String word = "default";
public Word(String word){
this.word = word;
this.letterArr = new Letter[word.length()];
}
public Letter[] letterArr;
void populateArr(){
for(int i = 0; i < letterArr.length; i++){
letterArr[i] = new Letter(word.substring(i, i+1));
}
}
public String buildDisplay(){
String val = "";
for(Letter obj: letterArr) val += obj.toString();
return val;
}
public void guess(String a){
for(Letter obj: letterArr) obj.checkLetter(a);
}
}
class Letter {
String letter;
public Letter(String letter){
this.letter = letter;
}
boolean guessed = false;
public String toString(){
return this.guessed ? " " + letter + " " : " _ ";
}
public void checkLetter(String a){
if (a.equals(this.letter)){
this.guessed = true;
}
}
}