-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathhangman.java
More file actions
92 lines (76 loc) · 3.19 KB
/
hangman.java
File metadata and controls
92 lines (76 loc) · 3.19 KB
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
import java.util.Scanner;
public class HangmanGame {
private static final String[] WORDS = { "java", "python", "programming", "hangman", "computer" };
private static final int MAX_TRIES = 6;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean playAgain = true;
while (playAgain) {
String secretWord = selectRandomWord(WORDS);
String guessedWord = initializeGuessedWord(secretWord);
int tries = 0;
boolean isGameOver = false;
System.out.println("Welcome to Hangman!");
System.out.println("Guess the word:");
while (!isGameOver) {
displayHangman(tries);
displayWord(guessedWord);
char guess = getGuess(scanner);
if (secretWord.contains(String.valueOf(guess))) {
guessedWord = updateGuessedWord(secretWord, guessedWord, guess);
} else {
tries++;
System.out.println("Incorrect guess. Tries left: " + (MAX_TRIES - tries));
}
if (guessedWord.equals(secretWord) || tries >= MAX_TRIES) {
isGameOver = true;
if (guessedWord.equals(secretWord)) {
System.out.println("Congratulations! You guessed the word: " + secretWord);
} else {
System.out.println("Game over! The word was: " + secretWord);
}
}
}
System.out.print("Play again? (yes/no): ");
String playAgainInput = scanner.next().toLowerCase();
playAgain = playAgainInput.equals("yes");
}
System.out.println("Thanks for playing Hangman!");
scanner.close();
}
private static String selectRandomWord(String[] words) {
int randomIndex = (int) (Math.random() * words.length);
return words[randomIndex];
}
private static String initializeGuessedWord(String word) {
StringBuilder builder = new StringBuilder(word.length());
for (int i = 0; i < word.length(); i++) {
builder.append("_");
}
return builder.toString();
}
private static void displayHangman(int tries) {
// Implement hangman drawing here
}
private static void displayWord(String word) {
System.out.println("Word: " + word);
}
private static char getGuess(Scanner scanner) {
System.out.print("Enter a letter: ");
String input = scanner.next().toLowerCase();
if (input.length() != 1 || !Character.isLetter(input.charAt(0))) {
System.out.println("Invalid input. Please enter a single letter.");
return getGuess(scanner);
}
return input.charAt(0);
}
private static String updateGuessedWord(String secretWord, String guessedWord, char guess) {
StringBuilder builder = new StringBuilder(guessedWord);
for (int i = 0; i < secretWord.length(); i++) {
if (secretWord.charAt(i) == guess) {
builder.setCharAt(i, guess);
}
}
return builder.toString();
}
}