This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHangman.java
331 lines (295 loc) · 11.2 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;
import java.sql.*;
import java.util.Random;
@SuppressWarnings("unused")
public class Hangman extends JFrame {
private static final int MAX_WRONG_GUESSES = 6;
private String currentWord;
private StringBuilder guessedWord;
private int wrongGuesses;
private JLabel wordLabel;
private JTextArea statusArea;
private JButton[] letterButtons;
private JButton restartButton;
private JTextArea hangmanImageArea;
public Hangman(String url, String username, String password) {
super("German Word Game");
setExtendedState(JFrame.MAXIMIZED_BOTH);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
getContentPane().setBackground(new Color(240, 240, 240));
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
wordLabel = new JLabel("", SwingConstants.CENTER);
wordLabel.setFont(new Font("Arial", Font.BOLD, 24));
topPanel.add(wordLabel);
add(topPanel, BorderLayout.NORTH);
JPanel centerPanel = new JPanel(new GridLayout(3, 8, 5, 5));
centerPanel.setBackground(new Color(240, 240, 240));
letterButtons = new JButton[26];
for (int i = 0; i < letterButtons.length; i++) {
char letter = (char) ('A' + i);
letterButtons[i] = new JButton(String.valueOf(letter));
letterButtons[i].setFont(new Font("Arial", Font.PLAIN, 18));
letterButtons[i].addActionListener(new LetterButtonListener());
letterButtons[i].setBackground(Color.GRAY);
letterButtons[i].setForeground(Color.WHITE);
centerPanel.add(letterButtons[i]);
}
add(centerPanel, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel bottomPanel = new JPanel(new BorderLayout());
statusArea = new JTextArea(7, 20);
statusArea.setFont(new Font("Arial", Font.PLAIN, 14));
statusArea.setEditable(false);
statusArea.setLineWrap(true);
statusArea.setWrapStyleWord(true);
JScrollPane statusScrollPane = new JScrollPane(statusArea);
bottomPanel.add(statusScrollPane, BorderLayout.CENTER);
restartButton = new JButton("Restart");
restartButton.setFont(new Font("Arial", Font.BOLD, 18));
restartButton.addActionListener(new RestartButtonListener());
restartButton.setVisible(false);
bottomPanel.add(restartButton, BorderLayout.EAST);
hangmanImageArea = new JTextArea(7, 20);
hangmanImageArea.setFont(new Font("Monospaced", Font.PLAIN, 14));
hangmanImageArea.setEditable(false);
hangmanImageArea.setLineWrap(true);
hangmanImageArea.setWrapStyleWord(true);
JScrollPane hangmanScrollPane = new JScrollPane(hangmanImageArea);
bottomPanel.add(hangmanScrollPane, BorderLayout.SOUTH);
add(bottomPanel, BorderLayout.SOUTH);
addKeyListener(new KeyboardInputListener());
initializeGame(url, username, password);
setVisible(true);
addKeyListener(new KeyboardInputListener());
}
private void initializeGame(String url, String username, String password) {
createTableAndDataIfNeeded(url, username, password);
currentWord = getRandomWordFromDatabase(url, username, password);
guessedWord = new StringBuilder();
for (int i = 0; i < currentWord.length(); i++) {
guessedWord.append("_");
}
wordLabel.setText(guessedWord.toString());
wrongGuesses = 0;
updateHangmanImage();
appendStatus("Type in the characters");
enableLetterButtons(true);
}
private void createTableAndDataIfNeeded(String url, String username, String password) {
try (Connection connection = DriverManager.getConnection(url, username, password)) {
if (!tableExists(connection, "GERMAN_WORDS")) {
createGermanWordsTable(connection);
addGermanWords(connection);
} else {
if (getTotalRows(connection) < 10) {
addGermanWords(connection);
}
}
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Exception: " + e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
}
}
private boolean tableExists(Connection connection, String tableName) throws SQLException {
DatabaseMetaData meta = connection.getMetaData();
ResultSet resultSet = meta.getTables(null, null, tableName, new String[]{"TABLE"});
return resultSet.next();
}
private void createGermanWordsTable(Connection connection) throws SQLException {
try (Statement statement = connection.createStatement()) {
statement.executeUpdate(
"CREATE TABLE GERMAN_WORDS (" +
"WORD VARCHAR(50))"
);
}
}
private void addGermanWords(Connection connection) throws SQLException {
String[] words = {
"AUTOMOBIL",
"TISCH",
"BUCH",
"STUHL",
"FENSTER",
"HAUS",
"SCHULE",
"KATZE",
"WASSER",
"COMPUTER"
};
String query = "INSERT INTO GERMAN_WORDS (WORD) VALUES (?)";
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
for (String word : words) {
preparedStatement.setString(1, word);
preparedStatement.executeUpdate();
}
}
}
private int getTotalRows(Connection connection) throws SQLException {
String query = "SELECT COUNT(*) FROM GERMAN_WORDS";
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query)) {
if (resultSet.next()) {
return resultSet.getInt(1);
}
return 0;
}
}
private String getRandomWordFromDatabase(String url, String username, String password) {
String randomWord = null;
try (Connection connection = DriverManager.getConnection(url, username, password)) {
String query = "SELECT WORD FROM GERMAN_WORDS ORDER BY DBMS_RANDOM.VALUE FETCH FIRST 1 ROWS ONLY";
try (PreparedStatement preparedStatement = connection.prepareStatement(query);
ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
randomWord = resultSet.getString("WORD").toUpperCase();
}
}
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Exception: " + e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
}
return randomWord;
}
private void updateGuessedWord(char letter) {
boolean letterFound = false;
for (int i = 0; i < currentWord.length(); i++) {
if (currentWord.charAt(i) == letter) {
guessedWord.setCharAt(i, letter);
letterFound = true;
}
}
if (!letterFound) {
wrongGuesses++;
}
wordLabel.setText(guessedWord.toString());
checkGameStatus();
}
private void checkGameStatus() {
if (guessedWord.toString().equals(currentWord)) {
appendStatus("You win! The word was: " + currentWord);
disableLetterButtons();
restartButton.setVisible(true);
} else if (wrongGuesses >= MAX_WRONG_GUESSES) {
appendStatus("You lose. The word was: " + currentWord);
disableLetterButtons();
restartButton.setVisible(true);
} else {
updateHangmanImage();
}
}
private void updateHangmanImage() {
String[] hangmanImages = {
" +---+\n" +
" | |\n" +
" |\n" +
" |\n" +
" |\n" +
" |\n" +
"=========",
" +---+\n" +
" | |\n" +
" O |\n" +
" |\n" +
" |\n" +
" |\n" +
"=========",
" +---+\n" +
" | |\n" +
" O |\n" +
" | |\n" +
" |\n" +
" |\n" +
"=========",
" +---+\n" +
" | |\n" +
" O |\n" +
" /| |\n" +
" |\n" +
" |\n" +
"=========",
" +---+\n" +
" | |\n" +
" O |\n" +
" /|\\ |\n" +
" |\n" +
" |\n" +
"=========",
" +---+\n" +
" | |\n" +
" O |\n" +
" /|\\ |\n" +
" / |\n" +
" |\n" +
"=========",
" +---+\n" +
" | |\n" +
" O |\n" +
" /|\\ |\n" +
" / \\ |\n" +
" |\n" +
"========="
};
String hangman = wrongGuesses <= MAX_WRONG_GUESSES ? hangmanImages[wrongGuesses] : hangmanImages[MAX_WRONG_GUESSES];
hangmanImageArea.setText(hangman);
}
private void enableLetterButtons(boolean enabled) {
for (JButton button : letterButtons) {
button.setEnabled(enabled);
}
}
private void disableLetterButtons() {
enableLetterButtons(false);
}
private class LetterButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
button.setEnabled(false);
char letter = button.getText().charAt(0);
updateGuessedWord(letter);
}
}
private class RestartButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
restartGame();
}
}
private void restartGame() {
String url = Main.getUrl();
String username = Main.getUsername();
String password = Main.getPassword();
initializeGame(url, username, password);
restartButton.setVisible(false);
}
private void appendStatus(String text) {
statusArea.append(text + "\n");
}
private class KeyboardInputListener implements KeyListener {
@Override
public void keyPressed(KeyEvent e) {
char keyChar = e.getKeyChar();
if (Character.isLetter(keyChar)) {
char letter = Character.toUpperCase(keyChar);
updateGuessedWord(letter);
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
public static void main(String[] args) {
String url = Main.getUrl();
String username = Main.getUsername();
String password = Main.getPassword();
SwingUtilities.invokeLater(() -> new Hangman(url, username, password));
}
}