forked from NatashaTheRobot/Stanford-CS-106A
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHangman.java
executable file
·135 lines (114 loc) · 3.53 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
/*
* 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.*;
public class Hangman extends ConsoleProgram {
private HangmanLexicon hangmanWords;
private HangmanCanvas canvas;
private RandomGenerator rgen = RandomGenerator.getInstance();
/** Tracks the number of guesses the player has */
private int guessCounter = 8;
public void run() {
setUpGame();
playGame();
}
//This is the word being guessed
private String hiddenWord;
//This is the secret word
private String word = pickWord();
//This is the latest character entered by the user
private char ch;
//This string keeps track of all the incorrect guessed letters
private String incorrectLetters = "";
/*Set up the game by displaying the welcome message,
*how many letters there are in the word,
*and how many guesses the user has
*/
private void setUpGame() {
canvas.reset();
hiddenWord = showNumberOfLetters();
canvas.displayWord(hiddenWord);
println("Welcome to Hangman!");
println("The word now looks like this: " + hiddenWord);
println("You have " + guessCounter + " guesses left.");
}
//Generates a random word selected from the HangmanLexicon
private String pickWord() {
hangmanWords = new HangmanLexicon();
int randomWord = rgen.nextInt(0, (hangmanWords.getWordCount() - 1));
String pickedWord = hangmanWords.getWord(randomWord);
return pickedWord;
}
//Shows how many letters there are in the word as part of game setup
private String showNumberOfLetters() {
String result = "";
for(int i = 0; i< word.length(); i++) {
result = result + "-";
}
return result;
}
private void playGame() {
while(guessCounter > 0) {
String getChar = readLine("Your guess: ");
while (true) {
if(getChar.length() > 1) {
getChar = readLine("You can only guess one letter at a time. Try again: ");
}
if(getChar.length() == 1) break;
}
ch = getChar.charAt(0);
if(Character.isLowerCase(ch)) {
ch = Character.toUpperCase(ch);
}
letterCheck();
if (hiddenWord.equals(word)) {
println("You guessed the word: " + word);
println("You win");
break;
}
println("The word now looks like this: " + hiddenWord);
println("You have " + guessCounter + " guesses left.");
}
if (guessCounter == 0) {
println("You're completely hung.");
println("The word was:" + word);
println("You lose.");
}
}
//updates the hiddenWord if the character entered is correct
private void letterCheck() {
//checks to see if the guessed letter is in the word
if(word.indexOf(ch) == -1) {
println("There are no " + ch + "'s in the word");
guessCounter--;
incorrectLetters = incorrectLetters + ch;
canvas.noteIncorrectGuess(incorrectLetters);
}
if(word.indexOf(ch) != -1) {
println("The guess is correct.");
}
//goes through each of the letters in the word and checks if it matches the guessed letter,
//if it's a match, updates the hidden word to reveal the position of the guessed letter
for(int i = 0; i < word.length(); i++) {
if (ch == word.charAt(i)) {
if (i > 0) {
hiddenWord = hiddenWord.substring(0, i) + ch + hiddenWord.substring(i + 1);
}
if(i == 0) {
hiddenWord = ch + hiddenWord.substring(1);
}
canvas.displayWord(hiddenWord);
}
}
}
public void init() {
canvas = new HangmanCanvas();
add(canvas);
}
}