This repository has been archived by the owner on Oct 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
HangmanGame.java
74 lines (63 loc) · 2.48 KB
/
HangmanGame.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
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class HangmanGame {
private static final String[] WORDS = {
"java", "programming", "hangman", "development", "computer", "language", "code"
};
private static final int MAX_ATTEMPTS = 6;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String wordToGuess = getRandomWord();
List<Character> guessedLetters = new ArrayList<>();
int attemptsLeft = MAX_ATTEMPTS;
System.out.println("Welcome to Hangman!");
System.out.println("You have " + MAX_ATTEMPTS + " attempts to guess the word.");
while (attemptsLeft > 0) {
displayWord(wordToGuess, guessedLetters);
System.out.print("Guess a letter: ");
char guess = scanner.nextLine().toLowerCase().charAt(0);
if (guessedLetters.contains(guess)) {
System.out.println("You've already guessed that letter. Try again.");
} else {
guessedLetters.add(guess);
if (wordToGuess.indexOf(guess) == -1) {
attemptsLeft--;
System.out.println("Wrong guess! Attempts left: " + attemptsLeft);
}
}
if (isWordGuessed(wordToGuess, guessedLetters)) {
System.out.println("Congratulations! You've guessed the word: " + wordToGuess);
break;
}
}
if (attemptsLeft == 0) {
System.out.println("Game Over! The word was: " + wordToGuess);
}
scanner.close();
}
private static String getRandomWord() {
Random random = new Random();
return WORDS[random.nextInt(WORDS.length)];
}
private static void displayWord(String word, List<Character> guessedLetters) {
StringBuilder display = new StringBuilder();
for (char letter : word.toCharArray()) {
if (guessedLetters.contains(letter)) {
display.append(letter).append(" ");
} else {
display.append("_ ");
}
}
System.out.println("Word: " + display.toString().trim());
}
private static boolean isWordGuessed(String word, List<Character> guessedLetters) {
for (char letter : word.toCharArray()) {
if (!guessedLetters.contains(letter)) {
return false;
}
}
return true;
}
}