-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cell.java
260 lines (232 loc) · 6.91 KB
/
Cell.java
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
/*----------------------------------------------------------------
* Author: Apurva Gandhi
* Email: ahgand22@g.holycross.edu
* Written: 12/01/2018
*
* Each Cell object manages information about and draws a
* single "cell" of the game grid.
*----------------------------------------------------------------*/
import GUI.*;
import java.awt.Color;
/**
* A <i>Cell</i> object holds all information about the state of a single cell
* of the minesweeper game board. This includes:
* - whether a mine is hidden in this cell or not
* - how many of its neighboring cells contain mines
* - whether it has been revealed yet or is still hidden
* Each Cell object knows how to draw itself in a graphical window, and it will
* draw itself in different styles depending on all the above state information.
*/
public class Cell extends Widget
{
/**
* Size of one cell when it is drawn on the screen, in pixels.
*/
public static final int SIZE = 20;
/**
* Whether a mine is hidden in this cell or not.
*/
protected boolean isMine;
/**
* Whether this cell is "revealed" or not.
*/
protected boolean isRevealed;
/**
* Count of how many neighboring cells have mines.
*/
protected int neighborMineCount;
/**
* Whether this cell has "flag" or not.
*/
protected boolean isFlag;
/**
* Count of how many cells have flags.
*/
protected int flagCount;
/**
* Constructor: Initialize a cell to be drawn at the given x, y coordinates
* on the screen. The cell will be blank. That is, it will not be a mine,
* and it will have no neighboring mines so a neighbor mine count of zero.
*/
public Cell(int x, int y)
{
super(x, y, SIZE, SIZE);
this.isMine = false;
this.isRevealed = false;
this.neighborMineCount = 0;
}
/**
* Hide a mine in this cell by changing the isMine variable to true.
*/
public void plantMine()
{
isMine = true;
}
/**
* plant a flag in this cell by changing the isFlag variable to true.
*/
public void plantFlag()
{
isFlag = true;
}
/**
* removes a flag in this cell by changing the isFlag variable to false.
*/
public void unplantFlag()
{
isFlag = false;
}
/**
* Returns true if a mine is hidden in this cell, otherwise returns false.
*/
public boolean isMine()
{
return isMine;
}
/**
* Returns true if a flag is present in this cell, otherwise returns false.
*/
public boolean isFlag()
{
return isFlag;
}
/**
* Increment the neighbor mine count variable by one.
*/
public void incrementNeighborMineCount()
{
neighborMineCount++;
}
/**
* Increment the flag count variable by one.
*/
public void incrementFlagCount()
{
flagCount++;
}
/**
* Set the neighbor mine count variable to a given value.
*/
public void setNeighborMineCount(int count)
{
neighborMineCount = count;
}
/**
* Returns the value of the neighbor mine count variable.
*/
public int getNeighborMineCount()
{
return neighborMineCount;
}
/**
* Returns the value of the flag count variable.
*/
public int getFlagCount()
{
return flagCount;
}
/**
* Change this cell so that it is "revealed" by setting isRevealed to true.
*/
public void reveal()
{
isRevealed = true;
}
/**
* Returns true if this cell is "revealed", otherwise returns false.
*/
public boolean isRevealed()
{
return isRevealed;
}
/**
* Hide a mine in this cell by changing the isMine variable to true.
*/
public void makeMine()
{
isMine = true;
}
/**
* Change this cell so that it shows the mine that is hiding in it.
*/
public void showMine()
{
if (isMine)
isRevealed = true;
}
/**
* Check whether there are neighboring mines.
*/
public boolean coastIsClear()
{
return (neighborMineCount == 0);
}
/**
* Paint this cell on the canvas. Don't call this directly, it is called by
* the GUI system automatically. This function should draw something on the
* canvas. Usually the drawing should stay within the bounds (x, y, width,
* height) which are protected member variables of GUI.Widget, which this
* class extends.
* @param canvas the canvas on which to draw.
*/
public void repaint(GUI.Canvas canvas)
{
//Draws the grid as all the cells are unrevealed
canvas.setPenColor(Color.GRAY);
canvas.raisedBevelRectangle(x, y, SIZE, SIZE, 4.0);
/**
* If cell is revelaed and there is no mine on it,
* it will print out the neighboring mine counts to the grid in various colors
*/
if (isRevealed() && !isMine())
{
canvas.setFont(15);
canvas.setPenColor(Color.WHITE);
canvas.raisedBevelRectangle(x, y, SIZE, SIZE, 2.0);
canvas.setPenRadius(5.0);
//Based on the neighboring mine count it will print the number to the grid
if(getNeighborMineCount() == 1)
{
canvas.setPenColor(Color.BLUE);
canvas.text(x+10,y+10,"" + getNeighborMineCount());
}
else if(getNeighborMineCount() == 2)
{
canvas.setPenColor(Color.GREEN);
canvas.text(x+10,y+10,"" + getNeighborMineCount());
}
else if(getNeighborMineCount() == 3)
{
canvas.setPenColor(Color.RED);
canvas.text(x+10,y+10,"" + getNeighborMineCount());
}
else if(getNeighborMineCount() == 4)
{
canvas.setPenColor(Color.MAGENTA);
canvas.text(x+10,y+10,"" + getNeighborMineCount());
}
else if (getNeighborMineCount() > 4)
{
canvas.setPenColor(Color.PINK);
canvas.text(x+10,y+10,"" + getNeighborMineCount());
}
}//end of isRevealed() && !isMine()
/**
* If cell is revelaed and there is mine on it,
* it will print out the image of the mine
*/
if(isRevealed() && isMine())
{
canvas.setPenColor(Color.WHITE);
canvas.raisedBevelRectangle(x, y, SIZE, SIZE, 4.0);
canvas.pictureCentered(x+10, y+10,"Mine2.jpg",20.0,20.0);
}
/**
* If there is a flag, it print out a picture of the flag to the screen
*/
if(isFlag())
{
canvas.pictureCentered(x+10, y+10,"indiaFlag1.jpg",20.0,20.0);
}
}//end of repaint
}//end of Cell