-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHangman.java
137 lines (117 loc) · 3.64 KB
/
Hangman.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
/*
* File: Hangman.java
* ------------------
* This program will eventually play the Hangman game from
* Assignment #4.
*/
import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.awt.*;
import java.util.Arrays;
import java.util.Random;
public class Hangman extends ConsoleProgram {
private static final int NUM_GUESSES = 8;
private static Random generator = new Random();
private String word;
private char[] working;
private String guessedLetters;
private int guessesRemaining;
public void run() {
setup();
while(true) {
play();
if(!playAgain())
break;
}
}
public void setup() {
lexicon = new HangmanLexicon("ShorterLexicon.txt");
println("Welcome to Hangman!");
}
public void play() {
pickWord();
guessesRemaining = NUM_GUESSES;
while(true) {
println("The word now looks like this " + workingString());
printNumGuesses();
String guessLine = getGuess();
if(guessLine.equalsIgnoreCase(word)) {
printWinMessage();
break;
}
checkGuess(guessLine);
if(guessesRemaining == 0) {
printLossMessage();
break;
} else if(!workingString().contains("-")) {
printWinMessage();
break;
}
}
}
private void checkGuess(String guessLine) {
char guess = guessLine.charAt(0);
if(guessedLetters.indexOf(guess) != -1) {
println("You have already guessed that letter");
return;
} else if(guessIsCorrect(guess)) {
println("That guess is correct.");
updateWorkingSet(guess);
} else {
println("There are no " + guess + "'s in the word.");
guessesRemaining--;
}
guessedLetters += guess;
}
public void pickWord() {
word = lexicon.getWord(generator.nextInt(lexicon.getWordCount()));
working = new char[word.length()];
Arrays.fill(working, '-');
guessedLetters = "";
}
public String getGuess() {
String guess = readLine("Your guess: ").toUpperCase();
if(guess.equals("") || guess.charAt(0) < 'A' || guess.charAt(0) > 'Z' || (guess.length() > 1 && !guess.equalsIgnoreCase(word))) {
println("Invalid guess!");
return getGuess();
}
return guess;
}
public boolean guessIsCorrect(char ch) {
return (word.indexOf(ch) != -1);
}
public void updateWorkingSet(char ch) {
for(int i = 0; i < word.length(); i++)
if(word.charAt(i) == ch)
working[i] = ch;
}
public String workingString() {
return new String(working);
}
public void printNumGuesses() {
if(guessesRemaining > 1) {
println("You have " + guessesRemaining + " guesses left.");
} else {
println("You have only one guess left.");
}
}
public void printWinMessage() {
println("You guessed the word: " + word + "\nYou win.");
}
public void printLossMessage() {
println("You are completely hung.");
println("The word was: " + word);
println("You lose.");
}
public boolean playAgain() {
println();
String ans = readLine("Play again? (y/n)").toLowerCase();
if(ans.equals("y"))
return true;
else if(ans.equals("n"))
return false;
else
return playAgain();
}
}