-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHangManGameLinkedList.java
91 lines (76 loc) · 2.05 KB
/
HangManGameLinkedList.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
package hangman;
/**
* A class to play the hangman game.
*/
public class HangManGameLinkedList {
/** The hangman representation. */
private HangMan hangman;
/** The game board state. */
private GameBoard board;
/** The prompt to the user. */
private Prompt prompt;
/** The word to guess. */
private String word;
/**
* Creates a new HangManGame object.
*/
public HangManGameLinkedList(HangMan hangman, GameBoard board, String word) {
this.hangman = hangman;
this.board = board;
this.prompt = new ConsolePrompt();
this.word = word;
}
/**
* Plays the hangman game.
*/
public void play() {
boolean endOfGame = false;
char guess;
prompt.display(board.toString());
do {
guess = prompt.nextLetter();
if (guess == 0) {
endOfGame = true;
}
if (board.isPriorGuess(guess)) {
prompt.display("You guessed " + guess + " already!");
prompt.display("guess: " + board);
}
if (!board.isPriorGuess(guess)) {
boolean success = board.doMove(guess);
if (!success) {
prompt.display("Bad guess!");
}
else {
prompt.display("Good guess!");
}
}
prompt.display("");
hangman.display(board.currentHungState());
prompt.display(board.previousGuessString());
if (board.inWinningState()) {
prompt.display("You won!");
prompt.display("The word was " + word + "!");
prompt.display("Number of guesses: " + board.numberOfGuesses());
endOfGame = true;
}
else if (board.inLosingState()) {
prompt.display("The word was " + word + "!");
prompt.display("You lose!");
endOfGame = true;
}
else {
prompt.display("");
prompt.display(board.toString());
}
} while (!endOfGame);
}
public static void main(String[] args) {
HangMan hangman = new HangManConsole(System.out);
Words words = new Words("grade2-words.txt");
String word = words.pick();
GameBoard board = new GameBoardLinkedList(word);
HangManGameLinkedList game = new HangManGameLinkedList(hangman, board, word);
game.play();
}
}