-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
186 lines (170 loc) · 4.1 KB
/
Copy pathBoard.java
File metadata and controls
186 lines (170 loc) · 4.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
import javax.swing.*;
import java.awt.Graphics;
import java.awt.Dimension;
/**
* @author ekaya
*/
public class Board extends JPanel
{
private Minefield minefield;
private JLabel status;
private int height;
private int width;
private int mines;
private Menu.CompletionListener completionListener;
/**
* Constructor that creates a minefield, initializes values, activates mouse, and sets up size of the board
*/
public Board(int height, int width, int mines, JLabel statusbar, Menu.CompletionListener completionListener)
{
this.height = height;
this.width = width;
this.mines = mines;
this.minefield = new Minefield(height, width, mines);
status = statusbar;
setPreferredSize(new Dimension(Configuration.BOARD_WIDTH, Configuration.BOARD_HEIGHT));
addMouseListener(new MouseReader(this));
this.completionListener = completionListener;
}
/**
* @param Graphics object
*/
@Override
public void paintComponent(Graphics g)
{
minefield.draw(g);
}
/**
* this method adjusts the statuses of cells based on user interaction through a mouse
*/
public void mouseClickOnLocation(int x, int y, String button)
{
if(isGameOver()) {
String result = "YOU WIN!";
setStatusbar(result);
repaint();
completionListener.handle(result);
return;
}
if(minefield.gameLost()) {
return;
}
Cell clickedCell = minefield.getCellByScreenCoordinates(x,y);
if (clickedCell.isMine()) {
if (button == "right") {
if(clickedCell.getStatus().equals("cvrd")) {
clickedCell.setStatus("mrkd");
this.removeMine();
}
else if(clickedCell.getStatus().equals("mrkd")){
clickedCell.setStatus("cvrd");
this.addMine();
}
else {
return;
}
}
else if (button == "left") {
if(clickedCell.getStatus().equals("mrkd")) {
setStatusbar("Invalid Action, Unflag the Cell First");
repaint();
return;
}
clickedCell.setStatus("opnd");
minefield.openAllMines();
minefield.gameLost(true);
String result = "YOU LOSE!";
setStatusbar(result);
repaint();
completionListener.handle(result);
return;
}
}
if (!clickedCell.isMine()) {
if (button == "right") {
if (clickedCell.getStatus().equals("opnd")) {
setStatusbar("Invalid action");
repaint();
return;
}
else if (clickedCell.getStatus().equals("mrkd")) {
this.addMine();
clickedCell.setStatus("cvrd");
}
else {
clickedCell.setStatus("mrkd");
this.removeMine();
}
}
else if (button == "left") {
if(clickedCell.getStatus().equals("mrkd")) {
setStatusbar("Invalid Action, Unflag the Cell First");
repaint();
return;
}
if(!clickedCell.getStatus().equals("opnd")) {
minefield.openCells((clickedCell));
}
}
}
if(isGameOver()) {
String result = "YOU WIN!";
setStatusbar(result);
completionListener.handle(result);
return;
}
repaint();
}
public void setStatusbar(String text) {
//A setter method for the text that is displayed in the statusbar. You should use the setText
//method of the JLabel class.
status.setText(text);
}
public String getStatusbar() {
//A getter method that returns the text of the statusbar. You should use the getText method of
//the JLabel class.
return status.getText();
}
public Minefield getMinefield() {
return this.minefield;
}
/**
* @return This method returns true if game is over
*/
public boolean isGameOver() {
if(minefield.getInfoCellCount() == 0) {
return true;
}
else return false;
}
/**
* This method adjusts the statusbar as the user flags for mines
*/
public boolean removeMine() {
boolean value = true;
if (mines > 0) {
mines -=1;
setStatusbar(mines + "Mines remaining");
}
else {
value = false;
setStatusbar("Invalid action");
}
return value;
}
/**
* This method adjusts the statusbar as the user unflags for mines
*/
public boolean addMine() {
boolean value = true;
if (mines < Configuration.MINES) {
mines +=1;
setStatusbar(mines + "Mines remaining");
}
else {
value = false;
setStatusbar("Invalid action");
}
return value;
}
}