-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hangman.java
109 lines (103 loc) · 3.74 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
package Java;
// Imports user created libraries.
import java.util.ArrayList;
import java.util.Scanner;
// Creates the public class Hangman.
public class Hangman {
// Defines more private classes.
private ArrayList<String> words;
private String wordToGuess;
private ArrayList<Character> guessedLetters;
private int remainingGuesses;
// Prepares the Hangman function. Deals with the words used in the game.
public Hangman() {
words = new ArrayList<String>();
words.add("programming");
words.add("gaming");
words.add("java");
words.add("rocks");
words.add("minecraft");
words.add("galaxy");
words.add("planet");
words.add("discord");
words.add("harmony");
words.add("religion");
words.add("variables");
words.add("conditionals");
words.add("expressions");
words.add("loops");
words.add("functions");
words.add("classes");
words.add("data");
words.add("structures");
words.add("dungeons");
words.add("dragons");
words.add("party");
wordToGuess = words.get((int) (Math.random() * words.size()));
guessedLetters = new ArrayList<Character>();
remainingGuesses = 6;
}
// Defines the play function. Deals with the background information of figuring
// out if the word matches the guessing word. Does the inputs.
public void play() {
Scanner scanner = new Scanner(System.in);
try {
while (remainingGuesses > 0) {
displayWord();
System.out.print("Enter a letter: ");
char letter = scanner.next().charAt(0);
if (guessedLetters.contains(letter)) {
System.out.println("You already guessed that letter.");
} else {
guessedLetters.add(letter);
if (wordToGuess.indexOf(letter) == -1) {
remainingGuesses--;
System.out.println("Sorry, that letter is not in the word. Remaining guesses: " + remainingGuesses);
} else {
System.out.println("Good guess!");
}
}
if (wordToGuess.equals(getDisplayWord())) {
System.out.println();
System.out.println("Congratulations, you won!");
return;
}
}
System.out.println();
System.out.println("Sorry, you lost. The word was " + wordToGuess);
} finally {
scanner.close();
}
}
// Outputs the word to be displayed.
private void displayWord() {
for (int i = 0; i < wordToGuess.length(); i++) {
char letter = wordToGuess.charAt(i);
if (guessedLetters.contains(letter)) {
System.out.print(letter);
} else {
System.out.print("_");
}
System.out.print(" ");
}
System.out.println();
}
// Updates the word to be displayed.
private String getDisplayWord() {
StringBuilder displayWord = new StringBuilder();
for (int i = 0; i < wordToGuess.length(); i++) {
char letter = wordToGuess.charAt(i);
if (guessedLetters.contains(letter)) {
displayWord.append(letter);
} else {
displayWord.append("_");
}
}
return displayWord.toString();
}
// Runs the game.
public static void main(String[] args) {
Hangman game = new Hangman();
game.play();
}
}