-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmines.js
216 lines (176 loc) · 4.62 KB
/
mines.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
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
var cl = function() {
if (console) {
for (var i in arguments) {
console.log(arguments[i]);
}
}
}
var Mines = function () {
var field = [],
div = null,
bombs = [],
bombsNeighbors = [],
fieldX = 0,
fieldY = 0,
symbol = { bomb: '*' };
get = function (param) {
try {
var value = eval(param);
return eval(param);
} catch (e) {
return false;
}
},
write = function (id, show) {
div = window.document.getElementById(id);
div.innerHTML = "";
var show = show || false;
for (var i = 0; i < field.length; i++) {
var line = field[i];
for (var j = 0; j < line.length; j++) {
var content = div.innerHTML;
var coordinates = {x:i, y:j}
var childDiv = writeField(line[j], show, coordinates);
div.appendChild(childDiv);
if (j == line.length - 1) {
childDiv.className += ' last-col';
}
//div.innerHTML = content;
if (i == field.length - 1) {
childDiv.className += ' last-row';
}
}
div.appendChild(createBreakSpan());
// var content = div.innerHTML;
//div.appendChild('<span class="break"><!-- --></span>');
//div.innerHTML = content;
}
return this;
};
createBreakSpan = function() {
var elem = document.createElement('span');
elem.className = 'break';
return elem;
};
getNeighbor = function (posX, posY) {
isValidXAxis = function () {
return true;
},
isValidYAxis = function (posY) {
return true;
},
isBomb = function () {
return (field[posX][posY].value == symbol.bomb);
}
return this;
};
setNeighbors = function (x, y) {
var posX, posY, neighbor = null;
for (var i = -1; i <= 1; i++) {
posX = x + i;
for (var j = -1; j <= 1; j++) {
posY = y + j;
neighbor = getNeighbor(posX, posY);
if((posX < fieldX && posX >= 0) && (posY < fieldY && posY >= 0) && !neighbor.isBomb()) {
field[posX][posY].value++
}
}
}
return this;
};
checkNeighborsByValue = function(coordinates, value){
var neighbors = [];
//field[coordinates.x][coordinates.y].elem.innerHTML = value;
for (var i = -1; i <= 1; i++) {
posX = coordinates.x + i;
for (var j = -1; j <= 1; j++) {
posY = coordinates.y + j;
if((posX < fieldX && posX >= 0) && (posY < fieldY && posY >= 0) && field[posX][posY].value != '*' && field[posX][posY].clicked == false) {
field[posX][posY].elem.innerHTML = field[posX][posY].value;
field[posX][posY].clicked = true;
field[posX][posY].elem.className += ' field-clicked';
if (field[posX][posY].value == 0) {
var neighborCoordinates = {x:posX, y:posY}
checkNeighborsByValue(neighborCoordinates, value);
}
}
}
}
return this;
};
writeField = function (field, show, coordinates) {
var elem = document.createElement('div');
elem.className = 'field-area';
elem.onclick = function(){
field.clicked = true;
checkField(field.value, elem, coordinates);
this.className += ' field-clicked';
}
field.elem = elem;
if (show) elem.innerHTML = field.value;
return elem ;
//elem.class = 'field-area';
//return elem.html;
};
checkField = function(field, elem, coordinates) {
switch (field) {
case '*' :
write('minefield', true);
alert('BOOM');
break;
case 0 :
elem.innerHTML = '0';
checkZero(coordinates);
break;
default :
elem.className += ' field-clicked';
elem.innerHTML = field;
break;
}
};
checkZero = function (coordinates) {
checkNeighborsByValue(coordinates, '0');
};
createField = function (m, n) {
cl('b', fieldX, fieldY);
fieldX = m;
fieldY = n;
cl('a', fieldX, fieldY);
for (var i = 0; i < m; i++) {
field.push([]);
bombsNeighbors.push([]);
for (var j = 0; j < n; j++) {
var quadrant = {value:0, clicked: false}
field[i].push(quadrant);
bombsNeighbors[i].push(quadrant);
}
}
cl('bombsNeighbors', bombsNeighbors);
cl('createField', field);
return this;
};
plantBombs = function (n) {
cl('plantBombs', field, n);
var i = 0;
while (i < n) {
var x = Math.round(Math.random()*(field.length - 1));
var y = Math.round(Math.random()*(field.length - 1));
if(field[x][y].value != '*') {
i++;
field[x][y].value = '*';
setNeighbors(x, y);
} else {
cl('duplicate');
}
}
//cl(cl(field);
return this;
};
return {
createField : createField,
write : write,
plantBombs : plantBombs,
field : field,
get : get
};
};