-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsgame.js
180 lines (170 loc) · 5.77 KB
/
msgame.js
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
"use strict";
//Pavols MS game engine. Needs
let MSGame = (function(){
// private constants
const STATE_HIDDEN = "hidden";
const STATE_SHOWN = "shown";
const STATE_MARKED = "marked";
function array2d( nrows, ncols, val) {
const res = [];
for( let row = 0 ; row < nrows ; row ++) {
res[row] = [];
for( let col = 0 ; col < ncols ; col ++)
res[row][col] = val(row,col);
}
return res;
}
// returns random integer in range [min, max]
function rndInt(min, max) {
[min,max] = [Math.ceil(min), Math.floor(max)]
return min + Math.floor(Math.random() * (max - min + 1));
}
class _MSGame {
constructor() {
this.init(8,10,10); // easy
}
validCoord(row, col) {
return row >= 0 && row < this.nrows && col >= 0 && col < this.ncols;
}
init(nrows, ncols, nmines) {
this.nrows = nrows;
this.ncols = ncols;
this.nmines = nmines;
this.nmarked = 0;
this.nuncovered = 0;
this.exploded = false;
// create an array
this.arr = array2d(
nrows, ncols,
() => ({mine: false, state: STATE_HIDDEN, count: 0}));
}
count(row,col) {
const c = (r,c) =>
(this.validCoord(r,c) && this.arr[r][c].mine ? 1 : 0);
let res = 0;
for( let dr = -1 ; dr <= 1 ; dr ++ )
for( let dc = -1 ; dc <= 1 ; dc ++ )
res += c(row+dr,col+dc);
return res;
}
sprinkleMines(row, col) {
// prepare a list of allowed coordinates for mine placement
let allowed = [];
for(let r = 0 ; r < this.nrows ; r ++ ) {
for( let c = 0 ; c < this.ncols ; c ++ ) {
if(Math.abs(row-r) > 2 || Math.abs(col-c) > 2)
allowed.push([r,c]);
}
}
this.nmines = Math.min(this.nmines, allowed.length);
for( let i = 0 ; i < this.nmines ; i ++ ) {
let j = rndInt(i, allowed.length-1);
[allowed[i], allowed[j]] = [allowed[j], allowed[i]];
let [r,c] = allowed[i];
this.arr[r][c].mine = true;
}
// erase any marks (in case user placed them) and update counts
for(let r = 0 ; r < this.nrows ; r ++ ) {
for( let c = 0 ; c < this.ncols ; c ++ ) {
if(this.arr[r][c].state == STATE_MARKED)
this.arr[r][c].state = STATE_HIDDEN;
this.arr[r][c].count = this.count(r,c);
}
}
let mines = []; let counts = [];
for(let row = 0 ; row < this.nrows ; row ++ ) {
let s = "";
for( let col = 0 ; col < this.ncols ; col ++ ) {
s += this.arr[row][col].mine ? "B" : ".";
}
s += " | ";
for( let col = 0 ; col < this.ncols ; col ++ ) {
s += this.arr[row][col].count.toString();
}
mines[row] = s;
}
console.log("Mines and counts after sprinkling:");
console.log(mines.join("\n"), "\n");
}
// puts a flag on a cell
// this is the 'right-click' or 'long-tap' functionality
uncover(row, col) {
console.log("uncover", row, col);
// if coordinates invalid, refuse this request
if( ! this.validCoord(row,col)) return false;
// if this is the very first move, populate the mines, but make
// sure the current cell does not get a mine
if( this.nuncovered === 0)
this.sprinkleMines(row, col);
// if cell is not hidden, ignore this move
if( this.arr[row][col].state !== STATE_HIDDEN) return false;
// floodfill all 0-count cells
const ff = (r,c) => {
if( ! this.validCoord(r,c)) return;
if( this.arr[r][c].state !== STATE_HIDDEN) return;
this.arr[r][c].state = STATE_SHOWN;
this.nuncovered ++;
if( this.arr[r][c].count !== 0) return;
ff(r-1,c-1);ff(r-1,c);ff(r-1,c+1);
ff(r ,c-1); ;ff(r ,c+1);
ff(r+1,c-1);ff(r+1,c);ff(r+1,c+1);
};
ff(row,col);
// have we hit a mine?
if( this.arr[row][col].mine) {
this.exploded = true;
}
return true;
}
// uncovers a cell at a given coordinate
// this is the 'left-click' functionality
mark(row, col) {
console.log("mark", row, col);
// if coordinates invalid, refuse this request
if( ! this.validCoord(row,col)) return false;
// if cell already uncovered, refuse this
console.log("marking previous state=", this.arr[row][col].state);
if( this.arr[row][col].state === STATE_SHOWN) return false;
// accept the move and flip the marked status
this.nmarked += this.arr[row][col].state == STATE_MARKED ? -1 : 1;
this.arr[row][col].state = this.arr[row][col].state == STATE_MARKED ?
STATE_HIDDEN : STATE_MARKED;
return true;
}
// returns array of strings representing the rendering of the board
// "H" = hidden cell - no bomb
// "F" = hidden cell with a mark / flag
// "M" = uncovered mine (game should be over now)
// '0'..'9' = number of mines in adjacent cells
getRendering() {
const res = [];
for( let row = 0 ; row < this.nrows ; row ++) {
let s = "";
for( let col = 0 ; col < this.ncols ; col ++ ) {
let a = this.arr[row][col];
if( this.exploded && a.mine) s += "M";
else if( a.state === STATE_HIDDEN) s += "H";
else if( a.state === STATE_MARKED) s += "F";
else if( a.mine) s += "M";
else s += a.count.toString();
}
res[row] = s;
}
return res;
}
getStatus() {
let done = this.exploded ||
this.nuncovered === this.nrows * this.ncols - this.nmines;
return {
done: done,
exploded: this.exploded,
nrows: this.nrows,
ncols: this.ncols,
nmarked: this.nmarked,
nuncovered: this.nuncovered,
nmines: this.nmines
}
}
}
return _MSGame;
})();