-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNIM753.java
More file actions
333 lines (220 loc) · 7.61 KB
/
Copy pathNIM753.java
File metadata and controls
333 lines (220 loc) · 7.61 KB
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
331
332
333
// Created by Jacob Schnettler
// Mr. L NIM 753
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.util.Random;
public class NIM753 extends Applet implements ActionListener {
String Title = "Advanced Nim 7 5 3";
Button firstPileBTN = new Button("Take.");
Button secondPileBTN = new Button("Take.");
Button thirdPileBTN = new Button("Take.");
Button fourthPileBTN = new Button("Take.");
Button resetGameBTN = new Button("Restart Game.");
Button startGameBTN = new Button("Start Game.");
Button switchPlayerBTN = new Button("Switch Player.");
Font title = new Font("Ariel", 1, 40);
Font subTitle = new Font("Ariel", 1, 20);
Font paragraph = new Font("Ariel", 1, 20);
Font heading = new Font("Ariel", 1, 15);
Random rand = new Random();
int firstRow = 0;
int secondRow = 0;
int thirdRow = 0;
int fourthRow = 0;
int take = 0;
int player = 1;
int chipsTaken = 0;
String input = "";
boolean gameEnded = false;
boolean showDirections = true;
public void drawBtn(Button btn, int x, int y) {
btn.addActionListener(this);
btn.setBounds(x, y + 150, 100, 40);
this.add(btn);
}
public void drawRow(int numChips, int x, int y, Graphics g, Color color) {
g.setColor(color);
g.drawString(numChips + " Chips Left", x, y - 25);
for (int n = 1; n <= numChips; n++) {
if (n == 1) {
g.fillOval(
x,
y,
40,
40
);
} else if (n == 2) {
g.fillOval(
x,
y + 50,
40,
40
);
} else if (n == 3) {
g.fillOval(
x + 50,
y,
40,
40
);
} else if (n == 4) {
g.fillOval(
x + 50,
y + 50,
40,
40
);
}
if (n == 1 || n == 2) {
} else if (n == 3 || n == 4) {
//g.fillOval(x + (n - 1) * 50, y + 10, 40, 40);
}
}
}
public void changePlayer() {
switchPlayerBTN.setVisible(false);
chipsTaken = 0;
toggleBoard(true);
if (player == 1) {
player = 2;
} else {
player = 1;
}
}
public void toggleBoard(boolean bool) {
firstPileBTN.setVisible(bool);
secondPileBTN.setVisible(bool);
thirdPileBTN.setVisible(bool);
fourthPileBTN.setVisible(bool);
}
public void init() {
this.setLayout(null);
resetGameBTN.addActionListener(this);
resetGameBTN.setBounds(100, 450, 150, 40);
this.add(resetGameBTN);
resetGameBTN.setVisible(false);
switchPlayerBTN.addActionListener(this);
switchPlayerBTN.setBounds(275, 450, 150, 40);
this.add(switchPlayerBTN);
switchPlayerBTN.setVisible(false);
startGameBTN.addActionListener(this);
startGameBTN.setBounds(100, 450, 150, 40);
this.add(startGameBTN);
drawBtn(firstPileBTN, 100, 150);
drawBtn(secondPileBTN, 250, 150);
drawBtn(thirdPileBTN, 400, 150);
drawBtn(fourthPileBTN, 550, 150);
toggleBoard(false);
}
public void resetGame() {
firstRow = 0;
secondRow = 0;
thirdRow = 0;
fourthRow = 0;
showDirections = true;
gameEnded = false;
chipsTaken = 0;
toggleBoard(false);
resetGameBTN.setVisible(false);
startGameBTN.setVisible(true);
}
public void startGame() {
firstRow = rand.nextInt(4) + 1;
secondRow = rand.nextInt(4) + 1;
thirdRow = rand.nextInt(4) + 1;
fourthRow = rand.nextInt(4) + 1;
chipsTaken = 0;
showDirections = false;
gameEnded = false;
toggleBoard(true);
switchPlayerBTN.setVisible(false);
resetGameBTN.setVisible(true);
startGameBTN.setVisible(false);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == resetGameBTN)
resetGame();
if (e.getSource() == startGameBTN)
startGame();
if (e.getSource() == firstPileBTN && 1 <= firstRow) {
firstRow = firstRow - 1;
chipsTaken++;
toggleBoard(false);
switchPlayerBTN.setVisible(true);
firstPileBTN.setVisible(true);
if (firstRow == 0)
firstPileBTN.setVisible(false);
}
if (e.getSource() == secondPileBTN && 1 <= secondRow) {
secondRow = secondRow - 1;
chipsTaken++;
toggleBoard(false);
secondPileBTN.setVisible(true);
if (secondRow == 0)
secondPileBTN.setVisible(false);
switchPlayerBTN.setVisible(true);
}
if (e.getSource() == thirdPileBTN && 1 <= thirdRow) {
thirdRow = thirdRow - 1;
chipsTaken++;
toggleBoard(false);
thirdPileBTN.setVisible(true);
switchPlayerBTN.setVisible(true);
}
if (e.getSource() == fourthPileBTN && 1 <= fourthRow) {
fourthRow = fourthRow - 1;
chipsTaken++;
toggleBoard(false);
fourthPileBTN.setVisible(true);
switchPlayerBTN.setVisible(true);
}
if (e.getSource() == switchPlayerBTN)
changePlayer();
if (
(firstRow == 0 && secondRow == 0 && thirdRow == 0 && fourthRow <= 1) ||
(firstRow == 0 && secondRow == 0 && thirdRow <= 1 && fourthRow == 0) ||
(firstRow == 0 && secondRow <= 1 && thirdRow == 0 && fourthRow == 0) ||
(firstRow <= 1 && secondRow == 0 && thirdRow == 0 && fourthRow == 0)
) {
gameEnded = true;
toggleBoard(false);
switchPlayerBTN.setVisible(false);
}
if (chipsTaken == 3) {
chipsTaken = 0;
toggleBoard(false);
}
repaint();
}
public void paint(Graphics g) {
if (showDirections) {
// Directions
g.setFont(title);
g.drawString("Rules/Directions", 100, 100);
g.setFont(subTitle);
g.drawString("There is four piles of chips, the goal of the game is to not be the last player to pickup the last chip.", 100, 170);
g.drawString("You can only take a maximum of 3 chips and a minimum of 1 per pile, and you can only take from one pile during your turn.", 100, 220);
} else {
if (gameEnded)
g.drawString("Player " + player + " Has beat the game!", 150, 250);
else {
drawRow(firstRow, 100, 150, g, Color.green);
drawRow(secondRow, 250, 150, g, Color.red);
drawRow(thirdRow, 400, 150, g, Color.orange);
drawRow(fourthRow, 550, 150, g, Color.pink);
}
g.setColor(Color.black);
g.setFont(subTitle);
g.drawString("Player " + player + " Is up.", 100, 100);
// Title
g.setFont(title);
g.drawString(Title, 100, 55);
// Paragraph
g.setFont(paragraph);
g.setColor(Color.black);
// Headings
g.setFont(heading);
}
}
}