-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGLib.java
202 lines (185 loc) · 5.63 KB
/
GLib.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
import javax.swing.*;
import java.awt.event.*;
public class GLib {
private JFrame frame;
private Computer cpu;
private int menu, menuSel, menuItems;
private JLabel line1, line2, line3, line4, line5, line6, line7;
private JTextField entryBox;
private boolean gameActive = false;
public GLib(Computer computer) {
cpu = computer;
frame = new JFrame();
frame.setSize(800, 600);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.addKeyListener(new KeyListener(this));
menu = 1;
menuSel = 1;
menuItems = 2;
// * creation of the menu lines;
line1 = new JLabel("----------------WELCOME-TO-NIM----------------");
line2 = new JLabel();
line3 = new JLabel();
line4 = new JLabel();
line5 = new JLabel();
line6 = new JLabel();
line7 = new JLabel();
entryBox = new JTextField("");
line1.setBounds(0, 12, 800, 12);
line2.setBounds(0, 24, 800, 12);
line3.setBounds(0, 36, 800, 12);
line4.setBounds(0, 48, 800, 12);
line5.setBounds(0, 60, 800, 12);
line6.setBounds(0, 72, 800, 12);
line7.setBounds(0, 84, 800, 12);
entryBox.setBounds(0, 96, 800, 20);
frame.add(line1);
frame.add(line2);
frame.add(line3);
frame.add(line4);
frame.add(line5);
frame.add(line6);
frame.add(line7);
frame.add(entryBox); // * adds labels
line1.setVisible(true);
line2.setVisible(true);
line3.setVisible(true);
line4.setVisible(true);
line5.setVisible(true);
line6.setVisible(true);
line7.setVisible(true); // * sets lables visible
entryBox.setVisible(false);
updateMenu();
}
public void menu(String key) {
if (!gameActive) {
if (key.equals("down")) {
if (menuSel + 1 > menuItems)
menuSel = 1;
else
menuSel += 1;
updateMenu();
} else if (key.equals("up")) {
if (menuSel - 1 < 1)
menuSel = menuItems;
else
menuSel -= 1;
updateMenu();
} else if (key.equals("enter"))
selectItem();
}
}
public void updateMenu() {
line3.setText("NUMBER OF PLAYERS");
if (menu == 1) {
if (menuSel == 1) {
line4.setText("1 PLAYER <<");
line5.setText("2 PLAYER");
} else if (menuSel == 2) {
line4.setText("1 PLAYER");
line5.setText("2 PLAYER <<");
}
} else if (menu == 2) {
line3.setText("SET DIFFICULTY");
if (menuSel == 1) {
line4.setText("EASY <<");
line5.setText("MEDIUM");
line6.setText("HARD");
} else if (menuSel == 2) {
line4.setText("EASY");
line5.setText("MEDIUM <<");
line6.setText("HARD");
} else if (menuSel == 3) {
line4.setText("EASY");
line5.setText("MEDIUM");
line6.setText("HARD <<");
}
}
}
public void selectItem() {
if (menu == 1) {
if (menuSel == 1) {
cpu.computerActive(true);
menu = 2;
menuItems = 3;
updateMenu();
} else {
cpu.computerActive(false);
setupGame();
}
} else if (menu == 2) {
if (menuSel == 1) {
cpu.setDiff("easy");
} else if (menuSel == 2) {
cpu.setDiff("medium");
} else {
cpu.setDiff("hard");
;
}
setupGame();
}
}
public void displayMsg(String msg) {
line6.setText(msg);
}
public void updateSticks(int sticks) {
String sticksString = " ";
for (int i = 0; i < sticks; i++) {
sticksString += "| ";
}
line3.setText(sticksString);
line4.setText("Sticks left: " + sticks);
}
public void setupGame() {
clearScreen();
if (!gameActive) {
gameActive = true;
updateSticks(Board.getSticks());
entryBox.setVisible(true);
entryBox.addActionListener(new ActionListenerNew(this));
} else {
updateSticks(Board.getSticks());
}
}
public void clearScreen() {
line2.setText("");
line3.setText("");
line4.setText("");
line5.setText("");
line6.setText("");
line7.setText("");
}
public void parseInput() {
String input = entryBox.getText();
int inputSticks = Integer.parseInt(input);
Game.runMove(inputSticks);
entryBox.setText("");
}
}
class KeyListener extends KeyAdapter {
private GLib glib;
public KeyListener(GLib lib) {
glib = lib;
}
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_UP)
glib.menu("up");
if (code == KeyEvent.VK_DOWN)
glib.menu("down");
if (code == KeyEvent.VK_ENTER)
glib.menu("enter");
}
}
class ActionListenerNew implements ActionListener {
private GLib glib;
public ActionListenerNew(GLib lib) {
glib = lib;
}
public void actionPerformed(ActionEvent e) {
glib.parseInput();
}
}