This repository has been archived by the owner on May 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.java
303 lines (280 loc) · 7.66 KB
/
Game.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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
/**
* {@code Game} class provides the actual HangMan game and and all associated
* utilities for the same. It is highly dependent on the {@code HangMan} for
* validaing each and every letter guessed by the current user. This class
* implements both {@code ActionListener} and {@code KeyListener} for purpose of
* taking input from both mouse click and keyboard (specifically for gueesing
* the letter).
*
* <p>
* KeyListener might be a bit glitched since it demands Window Focus. I'm
* working on solution to solve the same.
* </p>
*
* <p>
* The class provides for a pluggable panel {@code p}.
* </p>
*
* <p>
* Copyright (c) 2021. All rights reserved to Emmanuel Jojy. Use is subject to
* the above conditions.
* </p>
*
* @author Emmanuel Jojy
*
*/
public class Game implements ActionListener, KeyListener {
/**
* Pluggable panel which is plugged in to the frame generated by the {@code GUI}
* class.
*/
JPanel p;
private JPanel plug = null;
private JLabel lgus, lcrt, lwrg, head, status;
private JButton end, exit;
private HangMan hm;
private ArrayList<JButton> alpha = new ArrayList<JButton>();
/**
* Default constrctor provided for {@code Game} class. Initializes the pluggable
* JPanel p, creates instances of {@code Figure} class.
*
* To be invoked only from {@code GUI} class.
*/
public Game() {
p = new JPanel();
p.setSize(1366, 720);
p.setLayout(null);
p.addKeyListener(this);
p.setFocusable(true);
p.requestFocus();
hm = Main.hm;
head = new JLabel("Welcome, to HangMan!", JLabel.CENTER);
head.setBounds(283, 10, 800, 40);
head.setFont(new Font("Calibri", Font.PLAIN, 28));
p.add(head);
status = new JLabel("");
status.setBounds(20, 600, 450, 50);
status.setFont(new Font("Consolas", Font.PLAIN, 14));
p.add(status);
lgus = new JLabel("", JLabel.CENTER);
lgus.setBounds(50, 100, 580, 50);
lgus.setFont(new Font("Consolas", Font.PLAIN, 38));
lcrt = new JLabel("ACCURATE : ", JLabel.LEFT);
lcrt.setBounds(50, 220, 400, 30);
lcrt.setFont(new Font("Calibri", Font.PLAIN, 20));
lwrg = new JLabel("INCORRECT: ", JLabel.LEFT);
lwrg.setBounds(50, 255, 400, 30);
lwrg.setFont(new Font("Calibri", Font.PLAIN, 20));
p.add(lgus);
p.add(lcrt);
p.add(lwrg);
addKey();
update();
end = new JButton("END GAME");
end.setBounds(250, 580, 100, 30);
end.addActionListener(this);
p.add(end);
exit = new JButton("EXIT GAME");
exit.setBounds(633, 580, 100, 30);
exit.setVisible(false);
exit.addActionListener(this);
p.add(exit);
p.setVisible(true);
}
private void addKey() {
int i, x = 20, y = 300;
char ch;
for (i = 0; i < 26; i++) {
ch = (char) (i + 65);
JButton key = new JButton(ch + "");
if (i % 9 == 0) {
y += 60;
x = i == 18 ? 50 + 5 : 20;
}
key.setBounds(x, y, 50, 50);
x += 50 + 20;
alpha.add(key);
key.addActionListener(this);
p.add(key);
}
}
private void disableAll() {
for (int i = 0; i < 26; i++) {
alpha.get(i).setEnabled(false);
}
end.setVisible(false);
exit.setVisible(true);
complete();
}
private void complete() {
String query;
ResultSet res;
Statement st = Main.st;
int tot = 1, win = 0, loss = 0;
try {
query = "SELECT * FROM game WHERE NAME = '" + GUI.name + "';";
res = st.executeQuery(query);
res.next();
tot = res.getInt("TOTAL") + 1;
win = res.getInt("WIN");
loss = res.getInt("LOSS");
query = "DELETE FROM game WHERE NAME = '" + GUI.name + "';";
st.executeUpdate(query);
} catch (SQLException e) {
}
if (hm.win == true) {
head.setText("You guessed the word right");
query = "INSERT INTO game(NAME, TOTAL, WIN, LOSS) VALUES ('" + GUI.name + "', " + tot + ", " + (win + 1)
+ ", " + loss + ");";
} else if (hm.tries == 6) {
head.setText("Sorry, You Lost! The word was " + hm.word);
query = "INSERT INTO game(NAME, TOTAL, WIN, LOSS) VALUES ('" + GUI.name + "', " + tot + ", " + win + ", "
+ (loss + 1) + ");";
} else {
head.setText("You have end the game!");
query = "INSERT INTO game(NAME, TOTAL, WIN, LOSS) VALUES ('" + GUI.name + "', " + tot + ", " + win + ", "
+ loss + ");";
}
try {
st.executeUpdate(query);
} catch (SQLException e) {
System.out.println("Could Not Update!" + e);
}
}
private void update() {
try {
plug.setVisible(false);
p.remove(plug);
} catch (NullPointerException e) {
}
lgus.setText(hm.guess);
if (!hm.crt.isEmpty())
lcrt.setText("ACCURATE : " + hm.crt.toString());
if (!hm.wrg.isEmpty())
lwrg.setText("INCORRECT: " + hm.wrg.toString());
plug = new Figure();
p.add(plug);
status.setText("Used Life: " + hm.tries + "/6.");
p.setFocusable(true);
p.requestFocusInWindow();
}
/**
* Overriden method of {@code ActionListener}. Refer original documentation of
* the same for more details.
*/
@Override
public void actionPerformed(ActionEvent ae) {
JButton key = (JButton) ae.getSource();
if (key == exit) {
GUI.dashboard();
return;
}
if (key == end) {
disableAll();
return;
}
char ch = key.getText().charAt(0);
hm.validate(ch);
update();
key.setEnabled(false);
if (hm.win == true || hm.tries == 6)
disableAll();
}
/**
* Overriden method of {@code KeyListener}. Refer original documentation of the
* same for more details.
*/
@Override
public void keyPressed(KeyEvent e) {
char ch = Character.toUpperCase(e.getKeyChar());
if (ch > 64 && ch < 91) {
JButton key = alpha.get(ch - 65);
if (key.isEnabled()) {
hm.validate(ch);
update();
key.setEnabled(false);
} else {
status.setText(ch + " already guessed");
}
} else {
status.setText(ch + " Invalid Key");
}
if (hm.win == true || hm.tries == 6)
disableAll();
}
/**
* Overriden method of {@code KeyListener}. Empty body override. Refer original
* documentation of the same for more details.
*/
@Override
public void keyReleased(KeyEvent e) {
}
/**
* Overriden method of {@code KeyListener}. Empty body override. Refer original
* documentation of the same for more details.
*/
@Override
public void keyTyped(KeyEvent e) {
}
}
/**
* {@code Figure} draws the actual hangman and encapsulates it into a
* {@code Figure} object. The class extends {@code JPanel} so is in itself a
* JPanel. Mere creation of the {@code Figure} instance generates a pluggable
* {@code JPanel}.
*
* <p>
* The class in itself provides for a pluggable panel {@code p}.
* </p>
*
* <p>
* Copyright (c) 2021. All rights reserved to Emmanuel Jojy. Use is subject to
* the above conditions. Special thanks to Bivin C Benny for help in the
* Graphics class.
* </p>
*
* @author Emmanuel Jojy
*
*/
class Figure extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private int tries;
/**
* Iniatializes plug and other relevant fields. Dimensio set to 683 X 720
*/
public Figure() {
tries = Main.hm.tries;
setBounds(683, 0, 683, 720);
}
/**
* Draws the HangMan using standard methods in the Graphics class. Refer
* Graphics Class.
*/
public void paintComponent(Graphics g) {
g.setColor(Color.black);
g.drawLine(250, 150, 250, 550); // Vertical Bar
g.drawLine(225, 550, 375, 550); // Base
g.drawLine(250, 150, 450, 150); // Horizontal Bar
g.drawLine(450, 150, 450, 200); // Rope
if (tries > 0)
g.drawOval(413, 200, 76, 76); // Head
if (tries > 1)
g.drawLine(450, 276, 450, 430); // Body
if (tries > 2)
g.drawLine(450, 293, 410, 348); // Left Hand
if (tries > 3)
g.drawLine(450, 293, 490, 348); // Right Hand
if (tries > 4)
g.drawLine(450, 430, 400, 490); // Left Leg
if (tries > 5)
g.drawLine(450, 430, 500, 490); // Right Leg
}
}