Skip to content

Commit bab4a38

Browse files
authored
Merge pull request #80 from SakshiDosani/hangman
Enhanced Hangman game with hints, score, and used letters tracking
2 parents 596e00c + 2d00500 commit bab4a38

2 files changed

Lines changed: 123 additions & 76 deletions

File tree

Java/Hangman/HangmanGUI.java

Lines changed: 106 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,162 +1,192 @@
11
/**
2-
* 🎮 Hangman GUI Game
2+
* 🎮 Enhanced Hangman GUI Game
33
*
4-
* A simple word-guessing game using Java Swing.
5-
* Players guess letters to figure out a hidden word.
6-
* GUI displays the current word, remaining attempts, messages, and allows restarting.
4+
* Features added for Hacktoberfest:
5+
* ✅ Tracks used letters
6+
* ✅ Provides hints after 3 wrong guesses
7+
* ✅ Displays a score system (wins/losses)
8+
* ✅ Adds color-coded messages for better UX
79
*
8-
*
9-
* Author: Pradyumn Pratap Singh (Strange)
10+
* Author: Sakshi (Hacktoberfest Contribution)
11+
* Original Author: Pradyumn Pratap Singh (Strange)
1012
*/
1113

1214
import javax.swing.*;
1315
import java.awt.*;
1416
import java.awt.event.*;
15-
import java.util.Random;
17+
import java.util.*;
1618

1719
public class HangmanGUI extends JFrame implements ActionListener {
1820

19-
// Array of possible words for the game
20-
private String[] words = {"JAVA", "HANGMAN", "COMPUTER", "PROGRAMMING", "SWING"};
21+
// Word list with hints
22+
private final Map<String, String> wordHints = Map.of(
23+
"JAVA", "A popular programming language",
24+
"HANGMAN", "A classic word-guessing game",
25+
"COMPUTER", "An electronic device for calculations",
26+
"PROGRAMMING", "What developers love to do!",
27+
"SWING", "A Java GUI toolkit"
28+
);
2129

2230
// The current word to guess
2331
private String word;
2432

25-
// Array to store the current guessed letters (e.g., "_ A _ A")
33+
// Array to store guessed letters (e.g., "_ A _ A")
2634
private char[] guessedWord;
2735

28-
// Remaining attempts before the game is over
36+
// Game state variables
2937
private int attempts = 6;
38+
private int wins = 0;
39+
private int losses = 0;
40+
private Set<Character> usedLetters = new HashSet<>();
3041

3142
// Swing components
32-
private JLabel wordLabel, attemptsLabel, messageLabel;
43+
private JLabel wordLabel, attemptsLabel, messageLabel, usedLabel, scoreLabel;
3344
private JTextField inputField;
3445
private JButton guessButton, restartButton;
3546

3647
/**
37-
* Constructor to set up the GUI and initialize the game.
48+
* Constructor to set up the GUI and start the first game.
3849
*/
3950
public HangmanGUI() {
40-
// Select a random word from the list
41-
word = words[new Random().nextInt(words.length)];
42-
43-
// Initialize guessedWord array with underscores
44-
guessedWord = new char[word.length()];
45-
for (int i = 0; i < guessedWord.length; i++) guessedWord[i] = '_';
51+
setupUI();
52+
startNewGame();
53+
}
4654

47-
// JFrame properties
48-
setTitle("🎮 Hangman Game");
49-
setSize(400, 250);
55+
/**
56+
* Initializes GUI components and layout.
57+
*/
58+
private void setupUI() {
59+
setTitle("🎮 Enhanced Hangman Game");
60+
setSize(450, 320);
5061
setDefaultCloseOperation(EXIT_ON_CLOSE);
5162
setLocationRelativeTo(null);
52-
setLayout(new GridLayout(5, 1, 5, 5));
63+
setLayout(new GridLayout(7, 1, 5, 5));
5364

54-
// Label to display the guessed word
55-
wordLabel = new JLabel(new String(guessedWord), SwingConstants.CENTER);
56-
wordLabel.setFont(new Font("Segoe UI", Font.BOLD, 24));
65+
wordLabel = new JLabel("", SwingConstants.CENTER);
66+
wordLabel.setFont(new Font("Segoe UI", Font.BOLD, 28));
5767

58-
// Label to show remaining attempts
59-
attemptsLabel = new JLabel("Attempts left: " + attempts, SwingConstants.CENTER);
68+
attemptsLabel = new JLabel("", SwingConstants.CENTER);
69+
messageLabel = new JLabel("", SwingConstants.CENTER);
70+
usedLabel = new JLabel("", SwingConstants.CENTER);
71+
scoreLabel = new JLabel("Score: 0 Wins | 0 Losses", SwingConstants.CENTER);
6072

61-
// Label to display messages or prompts
62-
messageLabel = new JLabel("Enter a letter:", SwingConstants.CENTER);
63-
64-
// Text field for player to enter a guess
6573
inputField = new JTextField();
66-
67-
// Buttons for guessing and restarting the game
6874
guessButton = new JButton("Guess");
6975
restartButton = new JButton("Restart");
7076

71-
// Add action listeners
7277
guessButton.addActionListener(this);
73-
restartButton.addActionListener(e -> restartGame());
78+
restartButton.addActionListener(e -> startNewGame());
79+
80+
JPanel bottomPanel = new JPanel(new FlowLayout());
81+
bottomPanel.add(guessButton);
82+
bottomPanel.add(restartButton);
7483

75-
// Add components to JFrame
7684
add(wordLabel);
7785
add(attemptsLabel);
7886
add(messageLabel);
7987
add(inputField);
80-
81-
// Bottom panel for buttons
82-
JPanel bottomPanel = new JPanel(new FlowLayout());
83-
bottomPanel.add(guessButton);
84-
bottomPanel.add(restartButton);
88+
add(usedLabel);
89+
add(scoreLabel);
8590
add(bottomPanel);
8691

87-
// Make GUI visible
8892
setVisible(true);
8993
}
9094

95+
/**
96+
* Starts or restarts a new round of the game.
97+
*/
98+
private void startNewGame() {
99+
// Pick a random word
100+
List<String> keys = new ArrayList<>(wordHints.keySet());
101+
word = keys.get(new Random().nextInt(keys.size()));
102+
103+
guessedWord = new char[word.length()];
104+
Arrays.fill(guessedWord, '_');
105+
attempts = 6;
106+
usedLetters.clear();
107+
108+
// Update GUI
109+
updateLabels();
110+
messageLabel.setText("Enter a letter:");
111+
messageLabel.setForeground(Color.BLACK);
112+
guessButton.setEnabled(true);
113+
inputField.setText("");
114+
}
115+
91116
/**
92117
* Handles the Guess button click event.
93118
*/
94119
@Override
95120
public void actionPerformed(ActionEvent e) {
96-
String input = inputField.getText().toUpperCase(); // Convert to uppercase
97-
inputField.setText(""); // Clear input field
121+
String input = inputField.getText().toUpperCase();
122+
inputField.setText("");
98123

99-
// Validate input
100-
if (input.length() != 1) {
101-
messageLabel.setText("Enter only one letter!");
124+
if (input.length() != 1 || !Character.isLetter(input.charAt(0))) {
125+
messageLabel.setText("⚠️ Please enter a single letter!");
126+
messageLabel.setForeground(Color.ORANGE);
102127
return;
103128
}
104129

105130
char guess = input.charAt(0);
131+
if (usedLetters.contains(guess)) {
132+
messageLabel.setText("You already tried '" + guess + "'!");
133+
messageLabel.setForeground(Color.GRAY);
134+
return;
135+
}
136+
137+
usedLetters.add(guess);
106138
boolean correct = false;
107139

108-
// Check if the guessed letter is in the word
109140
for (int i = 0; i < word.length(); i++) {
110141
if (word.charAt(i) == guess) {
111142
guessedWord[i] = guess;
112143
correct = true;
113144
}
114145
}
115146

116-
// Update attempts if guess is incorrect
117-
if (!correct) attempts--;
147+
if (!correct) {
148+
attempts--;
149+
messageLabel.setText("❌ Wrong! Attempts left: " + attempts);
150+
messageLabel.setForeground(Color.RED);
151+
} else {
152+
messageLabel.setText("✅ Good guess!");
153+
messageLabel.setForeground(Color.GREEN);
154+
}
118155

119-
// Update GUI labels
120-
wordLabel.setText(new String(guessedWord));
121-
attemptsLabel.setText("Attempts left: " + attempts);
156+
// Reveal hint if 3 attempts remain
157+
if (attempts == 3) {
158+
messageLabel.setText("<html>Hint: " + wordHints.get(word) + "</html>");
159+
messageLabel.setForeground(new Color(0, 128, 255));
160+
}
161+
162+
updateLabels();
122163

123-
// Check for win condition
124164
if (new String(guessedWord).equals(word)) {
165+
wins++;
125166
messageLabel.setText("🎉 You Won! Word: " + word);
167+
messageLabel.setForeground(new Color(0, 200, 0));
126168
guessButton.setEnabled(false);
127-
}
128-
// Check for loss condition
129-
else if (attempts == 0) {
169+
} else if (attempts == 0) {
170+
losses++;
130171
messageLabel.setText("💀 Game Over! Word: " + word);
172+
messageLabel.setForeground(Color.RED);
131173
guessButton.setEnabled(false);
132174
}
175+
176+
scoreLabel.setText("Score: " + wins + " Wins | " + losses + " Losses");
133177
}
134178

135179
/**
136-
* Resets the game to start a new round.
180+
* Updates labels showing game state.
137181
*/
138-
private void restartGame() {
139-
// Select a new random word
140-
word = words[new Random().nextInt(words.length)];
141-
142-
// Reset guessed word
143-
guessedWord = new char[word.length()];
144-
for (int i = 0; i < guessedWord.length; i++) guessedWord[i] = '_';
145-
146-
// Reset attempts
147-
attempts = 6;
148-
149-
// Update GUI labels
182+
private void updateLabels() {
150183
wordLabel.setText(new String(guessedWord));
151184
attemptsLabel.setText("Attempts left: " + attempts);
152-
messageLabel.setText("Enter a letter:");
153-
154-
// Enable guess button
155-
guessButton.setEnabled(true);
185+
usedLabel.setText("Used letters: " + usedLetters.toString());
156186
}
157187

158188
/**
159-
* Main method to start the game.
189+
* Main method to run the game.
160190
*/
161191
public static void main(String[] args) {
162192
SwingUtilities.invokeLater(HangmanGUI::new);

Java/Hangman/Readme.MD

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 🎮 Enhanced Hangman GUI
2+
3+
This is an upgraded version of a Java Swing Hangman game — created as a **Hacktoberfest contribution**.
4+
5+
### ✨ Features
6+
- Tracks used letters
7+
- Displays hints after 3 wrong attempts
8+
- Shows a live score (wins/losses)
9+
- Color-coded messages for feedback
10+
- Simple, interactive GUI using Java Swing
11+
12+
### 🚀 How to Run
13+
1. Open the project in any Java IDE (like IntelliJ or VS Code with Java extensions).
14+
2. Compile and run:
15+
```bash
16+
javac HangmanGUI.java
17+
java HangmanGUI

0 commit comments

Comments
 (0)