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 pathCell.java
More file actions
222 lines (200 loc) · 5.14 KB
/
Copy pathCell.java
File metadata and controls
222 lines (200 loc) · 5.14 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
package gameCore;
import java.util.ArrayList;
public class Cell {
// Attributes
private boolean mine;
private int status; // 0 -> hidden 1-> flagged 2 -> shown
private int surroundingMines;
private int positionX;
private int positionY;
private ArrayList<Cell> neighbours;
// Constructor
/**
* Constructs a new gameCore.Cell.
* @param positionX int, the position of the cell in the x-axis. Should be higher than or equal to 0 and lower than the size of the {@link Board}.
* @param positionY int, the position of the cell in the y-axis. Should be higher than or equal to 0 and lower than the size of the {@link Board}.
*/
Cell (int positionX, int positionY) {
this.mine = false;
this.positionX = positionX;
this.positionY = positionY;
this.status = 0;
this.neighbours = new ArrayList<>();
}
// Methods
/**
* Checks if the cell contains a mine.
* @return True if and only if the cell contains a mine.
*/
boolean isMine() {
return mine;
}
/**
* Sets the value of mine
* @param mine
*/
void setMine(boolean mine) {
this.mine = mine;
}
/**
* Indicates how many of the surrounding cells contain a mine.
* @return an integer amount of mines that are contiguous to the cell, including diagonals.
*/
int getSurroundingMines() {
return surroundingMines;
}
/**
* Gets the row of the cell within the board.
* @return a positive integer. The first row produces 0, the second 1, and so on.
*/
public int getPositionX() {
return positionX;
}
/**
* Gets the column of the cell within the board.
* @return a positive integer. The first column produces 0, the second 1, and so on.
*/
public int getPositionY() {
return positionY;
}
/**
* Shows the status of the cell.
* 0 -> hidden 1-> flagged 2 -> shown
* @return 0 if hidden, 1 if flagged, and 2 if revealed.
*/
int getStatus() {
return status;
}
/**
* Shows the information about the cell available to the user.
* @return an array of two integers. The first first one describes the cell's status,
* except if a revealed cell is also a mine. In that case, it is 3. The second number is -1,
* except if the cell is a revealed non-mine cell. Then it is the number of surrounding cells.
*/
public int[] display(){
int[] answer = new int[2];
answer[1] = -1;
if (this.status == 0){
answer[0] = 0;//hidden
}
if (this.status == 1){
answer[0] = 1;//flagged
}
if (this.status == 2){
if (this.mine){
answer[0] = 3;//revealed mine
}
else {
answer[0] = 2;
answer[1] = this.surroundingMines;//revealed cell
}
}
return answer;
}
public int[] trueDisplay(){
int[] answer = new int[2];
answer[1] = this.surroundingMines;
if (this.mine){
answer[0] = 4;//mine
return answer;
}
return answer;
}
/**
* Reveals the cell, changing the status to 2. Only does this if the change is valid.
* @return true if and only if the change of status was successful.
*/
boolean reveal() {
boolean answer = this.isChangeValid(2);
if (answer) {
this.status = 2;
if (this.surroundingMines == 0){
this.autoReveal();
}
}
return answer;
}
/**
* Adds one cell to the list of this cell's neighbours.
* @param cell to be added.
*/
void addNeighbour(Cell cell){
this.neighbours.add(cell);
}
/**
* Flags the cell, changing the status to 1. Only does this if the change is valid.
* @return true if and only if the change of status was successful.
*/
boolean flag() {
boolean answer = this.isChangeValid(1);
if (answer) {
this.status = 1;
}
return answer;
}
/**
* Unflags the cell, changing the status to 0. Only does this if the change is valid.
* @return true if and only if the change of status was successful.
*/
boolean unflag() {
boolean answer = this.isChangeValid(0);
if (answer) {
this.status = 0;
}
return answer;
}
/**
* Cheks if the intended change is valid for this cell.
* @param i an integer that represents the action. 0 -> Unflag, 1 -> Flag, 2 -> Reveal.
* @return true if and only if the change is valid.
*/
boolean isChangeValid(int i) {
boolean answer = false;
switch (this.status) {
case 0:
if ((i == 1) || (i == 2)) {// a blank cell can be flagged or revealed
answer = true;
}
break;
case 1:
if (i == 0) {// a flagged cell can only be unflagged
answer = true;
}
break;
default:// a revealed cell cannot be interacted with
break;
}
return answer;
}
/**
* Gets the cell's neighbours.
* @return an ArrayList containing the cell's neighbours.
*/
public ArrayList<Cell> getNeighbours(){
return this.neighbours;
}
/**
* Changes the cell's surrounding mines to match reality.
*/
void updateSurroundingMines(){
int total = 0;
for (int i = 0; i < this.getNeighbours().size(); i++) {
Cell neighbour = this.getNeighbours().get(i);
if (neighbour.isMine()){
total++;
}
}
this.surroundingMines = total;
}
/**
* Recursively auto reveals the neighbours of a cell with no surrounding mines.
*/
private void autoReveal(){
for (int i = 0; i < this.neighbours.size(); i++) {
Cell neighbour = this.neighbours.get(i);
if (neighbour.getStatus() == 0){
neighbour.reveal();
}
}
}
}