-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabyrinth.js
executable file
·374 lines (341 loc) · 8.49 KB
/
labyrinth.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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/**
* Generate labyrinth on based method backtracking.
* @class Labyrinth
* @version 1.0.0
* @author Xortrike
* @license GNU General Public License v3.0
* @link https://github.com/xortrike/library-labyrinth.git
*/
class Labyrinth
{
/**
* Set default options
* @constructor
* @param {Object} options options
*/
constructor(options)
{
/** @private */
this.matrix = null;
/** @private */
this.pointX = 1;
/** @private */
this.pointY = 1;
/** @private */
this.pointWidth = 11;
/** @private */
this.pointHeight = 11;
/** @private */
this.pointOpen = 3;
/** @private */
this.pointWorm = 2;
/** @private */
this.pointBorder = 0;
/** @private */
this.pointInside = 1;
// Set options
if (this.__checkTypeOf(options, 'Object')) {
this.setOptions(options);
}
}
/**
* Set default setting options
* @public
* @param {Object} options Set setting options
*/
setOptions(options)
{
// Set labyrinth size
this.setSize(options);
// Set labyrinth points
this.setPoints(options);
// Set starting point
this.setStarting(options);
}
/**
* Size labyrinth there must be an odd number.
* Width more or equal 7
* Height more or equal 7
* @public
* @param {Object} options setting options
*/
setSize(options)
{
// Check options
if (this.__checkTypeOf(options, 'Object') === false) {
return false;
}
// Width
if ( options.hasOwnProperty('width') ) {
let width = parseInt(options.width);
if ( width >= 7 ) {
this.pointWidth = width;
} else {
console.warn('Invalid value: ' + options.width);
}
}
// Height
if ( options.hasOwnProperty('height') ) {
let height = parseInt(options.height);
if ( height >= 7 ) {
this.pointHeight = height;
} else {
console.warn('Invalid value: ' + options.height);
}
}
if ( this.pointWidth % 2 == 0 || this.pointHeight % 2 == 0 ) {
throw 'Size "width" or "height" not odd number.';
}
}
/**
* Set matrix points
* @public
* @param {Object} options setting options
*/
setPoints(options)
{
// Check options
if (this.__checkTypeOf(options, 'Object') === false) {
return false;
}
// Point border
if ( options.hasOwnProperty('border') ) {
// Border point
let border = parseInt(options.border);
if ( Number.isInteger(border) ) {
this.pointBorder = border;
} else {
console.warn('Invalid value: ' + options.border);
}
}
// Point inside
if ( options.hasOwnProperty('inside') ) {
// Inside point
let inside = parseInt(options.inside);
if ( Number.isInteger(inside) ) {
this.pointInside = inside;
} else {
console.warn('Invalid value: ' + options.inside);
}
} else {
// Random
this.pointInside = 0;
while ( this.pointInside == this.pointBorder ) {
this.pointInside++;
}
}
if ( this.pointBorder == this.pointInside ) {
throw 'Point "border" equals "inside".'
}
// Set other points
let points = new Array();
points.push(this.pointBorder);
points.push(this.pointInside);
// Point worm
this.pointWorm = 1;
while ( points.indexOf(this.pointWorm) >= 0 ) {
this.pointWorm++;
}
points.push(this.pointWorm);
// Point open
this.pointOpen = 1;
while ( points.indexOf(this.pointOpen) >= 0 ) {
this.pointOpen++;
}
}
/**
* Set point starting
* @public
* @param {Object} options setting options
*/
setStarting(options)
{
// Check options
if (this.__checkTypeOf(options, 'Object') === false) {
return false;
}
if (options.hasOwnProperty('starting') === false) {
return false;
}
let starting = parseInt(options.starting).toLocaleString();
if (starting === 'lt') {
// Left-top
this.pointX = 1;
this.pointY = 1;
} else if (starting === 'rt') {
// Right-top
this.pointX = this.pointWidth - 1;
this.pointY = 1;
} else if (starting === 'lb') {
// Left-bottom
this.pointX = 1;
this.pointY = this.pointHeight - 1;
} else if (starting === 'rb') {
// Right-bottom
this.pointX = this.pointWidth - 1;
this.pointY = this.pointHeight - 1;
} else if (starting === 'cc') {
// Center
this.pointX = (this.pointWidth - 1) / 2;
if (this.pointX % 2 == 0) {
this.pointX--;
}
this.pointY = (this.pointHeight - 1) / 2;
if (this.pointY % 2 == 0) {
this.pointY--;
}
}
}
/**
* Start generate labyrinth
* @public
* @return {Array} labyrinth matrix
*/
generate(width, height)
{
if (this.__checkTypeOf(width, "Number") && this.__checkTypeOf(height, "Number")) {
this.setSize({
width: width,
height: height
});
}
// Stage #1
this.__dataPreparation();
// Stage #2
this.__dataProcessing();
return this.matrix;
}
/**
* Preparing data to create a matrix
* @private
*/
__dataPreparation()
{
// Create new empty matrix
this.matrix = new Array(this.pointHeight);
// Set default data to matrix
for (let y = 0; y < this.pointHeight; ++y) {
this.matrix[y] = new Array(this.pointWidth);
for (let x = 0; x < this.pointWidth; ++x) {
if (x % 2 == 1 && y % 2 == 1) {
this.matrix[y][x] = this.pointOpen;
} else {
this.matrix[y][x] = this.pointBorder;
}
}
}
}
/**
* Generation process labyrinth
* @private
*/
__dataProcessing()
{
let nextDirection;
do {
// Forward movement by set direction
nextDirection = this.__getNextDirection(this.pointOpen, 2);
if (nextDirection > 0) {
this.__makeNextStep(nextDirection, this.pointWorm);
} else {
// Backward movement by set direction
nextDirection = this.__getNextDirection(this.pointWorm, 1);
if (nextDirection > 0) {
this.__makeNextStep(nextDirection, this.pointInside);
}
}
} while (nextDirection > 0);
// Set finish point
this.matrix[this.pointY][this.pointX] = this.pointInside;
}
/**
* Check variable type
* @private
* @param {Mixed} variable - Any variable
* @param {String} type - Check type
* @return {Boolean} Check result
*/
__checkTypeOf(variable, type = "")
{
let typeType = new Object().toString.call(type).slice(8, -1);
let variableType = new Object().toString.call(variable).slice(8, -1);
if (typeType === "String" && type.length > 0) {
return (variableType.toLowerCase() === type.toLowerCase());
}
return variableType;
}
/**
* Return random number in set range number
* @private
* @param {Number} a Input number
* @param {Number} b Input number
*/
__getRandomNumber(a = 0, b = 1)
{
return Math.floor( Math.random() * ( b - a ) ) + a;
}
/**
* Return direction next step
* @private
* @param {Number} point
* @param {Number} step
* @return {Number}
*/
__getNextDirection(point, step = 2)
{
// Array directions
let directions = new Array();
// Move to up
if (this.pointY-step > 0 && this.matrix[this.pointY-step][this.pointX] == point) {
directions.push(1);
}
// Move to right
if (this.pointX+step < this.pointWidth && this.matrix[this.pointY][this.pointX+step] == point) {
directions.push(2);
}
// Move to down
if (this.pointY+step < this.pointHeight && this.matrix[this.pointY+step][this.pointX] == point) {
directions.push(3);
}
// Move to left
if (this.pointX-step > 0 && this.matrix[this.pointY][this.pointX-step] == point) {
directions.push(4);
}
// No direction
if (directions.length == 0) {
return 0;
}
// Random direction
let index = this.__getRandomNumber(0, directions.length);
return directions[index];
}
/**
* Make next step
* @private
* @param {Number} direction
* @param {Number} point
*/
__makeNextStep( direction, point )
{
if ( direction == 1 ) {
// Move to up
this.matrix[this.pointY--][this.pointX] = point;
this.matrix[this.pointY--][this.pointX] = point;
} else if ( direction == 2 ) {
// Move to right
this.matrix[this.pointY][this.pointX++] = point;
this.matrix[this.pointY][this.pointX++] = point;
} else if ( direction == 3 ) {
// Move to down
this.matrix[this.pointY++][this.pointX] = point;
this.matrix[this.pointY++][this.pointX] = point;
} else if ( direction == 4 ) {
// Move to left
this.matrix[this.pointY][this.pointX--] = point;
this.matrix[this.pointY][this.pointX--] = point;
} else {
return false;
}
return true;
}
}