-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameScreen.java
More file actions
458 lines (361 loc) · 15.1 KB
/
Copy pathGameScreen.java
File metadata and controls
458 lines (361 loc) · 15.1 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.Random;
public class GameScreen implements ActionListener, MouseListener, ComponentListener, Runnable {
public JFrame frame = new JFrame();
public GridBagConstraints c = new GridBagConstraints();
public GameButton[][] buttons;
private int bWidth;
private int bLength;
private int sizeX, sizeY;
private String[] grass_colours, font_colours;
private Color original_color;
public int amtMoves;
Random rand = new Random();
public GameScreen(int sizeX, int sizeY, int bombDensity){
bWidth = 80;
bLength = 80;
this.sizeX = sizeX;
this.sizeY = sizeY;
GameButton.amtTiles = 0;
GameButton.amtBomb = 0;
GameButton.amtChecked = 0;
this.amtMoves = 0;
try{
imageMaster = ImageIO.read(new File("sprites/minesweeper_flag.png"));
} catch(Exception e){
e.printStackTrace();
}
buttons = new GameButton[sizeX][sizeY];
frame.setLayout(new GridBagLayout());
c.insets = new Insets(0,0,0,0);
c.gridx = 0;
c.gridy = 0;
c.ipadx = sizeX;
c.ipady = sizeY;
c.weightx = 1.0;
c.weighty = 1.0;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.CENTER;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Game");
frame.setSize(840,840);
frame.setMinimumSize(new Dimension(180,180));
frame.setResizable(true);
frame.setLocationRelativeTo(null);
//frame.setLayout(new GridLayout(Math.min(sizeX, sizeY), Math.min(sizeX,sizeY)));
frame.setVisible(true);
// setup buttons grid
for(int row = 0; row < sizeX; row++){
for(int collumn = 0; collumn < sizeY; collumn++){
//buttons[row][collumn] = new GameButton(String.format("%d %d", row, collumn), row, collumn, false);
buttons[row][collumn] = new GameButton(" ", row, collumn, false);
GameButton currentButton = buttons[row][collumn];
// setup arrays of colours
grass_colours = new String[]{"#AAD751","#8db342","#E5C29F","#D7B899"};
font_colours = new String[]{"#FFFFFF","#0000FF","#008000","#FF0000","#301934","#550000","#00FFFF","#A020F0","#808080"};
//currentButton.setBounds(row*bWidth,collumn*bLength,bWidth,bLength);
//currentButton.setIcon(new ImageIcon("green_button.png"));
currentButton.setFont(new Font("Arial", Font.BOLD, bWidth / 4));
currentButton.setBorder(new LineBorder(Color.BLACK, 0));
currentButton.setForeground(Color.BLACK);
currentButton.setBackground(Color.decode(grass_colours[((row + collumn) % 2)]));
original_color = currentButton.getBackground();
// limits the button size
currentButton.setPreferredSize(currentButton.getSize());
currentButton.setMaximumSize(currentButton.getPreferredSize());
currentButton.setFocusable(false);
currentButton.setIcon(null);
currentButton.addActionListener(this);
currentButton.addMouseListener(this);
currentButton.addComponentListener(this);
//currentButton.setIcon(new ImageIcon("https://images.emojiterra.com/google/noto-emoji/unicode-15/color/512px/1f6a9.png"));
frame.add(currentButton, c);
c.gridy++;
}
c.gridx++;
c.gridy = 0;
}
if(bombDensity <= sizeX * sizeY){
this.placeBombs(bombDensity);
} else{
System.out.println("Too many bombs!");
}
}
private void placeBombs(int bombDensity){
if(GameButton.amtTiles - GameButton.amtBomb >= bombDensity){
for(int i = 0; i < bombDensity; i++){
int randX = rand.nextInt(sizeX);
int randY = rand.nextInt(sizeY);
if(!buttons[randX][randY].isBomb()){
buttons[randX][randY].setBomb(true);
} else{
i--;
}
}
} else{
System.out.println("No tiles to place bombs");
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() instanceof GameButton){
int pressedX = ((GameButton) e.getSource()).getbX();
int pressedY = ((GameButton) e.getSource()).getbY();
GameButton currentButton = buttons[pressedX][pressedY];
this.amtMoves++;
//System.out.println("Clicked");
/*
System.out.println(GameButton.amtBtn);
System.out.println(GameButton.amtBomb);
*/
//System.out.println(((GameButton) e.getSource()).getbX() + " " + ((GameButton) e.getSource()).getbY() + " " + ((GameButton) e.getSource()).isBomb());
if(currentButton.isBomb()){
// if the clicked bomb is a bomb and it is the first move, delete the bomb and replace it in another place
if(this.amtMoves == 1){
this.placeBombs(1);
currentButton.setBomb(false);
} else{
System.out.println("You lose");
//MainScreen mainScreen = new MainScreen();
//frame.dispose();
}
}
// check neighbour tiles to count bombs
try{
this.checkForBombs(pressedX, pressedY, currentButton);
} catch (StackOverflowError err){
System.out.println("Stack overflow error");
}
currentButton.setChecked(true);
this.checkWinCondition();
}
}
public void checkForBombs(int pressedX, int pressedY, GameButton currentButton) throws StackOverflowError{
ArrayList<GameButton> checkedNeighbours = this.getNeighbours(pressedX,pressedY, 1);
currentButton.setBombsAround(this.checkAmtBombsAround(pressedX, pressedY, 1));
if(!currentButton.isBomb()){
currentButton.setBackground(Color.decode(grass_colours[((currentButton.getbX() + currentButton.getbY()) % 2) + 2]));
currentButton.setForeground(Color.decode(font_colours[currentButton.getBombsAround()]));
currentButton.setIcon(null);
}
for(int i = 0; i < checkedNeighbours.size(); i++){
GameButton currentNeighbour = checkedNeighbours.get(i);
// currentNeighbour is the neighbour being checked
// currentButton is the root
currentNeighbour.setBombsAround(checkAmtBombsAround(currentNeighbour.getbX(), currentNeighbour.getbY(), 1));
if(!currentNeighbour.isChecked() && !currentNeighbour.isBomb() && currentButton.getBombsAround() == 0 && !currentButton.isBomb()){
//currentButton.setBackground(Color.decode(grass_colours[((currentButton.getbX() + currentButton.getbY()) % 2) + 2]));
if(currentNeighbour.getBombsAround() == 0){
// only checks the four neighbour tiles # up left down right
if(currentNeighbour.getbX() == pressedX || currentNeighbour.getbY() == pressedY){
currentNeighbour.setChecked(true);
this.checkForBombs(currentNeighbour.getbX(), currentNeighbour.getbY(), currentNeighbour);
}
} else{
currentNeighbour.setChecked(true);
currentNeighbour.setBackground(Color.decode(grass_colours[((currentNeighbour.getbX() + currentNeighbour.getbY()) % 2) + 2]));
currentNeighbour.setForeground(Color.decode(font_colours[currentNeighbour.getBombsAround()]));
currentNeighbour.setIcon(null);
currentNeighbour.setText(String.format("%d", this.checkAmtBombsAround(checkedNeighbours.get(i).getbX(), checkedNeighbours.get(i).getbY(), 1)));
}
}
}
if(currentButton.isBomb()){
if(!currentButton.isChecked()){
Thread loseThread = new Thread(() -> {
try {
Thread.sleep(2000);
System.out.println("Thread awoken");
MainScreen mainScreen = new MainScreen();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
loseThread.start();
this.showAllBombs();
}
} else{
if(currentButton.getBombsAround() == 0){
currentButton.setText(" ");
} else{
currentButton.setText(String.format("%d", currentButton.getBombsAround()));
}
}
}
public int checkAmtBombsAround(int x, int y, int radius){
ArrayList<GameButton> checkedNeighbours = this.getNeighbours(x,y, radius);
int bombCount = 0;
for(int i = 0; i < checkedNeighbours.size(); i++){
if(checkedNeighbours.get(i).isBomb()){
bombCount++;
}
//checkedNeighbours.get(i).setText("Checked");
}
return bombCount;
}
public void showBomb(GameButton button){
int rng = rand.nextInt(9);
currentButton = button;
currentButton.setText("");
currentButton.setBackground(Color.decode(font_colours[rng]));
try{
imageMaster = ImageIO.read(new File("sprites/bomb_icon.png"));
} catch(Exception e){
e.printStackTrace();
}
currentButton.setChecked(true);
currentButton.setIcon(new ImageIcon(imageMaster.getScaledInstance(Math.min(currentButton.getSize().width,currentButton.getSize().height), Math.min(currentButton.getSize().width,currentButton.getSize().height), Image.SCALE_SMOOTH)));
}
public void showAllBombs(){
for(int i = 0; i < sizeX; i++){
for(int j = 0; j < sizeY; j++){
if(buttons[i][j].isBomb()){
this.showBomb(buttons[i][j]);
}
}
}
}
public ArrayList<GameButton> getNeighbours(int x, int y, int radius){
//int count = 0;
ArrayList<GameButton> neighbours = new ArrayList<>();
int maxCheckX, maxCheckY;
if(x+radius > sizeX-1){
maxCheckX = sizeX-1;
} else{
maxCheckX = x+radius;
}
if(y+radius > sizeY-1){
maxCheckY = sizeY-1;
} else{
maxCheckY = y+radius;
}
for(int checkX = x-radius; checkX <= maxCheckX; checkX++){
for(int checkY = y-radius; checkY <= maxCheckY; checkY++){
//count++;
if(checkX < 0){
checkX = 0;
}
if(checkY < 0){
checkY = 0;
}
if(checkX > sizeX-1){
checkX = sizeX-1;
}
if(checkY > sizeY-1){
checkY = sizeY-1;
}
if(checkX != x || checkY != y){
neighbours.add(buttons[checkX][checkY]);
}
}
}
//System.out.println(count);
return neighbours;
}
public void checkWinCondition(){
if(GameButton.amtChecked >= GameButton.amtTiles - GameButton.amtBomb){
System.out.println("Ganhou");
MainScreen mainScreen = new MainScreen();
frame.dispose();
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
if(e.getSource() instanceof GameButton){
/*
GameButton currentButton = (GameButton) e.getSource();
currentButton.setBackground(currentButton.getBackground().brighter());
*/
}
}
@Override
public void mouseExited(MouseEvent e) {
if(e.getSource() instanceof GameButton){
//GameButton currentButton = (GameButton) e.getSource();
//currentButton.setBackground(original_color);
}
}
private BufferedImage imageMaster;
private Image imageScaled;
@Override
public void mouseClicked(MouseEvent e) {
if(e.getSource() instanceof GameButton && e.getButton() == MouseEvent.BUTTON3){
int pressedX = ((GameButton) e.getSource()).getbX();
int pressedY = ((GameButton) e.getSource()).getbY();
GameButton currentButton = buttons[pressedX][pressedY];
if(!currentButton.isChecked()){
if(currentButton.isFlagged()){
currentButton.setFlagged(false);
currentButton.setText(" ");
currentButton.setIcon(null);
} else{
currentButton.setFlagged(true);
currentButton.setText("");
currentButton.setIcon(new ImageIcon(imageScaled));
}
}
}
}
private GameButton currentButton;
private Dimension size;
private Insets insets;
@Override
public void componentResized(ComponentEvent e) {
//System.out.println("size");
currentButton = (GameButton) e.getComponent();
size = currentButton.getSize();
insets = currentButton.getInsets();
int dimension = Math.min(size.width - insets.left - insets.right, size.height - insets.top - insets.bottom);
currentButton.setFont(new Font("Arial", Font.BOLD, size.height / 2));
//size.width -= insets.left + insets.right;
//size.height -= insets.top + insets.bottom;
size.width = dimension;
size.height = dimension;
/*
if(size.width > size.height){
size.width = -1;
} else{
size.height = -1;
}
*/
if(imageMaster != null){
imageScaled = imageMaster.getScaledInstance(size.width, size.height, Image.SCALE_SMOOTH);
}
if(currentButton.isFlagged() && !currentButton.isChecked() && currentButton.getIcon().getIconHeight() != imageScaled.getHeight(null) &&
currentButton.getIcon().getIconWidth() != imageScaled.getWidth(null)){
currentButton.setIcon(new ImageIcon(imageScaled));
}
if(currentButton.isChecked() && currentButton.isBomb()){
currentButton.setIcon(new ImageIcon(imageScaled));
}
}
public void dispose(){
frame.dispose();
}
@Override
public void componentMoved(ComponentEvent e) {
}
@Override
public void componentShown(ComponentEvent e) {
}
@Override
public void componentHidden(ComponentEvent e) {
}
@Override
public void run() {
System.out.println("Runnable started");
}
}