forked from zminic/minesweeper.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.ts
211 lines (172 loc) · 5.43 KB
/
Board.ts
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
/// <reference path="interfaces/jquery.d.ts" />
/// <reference path="interfaces/knockout.d.ts" />
/// <reference path="interfaces/knockout.es5.d.ts" />
/// <amd-dependency path="knockout-es5" />
import utils = require("utils");
import $ = require("jquery");
import ko = require("knockout");
export interface Field
{
isRevealed: boolean;
isFlagged: boolean;
isMine: boolean;
mineCount: number;
index: number;
}
export interface RevealEventArgs
{
field: Field;
mouseButton: number;
}
export class Board
{
fields: Field[];
private eventReveal = new utils.Event<RevealEventArgs>();
private eventWin = new utils.Event();
private eventGameOver = new utils.Event();
public get OnReveal(): utils.IEvent<RevealEventArgs> { return this.eventReveal; }
public get OnWin(): utils.IEvent<string> { return this.eventWin; }
public get OnGameOver(): utils.IEvent<string> { return this.eventGameOver; }
constructor(private dimension: number, private mineCount: number, private boardElement: JQuery)
{
this.fields = [];
ko.cleanNode(this.boardElement[0]);
boardElement.append($('#boardTemplate').html());
boardElement.on('contextmenu', () => false);
ko.track(this);
ko.applyBindings(this, this.boardElement[0]);
}
private getRandomNumber(max)
{
return Math.floor((Math.random() * 1000) + 1) % max;
}
draw()
{
this.drawFields();
this.plantMines();
this.calculateDistances();
}
private drawFields()
{
for (var i=0; i < this.dimension * this.dimension; i++)
{
var newField = {
isRevealed: false,
isFlagged: false,
isMine: false,
mineCount: 0,
index: i
};
ko.track(newField);
this.fields.push(newField);
}
}
private plantMines()
{
var minesPlanted: number = 0;
while (minesPlanted < this.mineCount)
{
var index = this.getRandomNumber(this.dimension * this.dimension);
if (!this.fields[index].isMine)
{
this.fields[index].isMine = true;
minesPlanted++;
}
}
}
private calculateDistances()
{
for (var i=0; i < this.dimension * this.dimension; i++)
{
var field = this.fields[i];
if (!field.isMine)
{
field.mineCount = this.getNearFields(field, (f) => f.isMine).length;
}
}
}
private getNearFields(from: Field, filter: (field: Field) => boolean = () => true): Field[]
{
var result = [],
dim = this.dimension,
len = this.fields.length,
ind = from.index,
isFirstColumn = (i) => i % dim == 0 && i != 0,
isLastColumn = (i) => (i + 1) % dim == 0 && i != len - 1;
// up
if (ind - dim >= 0)
result.push(this.fields[ind - dim]);
// down
if (ind + dim < len)
result.push(this.fields[ind + dim]);
// left
if (ind - 1 >= 0 && !isFirstColumn(ind))
result.push(this.fields[ind - 1]);
// right
if (ind + 1 < len && !isLastColumn(ind))
result.push(this.fields[ind + 1]);
// up left
if (ind - dim - 1 >= 0 && !isFirstColumn(ind - dim))
result.push(this.fields[ind - dim - 1]);
// up right
if (ind - dim + 1 >= 0 && !isLastColumn(ind - dim))
result.push(this.fields[ind - dim + 1]);
// bottom left
if (ind + dim - 1 < len && !isFirstColumn(ind + dim))
result.push(this.fields[ind + dim - 1]);
// bottom right
if (ind + dim + 1 < len && !isLastColumn(ind - dim))
result.push(this.fields[ind + dim + 1]);
return result.filter(filter);
}
private onRevealField = (field: Field, event: any) => {
this.eventReveal.trigger({
field: field,
mouseButton: event.button
});
}
revealField = (field: Field, auto: boolean = false) =>
{
// do not reveal flagged and revealed fields in auto mode
if (field.isFlagged || auto && field.isRevealed) return;
if (field.isMine)
{
this.revealBoard();
this.eventGameOver.trigger();
}
else if (field.isRevealed && !auto)
{
var flagged = this.getNearFields(field, (f) => f.isFlagged);
if (field.mineCount == flagged.length)
{
this.getNearFields(field, (f) => !f.isRevealed && !f.isFlagged).forEach((f) => this.revealField(f, true));
}
}
else
{
field.isRevealed = true;
field.isFlagged = false;
// auto reveal
if (field.mineCount == 0)
{
this.getNearFields(field).forEach((f) => this.revealField(f, true));
}
if (this.isGameOver()) this.eventWin.trigger();
}
};
private revealBoard()
{
for(var i=0; i < this.fields.length; i++)
{
this.fields[i].isRevealed = true;
}
}
private isGameOver()
{
return this.fields.filter((f) => !f.isRevealed).length == this.mineCount;
}
flag(field: Field)
{
if (!field.isRevealed) field.isFlagged = !field.isFlagged;
}
}