forked from alibaba/weex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminesweeper.we
215 lines (209 loc) · 5.98 KB
/
minesweeper.we
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
<template>
<container>
<text class="btn">{{board}}</text>
<container repeat="{{row}}" style="flex-direction: row; flex: 1;">
<container repeat="{{col}}" style="flex: 1;">
<text tid="{{tid}}" onclick="onclick" onlongpress="onlongpress" class="{{state}} tile" around="{{around}}">{{text}}</text>
</container>
</container>
<text onclick="restart" class="btn">START</text>
</container>
</template>
<script>
module.exports = {
data: {
size: 9,
max: 10,
board: 0,
row: [],
vector: [[-1, 0], [-1, -1], [0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1]],
strings: {
mine: "❎",
flag: "✅",
win: "YOU WIN!",
lose: "YOU LOSE~"
},
finished: false
},
methods: {
map: function(x, y, callback) { // visit tiles around (x, y)
for (var i = 0; i < 8; ++i) {
var mx = x + this.vector[i][0];
var my = y + this.vector[i][1];
if (mx >= 0 && my >= 0 && mx < this.size && my < this.size) {
callback(this.row[mx].col[my]);
}
}
},
dfs: function(tile) { // dfs a tile
var pos = this.position(tile.tid);
var context = this;
tile.state = "open";
this.map(pos["x"], pos["y"], function(node) {
if (node.around == 0 && node.state == "normal") { // dfs
context.dfs(node); // dfs recursively
} else {
context.display(node); // display tile
}
});
},
random: function(min, max) { // generate random number between [min, max)
return parseInt(Math.random() * (max - min) + min);
},
plant: function() { // arrange mines
var count = 0;
while (count < this.max) {
var x = this.random(0, this.size);
var y = this.random(0, this.size);
var tile = this.row[x].col[y];
if (tile.value == 0) {
++count;
tile.value = 1;
}
}
},
calculate: function() { // calculate values around tiles
for (var i = 0; i < this.size; ++i) {
for (var j = 0; j < this.size; ++j) {
var around = 0;
this.map(i, j, function(tile) {
around += tile.value;
});
this.row[i].col[j].around = around;
}
}
},
restart: function(e) { // restart game
var row = [];
var count = 0;
this.board = this.max; // display remain mines
this.finished = false;
for (var i = 0; i < this.size; ++i) { // init data-binding
var col = { "col": [] };
for (var j = 0; j < this.size; ++j) {
var tid = i * this.size + j;
col["col"][j] = {
tid: "" + tid,
state: "normal",
value: 0,
text: "",
around: 0
};
}
row[i] = col;
}
this.row = row; // will cause view tree rendering
this.plant(); // arrange mines
this.calculate(); // calculate around values
},
unfinished: function() { // check game status
var finished = this.finished;
if (this.finished) { // restart if finished
this.restart();
}
return !finished;
},
position: function(tid) { // return (x, y) with tile id
var row = parseInt(tid / this.size);
var col = tid % this.size;
return { x: row, y: col };
},
display: function(tile) {
tile.state = "open";
tile.text = (tile.around == 0) ? "" : tile.around;
},
tile: function(event) { // return tile object with click event
var tid = event.target.attr["tid"];
var pos = this.position(tid);
return this.row[pos["x"]].col[pos["y"]];
},
onclick: function(event) { // onclick tile
if (this.unfinished()) {
var tile = this.tile(event);
if (tile.state == "normal") {
if (tile.value == 1) { // lose game
this.onfail();
} else { // open it
this.display(tile);
if (tile.around == 0) {
this.dfs(tile); // start dfs a tile
}
this.judge(); // game judgement
}
}
}
},
onlongpress: function(event) { // onlongpress tile
if (this.unfinished()) {
var tile = this.tile(event);
tile.state = tile.state == "flag" ? "normal" : "flag";
if (tile.state == "flag") {
--this.board;
tile.text = this.strings.flag; // flag
} else {
++this.board;
tile.text = "";
}
this.judge();
}
},
foreach: function(callback) { // enumerate all tiles
for (var i = 0; i < this.size; ++i) {
for (var j = 0; j < this.size; ++j) {
callback(this.row[i].col[j]);
}
}
},
judge: function() {
var count = 0;
this.foreach(function(tile) {
if (tile.state == "open" || tile.state == "flag") {
++count;
}
});
if (count == this.size * this.size) { // win
this.finished = true;
this.board = this.strings.win;
}
},
onfail: function() { // fail
this.board = this.strings.lose;
this.finished = true;
var mine = this.strings.mine;
this.foreach(function(tile) {
if (tile.value == 1) {
tile.text = mine;
}
});
}
}
}
</script>
<style>
.btn {
margin: 2;
background-color: #e74c3c;
color: #ffffff;
text-align: center;
flex: 1;
font-size: 66;
height: 80;
}
.normal {
background-color: #95a5a6;
}
.open {
background-color: #34495e;
color: #ffffff;
}
.flag {
background-color: #95a5a6;
}
.tile {
margin: 2;
font-size: 66;
height: 80;
padding-top: 0;
text-align: center;
}
</style>