-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCells.cs
More file actions
executable file
·311 lines (307 loc) · 9.66 KB
/
Cells.cs
File metadata and controls
executable file
·311 lines (307 loc) · 9.66 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
#region Copyright (c) 2010, Pavel Poltavets
/*
{*********************************************************}
{ }
{ Sapper .NET application }
{ }
{ Copyright (c) 2010, Pavel Poltavets }
{ poltavets-pavel@mail.ru }
{ }
{ ALL RIGHTS RESERVED }
{ }
{*********************************************************}
*/
#endregion Copyright (c) 2010, Pavel Poltavets
using System;
using System.Collections.Generic;
using Sapper.Types;
namespace Sapper.Kernel {
#region Help
/// <summary>
/// Represents a cell in the field of mines.
/// </summary>
/// <remarks>A cell in the field of mines is specified by state and mode.</remarks>
#endregion Help
public abstract class Cell : ICell {
protected enum CellState { Normal, Label, Undefined }
[FlagsAttribute]
private enum CellModes {
None = 0,
Closed = 0x1,
HasMine = 0x2,
Selected = 0x4,
SelectedAround = 0x8,
DetonatedMine = 0x10,
Unused01 = 0x20,
Unused02 = 0x40,
HasVisited = 0x80
}
private static CellState DefaultCellState = CellState.Normal;
private int minesNumber;
private CellsCollection<ICell> aroundCells;
private CellModes mode;
private CellState state;
private void DepthFirstSearch(ICell currentCell, ref int counter) {
if(currentCell.ChangeState()) {
counter++;
}
if(currentCell.MinesNumber > 0 || currentCell.HasMine)
return;
currentCell.HasVisited = true;
foreach(ICell cell in currentCell.CellsAround)
if(!cell.HasVisited) {
DepthFirstSearch(cell, ref counter);
}
}
private bool GetMode(CellModes cellMode) {
return (mode & cellMode) != 0;
}
private void SetMode(CellModes cellMode, bool value) {
mode = value ? (mode | cellMode) : (mode & ~cellMode);
}
private CellState GetNextState() {
CellState cellState = state + 1;
if(!Enum.IsDefined(typeof(CellState), cellState))
cellState = DefaultCellState;
return cellState;
}
private bool CheckAround() {
int count = 0;
foreach(Cell cell in aroundCells) {
if(cell.IsLabelState || cell.IsMineDetonated) {
count++;
}
}
return count == MinesNumber;
}
protected Cell() {
Closed = true;
state = DefaultCellState;
aroundCells = new CellsCollection<ICell>();
}
protected abstract void Update();
#region Help
/// <summary>
/// Gets <see cref="Sapper.CellState">state</see> of the cell.
/// </summary>
#endregion
protected CellState State { get { return state; } }
protected abstract bool UseEffect3D { get; }
public void AddAroundCell(ICell cell) {
if(cell.HasMine) {
minesNumber++;
}
aroundCells.Add(cell);
}
#region Help
/// <summary>
/// Open the cell if his state is not <see cref="Sapper.CellState">Label</see>
/// </summary>
#endregion
public bool ChangeState() {
bool isChanged = CanOpened;
if(isChanged) {
state = CellState.Normal;
Closed = false;
Update();
}
return isChanged;
}
public bool MarkLabel() {
bool notLabelState = Closed && !IsLabelState;
if(notLabelState) {
state = CellState.Label;
Update();
}
return notLabelState;
}
#region Help
/// <summary>
/// Opens a <see cref="Sapper.Cell">cell</see> with empty cells and arounding cells if checked.
/// </summary>
/// <returns>
/// Returns <see cref="Sapper.CellOpenResults">struct</see> with two integers:
/// <see cref="Sapper.CellOpenResults.MineCount">count of the opened mines</see>;
/// <see cref="Sapper.CellOpenResults.OpenedCount">count of the opened cells</see>.
/// </returns>
#endregion Help
public CellOperationOpening Open(bool isWithAround) {
CellOperationOpening opening = new CellOperationOpening();
if(isWithAround) {
if(!Closed && !IsMineDetonated && CheckAround())
foreach(Cell cell in aroundCells) {
opening += cell.Open(false);
}
} else if(CanOpened) {
int counter = 0;
if(HasMine) {
opening.MineCount = 1;
IsMineDetonated = true;
}
DepthFirstSearch(this, ref counter);
opening.OpenedCount = counter;
}
return opening;
}
#region Help
/// <summary>
/// Set a next <see cref="Sapper.Cell">Cell</see> <see cref="Sapper.Cell.State">state</see>.
/// </summary>
/// <returns>
/// A integer value which indicates the changed <see cref="Sapper.CellState">Label</see> state:
/// -1 - current <see cref="Sapper.CellState">state</see> is <see cref="Sapper.CellState">Label</see>,
/// 0 - not <see cref="Sapper.CellState">Label</see> state changed,
/// +1 - previous <see cref="Sapper.CellState">state</see> is <see cref="Sapper.CellState">Label</see>.
/// </returns>
#endregion Help
public int SetNextState() {
int result = 0;
if(IsLabelState)
result = +1;
state = GetNextState();
if(IsLabelState)
result = -1;
Update();
return result;
}
public void ShowMine() {
if(Closed && ((HasMine && !IsLabelState) || (!HasMine && IsLabelState))) {
Closed = false;
Update();
}
}
public int MinesNumber { get { return minesNumber; } }
public bool CanOpened { get { return Closed && !IsLabelState; } }
public CellsCollection<ICell> CellsAround {
get { return aroundCells; }
}
public bool Closed {
get { return GetMode(CellModes.Closed); }
set { SetMode(CellModes.Closed, value); }
}
public bool HasMine {
get { return GetMode(CellModes.HasMine); }
set { SetMode(CellModes.HasMine, value); }
}
public bool HasVisited {
get { return GetMode(CellModes.HasVisited); }
set { SetMode(CellModes.HasVisited, value); }
}
public bool IsLabelState { get { return state == CellState.Label; } }
public bool IsMineDetonated {
get { return GetMode(CellModes.DetonatedMine); }
set { SetMode(CellModes.DetonatedMine, value); }
}
public bool IsSelected {
get { return GetMode(CellModes.Selected); }
set {
if(value != IsSelected && CanOpened) {
SetMode(CellModes.Selected, value);
Update();
}
}
}
public bool IsSelectedAround {
get { return GetMode(CellModes.SelectedAround); }
set {
IsSelected = value;
if(value != IsSelectedAround) {
SetMode(CellModes.SelectedAround, value);// don't need update
foreach(Cell cell in aroundCells) {
cell.IsSelected = value;
}
}
}
}
}
#region Help
/// <summary>
/// Represents a visual cell in the field of mines.
/// </summary>
#endregion Help
public abstract class VisualCell<TVertex, TBox> : Cell, IVisualCell<TVertex, TBox>
where TVertex : struct
where TBox : struct {
private const double InternalBoxCoefficient = 0.5d;
private TBox boxBounds;
private TVertex centerVertex;
private IPainter<TVertex, TBox> lastPainter;
private CellBackfillType GetBackFillType() {
CellBackfillType fillType;
if(IsMineDetonated) {
fillType = CellBackfillType.Detonated;
} else {
fillType = (Closed && !IsSelected) ? CellBackfillType.Closed : CellBackfillType.Opened;
}
return fillType;
}
protected TVertex[] vertices;
protected TVertex[] verticesEffect3D;
internal abstract void InitializeVertices(CellOperationInitialize<TVertex, TBox> cellInit);
protected abstract TBox GetInternalBox(double boxCoefficient);
protected override sealed void Update() {
if(lastPainter != null && lastPainter.IsValid) {
Draw(lastPainter, null);
}
}
protected TBox BoundsBox { get { return boxBounds; } set { boxBounds = value; } }
protected TVertex Center { get { return centerVertex; } set { centerVertex = value; } }
protected abstract int VertexCount { get; }
protected override sealed bool UseEffect3D { get { return verticesEffect3D != null; } }
protected VisualCell()
: base() {
}
public void Draw(IPainter<TVertex, TBox> painter, ISelector<TVertex, TBox> selector) {
// Set reference to external painter object (only for cell Update)
lastPainter = painter;
// Basic cell visual data
painter.FillPolygon(vertices, GetBackFillType());
if(UseEffect3D && Closed && !IsSelected) {
painter.DrawBorder3D(verticesEffect3D);
}//else
painter.DrawPolygon(vertices);
// Adding this cell to selector if needed
if(selector != null) {
selector.AddCell(this, vertices);
}
// Other common cell visual data
TVertex center = Center;
TBox internalBox = GetInternalBox(InternalBoxCoefficient);
if(!Closed) {
if(HasMine) {
if(!IsLabelState) {
painter.DrawMine(center, internalBox, true);
}
} else {
switch(State) {
case CellState.Label:
painter.DrawMine(center, internalBox, false);
break;
case CellState.Normal:
if(MinesNumber != 0) {
painter.DrawInteger(center, internalBox, MinesNumber);
}
break;
}
}
} else {// closed
switch(State) {
case CellState.Undefined:
painter.DrawUndefined(center, internalBox);
break;
case CellState.Label:
painter.DrawLabel(center, internalBox);
break;
}
}
}
public void Initialize(CellOperationInitialize<TVertex, TBox> cellInit) {
vertices = new TVertex[VertexCount];
if(cellInit.UseEffect3D) {
verticesEffect3D = new TVertex[VertexCount];
}
BoundsBox = cellInit.ExtentsBox;
InitializeVertices(cellInit);
}
}
}