|
1 | | -import java.util.*; |
| 1 | +```java |
| 2 | +import java.util.Random; |
| 3 | +import java.util.Scanner; |
2 | 4 |
|
3 | 5 | class Main { |
4 | | - public static void main(String args[]) { |
5 | | - Hang hm = new Hang(); |
6 | | - hm.Generate(); |
| 6 | + public static void main(String[] args) { |
| 7 | + Hangman game = new Hangman(); |
| 8 | + game.start(); |
7 | 9 | } |
8 | 10 | } |
9 | 11 |
|
10 | | -class Hang { |
11 | | - Random rd = new Random(); |
12 | | - Scanner sc = new Scanner(System.in); |
13 | | - String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
14 | | - int n = s.length(); |
15 | | - char[] c = new char[6]; // Array for random letters |
16 | | - char[] c1 = {'_', '_', '_', '_', '_', '_'}; // Array to display guessed letters |
17 | | - int maxAttempts = 6; // Maximum allowed attempts |
18 | | - |
19 | | - void Generate() { |
20 | | - // Generate a random 6-letter word |
21 | | - for (int i = 0; i < 6; i++) { |
22 | | - c[i] = s.charAt(rd.nextInt(n)); |
23 | | - } |
24 | | - |
25 | | - System.out.println("Guess the 6-letter word:"); |
| 12 | +class Hangman { |
| 13 | + private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 14 | + private static final int WORD_LENGTH = 6; |
| 15 | + private final Random random = new Random(); |
| 16 | + private final Scanner scanner = new Scanner(System.in); |
| 17 | + private final char[] word = new char[WORD_LENGTH]; |
| 18 | + private final char[] display = new char[WORD_LENGTH]; |
| 19 | + private int attemptsRemaining = 6; |
| 20 | + |
| 21 | + public void start() { |
| 22 | + generateWord(); |
| 23 | + System.out.println("Guess the " + WORD_LENGTH + "-letter word:"); |
26 | 24 | displayWord(); |
27 | | - Function(); |
28 | | - } |
29 | | - |
30 | | - void displayWord() { |
31 | | - // Display the current state of the guessed word |
32 | | - for (int i = 0; i < 6; i++) { |
33 | | - System.out.print(c1[i] + " "); |
34 | | - } |
35 | | - System.out.println(); |
| 25 | + play(); |
36 | 26 | } |
37 | 27 |
|
38 | | - void Function() { |
39 | | - int attempts = 0; |
40 | | - |
41 | | - // Loop until the word is guessed or attempts are exhausted |
42 | | - while (attempts < maxAttempts && !isWordGuessed()) { |
43 | | - System.out.println("Enter your guess (a single letter): "); |
44 | | - char guess = sc.nextLine().toUpperCase().charAt(0); |
45 | | - |
46 | | - boolean correctGuess = false; |
47 | | - |
48 | | - // Check if the guessed letter is in the word |
49 | | - for (int i = 0; i < 6; i++) { |
50 | | - if (c[i] == guess && c1[i] == '_') { |
51 | | - c1[i] = guess; |
52 | | - correctGuess = true; |
53 | | - } |
54 | | - } |
55 | | - |
56 | | - // If the guess was incorrect, increment attempts |
57 | | - if (!correctGuess) { |
58 | | - attempts++; |
59 | | - System.out.println("Wrong guess! Attempts left: " + (maxAttempts - attempts)); |
60 | | - } |
61 | | - |
62 | | - // Display the current state of the word |
63 | | - displayWord(); |
64 | | - } |
65 | | - |
66 | | - // Check if the word was fully guessed |
67 | | - if (isWordGuessed()) { |
68 | | - System.out.println("You've guessed the word correctly."); |
69 | | - } else { |
70 | | - System.out.println("You've run out of attempts. The word was: " + Arrays.toString(c)); |
| 28 | + private void generateWord() { |
| 29 | + for (int i = 0; i < WORD_LENGTH; i++) { |
| 30 | + word[i] = ALPHABET.charAt(random.nextInt(ALPHABET.length())); |
| 31 | + display[i] = '_'; |
71 | 32 | } |
72 | 33 | } |
73 | 34 |
|
74 | | - boolean isWordGuessed() { |
75 | | - for (char ch : c1) { |
76 | | - if (ch == '_') { |
77 | | - return false; |
78 | | - } |
| 35 | + private void displayWord() { |
| 36 | + for (int i = 0; i < WORD_LENGTH; i++) { |
| 37 | + System.out.print(display[i] + " "); |
79 | 38 | } |
80 | | - return true; |
| 39 | + System.out.println(); |
81 | 40 | } |
82 | | -} |
| 41 | +``` |
0 commit comments