This repository was archived by the owner on Mar 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphUser.java
More file actions
237 lines (205 loc) · 6.25 KB
/
GraphUser.java
File metadata and controls
237 lines (205 loc) · 6.25 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
import gameCore.API;
import gameCore.Board;
import gameCore.Cell;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GraphUser extends JFrame implements API {
// constants & attributes
private static final int CELL_SIZE = 60; // gameCore.Cell width and height, in pixel
private static Color BGCOLOR_NOT_REVEALED;
private static Color FGCOLOR_NOT_REVEALED; // flag
private static Color BGCOLOR_REVEALED;
private static Color FGCOLOR_REVEALED; // number of mines
private static Font FONT_NUMBERS;
private static int COLS;
private static int CANVAS_WIDTH; // gameCore.Game board width/height
private static int CANVAS_HEIGHT;
private static Board board;
private static CellMouseListener listener;
private static Container cp;
// buttons
private static JButton[][] btnCells;
GraphUser(Board board){
this.board = board;
this.COLS = board.getSize();
this.CANVAS_WIDTH = CELL_SIZE * COLS;
this.CANVAS_HEIGHT = CELL_SIZE * COLS;
this.BGCOLOR_NOT_REVEALED = Color.GREEN;
this.FGCOLOR_NOT_REVEALED = Color.RED;
this.BGCOLOR_REVEALED = Color.DARK_GRAY;
this.FGCOLOR_REVEALED = Color.LIGHT_GRAY;
this.FONT_NUMBERS = new Font("Monospaced", Font.BOLD, 20);
this.cp = this.getContentPane();
cp.setLayout(new GridLayout(COLS, COLS, 2, 2));
this.btnCells = new JButton[COLS][COLS];
// Allocate a common instance of listener as the MouseEvent listener
// for all the JButtons
this.listener = new CellMouseListener();
// Construct JButtons and add to the content-pane
GraphUser.construcButtons();
// Set the size of the content-pane and pack all the components
// under this container.
cp.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // handle window-close button
setTitle("Minesweeper");
setVisible(true); // show it
}
/**
* Shows the board as seen by the user.
*/
@Override
public void showBoard() {//actually updates the board window
this.updateGraphUser();
repaint();
}
/**
* Shows the debugging board.
*/
@Override
public void showTrueBoard() {//actually updates the debugging board window
}
/**
* Gets the first cell chosen by the user.
*
* @return an array of integers. the 0 index contains the x-coordinate and the 1 index contains the y-coordinate.
*/
@Override
public Cell firstCell() {
return this.selectCell();
}
/**
* Obtain a cell selected by the user in order to perform an action on it.
*
* @return an array of integers. the 0 index contains the x-coordinate and the 1 index contains the y-coordinate. If the user wants to change the action, the return is -1,-1.
*/
@Override
public Cell selectCell() {
while (!this.listener.getAuxButton()){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.listener.setAuxButton(false);
int x = this.listener.getAuxCell()[0];
int y = this.listener.getAuxCell()[1];
return this.board.getCell(x, y);
}
/**
* Performs an action in the cell
*
* @return An integer: 0 -> Unflag, 1 -> Flag, 2 -> Reveal
*/
@Override
public int action() {
Cell cell = this.board.getCell(this.listener.getAuxCell()[0], this.listener.getAuxCell()[1]);
int[] status = cell.display();
if (this.listener.isClick()) {
return status[0] == 0 ? 2 : -1;
}
if (status[0] == 0) {
return 1;
}
if (status[0] == 1) {
return 0;
}
return -1;
}
/**
* Receives the round number.
*
* @param round integer.
*/
@Override
public void receiveRound(int round) {
/*TODO*/
}
private void updateGraphUser(){
for (int i = 0; i < this.board.getSize(); i++) {
for (int j = 0; j < this.board.getSize(); j++) {
Cell cell = this.board.getCell(i, j);
JButton button = this.btnCells[i][j];
int[] display = cell.display();
if (display[0] == 0){
// hidden
button.setForeground(FGCOLOR_NOT_REVEALED);
button.setBackground(BGCOLOR_NOT_REVEALED);
button.setFont(FONT_NUMBERS);
button.setText(""); // display blank
}
if (display[0] == 1){
//flagged
button.setText("!");
}
if (display[0] == 2){
button.setForeground(FGCOLOR_REVEALED);
button.setBackground(BGCOLOR_REVEALED);
button.setText("" + display[1]);
}
if (display[0] == 3){
button.setForeground(FGCOLOR_NOT_REVEALED);
button.setBackground(BGCOLOR_REVEALED);
button.setText("X");
}
}
}
}
private static void construcButtons(){
for (int row = 0; row < COLS; row++) {
for (int col = 0; col < COLS; col++) {
btnCells[row][col] = new JButton(); // Allocate each JButton of the array
cp.add(btnCells[row][col]); // add to content-pane in GridLayout
btnCells[row][col].addMouseListener(listener);
btnCells[row][col].setEnabled(true); // enable button
btnCells[row][col].setForeground(FGCOLOR_NOT_REVEALED);
btnCells[row][col].setBackground(BGCOLOR_NOT_REVEALED);
btnCells[row][col].setFont(FONT_NUMBERS);
btnCells[row][col].setText(""); // display blank
}
}
}
private class CellMouseListener extends MouseAdapter {
private boolean auxButton;
private int auxCell[];
private boolean click;// true -> left click, false -> right click
public boolean getAuxButton(){
return auxButton;
}
public void setAuxButton(boolean b){
this.auxButton = b;
}
public int[] getAuxCell() {
return auxCell;
}
public boolean isClick() {
return click;
}
@Override
public void mouseClicked(MouseEvent e) {
this.auxCell = new int[2];
// Determine the (row, col) of the JButton that triggered the event
// Get the source object that fired the Event
JButton source = (JButton)e.getSource();
// Scan all rows and columns, and match with the source object
boolean found = false;
for (int row = 0; row < COLS && !found; ++row) {
for (int col = 0; col < COLS && !found; ++col) {
if (source == btnCells[row][col]) {
this.auxCell[0] = row;
this.auxCell[1] = col;
this.auxButton = true;
found = true; // break both inner/outer loops
}
}
}
if (e.getButton() == MouseEvent.BUTTON1) { // Left-button clicked
this.click = true;
} else if (e.getButton() == MouseEvent.BUTTON3) { // right-button clicked
this.click = false;
}
}
}
}