-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathnumber-place-core.js
595 lines (528 loc) · 15.8 KB
/
number-place-core.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
class Grid {
/**
*
* @param {Number} row
* @param {Number} col
*/
constructor( row, col ) {
this.row = row;
this.col = col;
// this.blockRow = Math.floor( row / 3 );
// this.blockCol = Math.floor( col / 3 );
this.visibility = true;
this.value = 0;
this.userValue = 0;
}
/**
* 设置 value(数独终盘中的值)
* @param {Number} value
*/
setValue( value ) {
this.value = value;
}
/**
* 用户填写的值
* @param {Number} value
*/
placeValue( value ) {
this.userValue = value;
}
/**
* 判断用户填写的值是否正确
* @returns {Boolean}
*/
isRight() {
return this.value === this.userValue;
}
/**
* 设置方格是否可见
* @param {Boolean} v
*/
setVisible( v ) {
this.visibility = v;
}
/**
* 查询方格是否可见
*/
isVisible() {
return this.visibility;
}
/**
* 判断值 v 是否在有效区间内
* @param {Number} v
*/
static isValidValue(v) {
if( v > 0 && v < 10 ) {
return true;
}
return false;
}
/**
* 返回 grid 对象所在的块行列
* @param {{row: Number, col: number}} grid
* @returns {{row:Number, col: Number}}
*/
static blockBelonged(grid) {
let block = {};
block.row = Math.floor( grid.row / 3 );
block.col = Math.floor( grid.col / 3 );
return block;
}
}
class Utils {
/**
* 数组中的元素去重,只保留唯一的元素
* @param {Array<>} array 要去重的数组
* @returns {Array<>} 元素全部唯一的数组
*/
static distinctArray(array) {
let result = array.sort().reduce( (pre, current) => {
if( pre.length == 0 || pre[pre.length-1] != current ) {
pre.push( current );
}
return pre;
}, []);
return result;
}
/**
* 将 basic 数组中与 exclude 数组中相同的元素剔除掉
* @param {{basic: Array<Number>, exclude: Array<Number>, keepOrder: Boolean}} params
* @param {Array<Number>} basic 基本数组
* @param {Array<Number>} exclude 要排除的元素构成的数组
* @param {Bollean} keepOrder 是否保留原来的数组顺序,若否或无定义,则打乱数组
* @returns {Array<Number>} 去掉重叠元素的数组的数组
*/
static getRandomValue(params) {
params = ( params === undefined ) ? {} : params;
let basic = params.basic;
let exclude = params.exclude;
if( exclude && exclude.length > 0 ) {
basic = [];
for(let i = 1; i <= 9; i += 1) {
if(exclude.indexOf(i) == -1) {
basic.push(i)
}
}
// console.log("getR", basic, exclude)
basic.sort( (a, b) => {
return a > b; // 从小到大顺序排列
});
} else if( basic === undefined ) {
basic = this.genBasicArray();
// let basic = [1, 2, 3, 4, 5, 6, 7, 8, 9];
}
if( !params.keepOrder )
return this.randomArray(basic);
else
return basic;
}
/**
* 打乱数组
* @param {Array<>} array
* @returns 数组乱序排列的数组
*/
static randomArray(array) {
let size = array.length;
let arr = new Array(size);
let randomIndex;
let exCount = 0;
for(let i = 0; i < size; i++) {
randomIndex = Math.floor( Math.random() * (size - exCount) );
arr[i] = array[randomIndex];
array[randomIndex] = array[ size - 1 - exCount ];
array[ size - 1 - exCount ] = 0;
exCount++;
}
return arr;
}
/**
* 判断某一组 Grid 对象是否有效,值从 1 ~ 9 且不重复
* @param {Array<Number>} grids
*
* @returns {Boolean}
*/
static isGridsValueValid( gridsValue ) {
let sum = 0;
let value = this.distinctArray(gridsValue);
for( let i = 0; i < 9; i++ ) {
sum += value[i];
}
if( sum == 45 ) {
return true;
}
return false;
}
/**
* 将 1~9 这 9 个元素填充到数组并返回该数组
* @returns {Array<Number>} 包含 1~9 这 9 个顺序排列的数组
*/
static genBasicArray() {
return [1, 2, 3, 4, 5, 6, 7, 8, 9];
}
/**
* 顺序打印出所有格子
*/
static printAll(board) {
let toPrint = "";
let value, grid;
for( let i = 0; i < 9; i++ ) {
for( let j = 0; j < 9; j++ ) {
grid = board.grids[i * 9 + j];
if( grid.isVisible() )
value = grid.value
else
value = 'X';
toPrint += value + " ";
}
toPrint += "\n";
}
console.log( toPrint );
}
static printRow(board, row) {
let toPrint = "";
for( let j = 0; j < 9; j++ ) {
toPrint += board.grids[row * 9 + j].value + " ";
}
toPrint += "\n";
console.log( toPrint );
}
}
class Choice {
/**
*
* @param {Array<Number>} choiceSet
*/
constructor(choiceSet) {
this.choiceSet = choiceSet;
this.attemptIndex = -1;
}
/**
* 将索引移至下一个位置,并返回该位置的数字
*/
next() {
this.attemptIndex++;
if(this.attemptIndex <= this.choiceSet.length) {
return this.choiceSet[this.attemptIndex];
}
return undefined;
}
}
class Board {
constructor() {
this.grids = new Array( 81 );
for( let i = 0; i < 9; i++ ) {
for( let j = 0; j < 9; j++ ) {
this.grids[i * 9 + j] = new Grid( i, j );
}
}
}
init() {
// populate the first row
let row0 = this.getRowGrids(0);
let randomArray = Utils.getRandomValue();
randomArray.forEach( (element, index ) => {
row0[index].setValue( element );
});
// Utils.printAll(this);
let used, unused, grid, index;
let temp = 0;
for(let i = 1; i < 9; i += 1) {
for(let j = 0; j < 9; j += 1) {
grid = this.grids[i*9+j];
// process.stdout.write( `i=${i}, j=${j}. `);
if( grid.choice === undefined ) {
used = this.getUsedValueArrayAt( {row: i, col: j} );
unused = Utils.getRandomValue( {exclude: used} );
grid.choice = new Choice( unused );
}
index = this.populateGrid(grid);
// console.log(used, unused, i, j, index)
i = index.i;
j = index.j;
temp += 1;
}
}
}
/**
* 填充方格,并返回修正过的 i,j索引
*
* 该函数属于回溯部分,在方格没有可选数字时,修正索引,以便进行回溯
* @param {Grid} grid
* @returns {{i: Number, j: Number}}
*/
populateGrid(grid) {
let i = grid.row, j = grid.col;
let value = grid.choice.next();
if( value !== undefined ) {
// 有可以选择的数字
grid.setValue(value);
}
else {
// 没有可选的数字
// Utils.printRow(this, i);
grid.value = 0;
grid.choice = undefined;
if( j == 0 ) {
// i = (i > 0 ? i - 1 : i );
i -= 1;
j -= 1;
this.resetPartialGrids({rowStart: i, rowEnd: i+1, colStart: 1, colEnd: 9}); // 返回上一行后,清空该行其它列的数据
}
else {
j -= 2;
}
}
return {i, j};
}
/**
* 获取位置 pos 上可以存放的值集合(乱序)
*
* 注意,可能会出现每个位置没有可用的数字,这时该函数返回 0 而不是 undefined
* @param {{row: Number, col: Number}} pos
* @return Number
*/
getRandomValidValue(pos) {
let used = this.getUsedValueArrayAt(pos);
let valueArray = Utils.getRandomValue({exclude: used});
return valueArray[0] === undefined ? 0 : valueArray[0];
}
/**
* 按指定的模式,获得位置为(i,j)的使用过的数字集合
* @param {{mode: String, row: Number, col: Number}} cond
* mode can be row, column or block
* @returns Array<Number>
*/
getUsedValueArray( cond ) {
let grids = this.getGrids( cond );
let arr = new Array();
grids.forEach( grid => {
if( Grid.isValidValue( grid.value ) )
arr.push( grid.value )
});
return arr;
}
/**
* 获取 pos 位置上不可用的值集合,包括 pos 所在的行、列和块中有效的值
* @param {{row: Number, col: Number}} pos
* @returns Array<Number>
*/
getUsedValueArrayAt(pos) {
let row = pos.row;
let col = pos.col;
let used = new Array();
let block = Grid.blockBelonged(pos);
let rowGrids = this.getUsedValueArray( {mode: "row", row: row} );
let colGrids = this.getUsedValueArray( {mode: "column", col: col});
let blockGrids = this.getUsedValueArray( {mode: "block", row: block.row, col: block.col} );
used.push.apply( used, rowGrids );
used.push.apply( used, colGrids );
used.push.apply( used, blockGrids );
let result = Utils.distinctArray( used );
// console.log( `used at: ${row} ${col} ${result}` );
return result;
}
/**
* 获取第 row 行的所有格子对象
* @param {Number} row
* @return {Array<Grid>}
*/
getRowGrids( row ) {
let rowArray = new Array( 9 );
for( let i = 0; i < 9; i++ ) {
rowArray[i] = this.grids[i + row * 9];
}
return rowArray;
}
/**
* 获取第 col 列的所有格子对象
* @param {Number} col
* @return {Array<Grid>}
*/
getColumnGrids( col ) {
let colArray = new Array( 9 );
for( let i = 0; i < 9; i++ ) {
colArray[i] = this.grids[i * 9 + col];
}
return colArray;
}
/**
* 获取某一块(块的行数为 row, 块的列数为 col)的格子对象
* @param {Number} row
* @param {Number} col
* @return {Array<Grid>}
*/
getBlockGrids( row, col ) {
let block = new Array( 9 );
for( let i = 0; i < 3; i++ ) {
for( let j = 0; j < 3; j++ ) {
block[i * 3 + j] = this.grids[ ( row ) * 27 + col * 3 + i * 9 + j ];
}
}
return block;
}
/**
* 获取任意有效区域的格子
* row range: [rowStart, rowEnd) , rowStart is INCLUDED while rowEnd is not
* col range: [colStart, colEnd)
* @param {{rowStart: Number, colStart: Number, rowEnd: Number, colEnd: Number}} area
* @return {Array<Grid>}
*/
getPartialGrids(area) {
let size = (area.rowEnd - area.rowStart) * (area.colEnd - area.colStart);
if( size <= 0 ) {
return new Array();
}
let part = new Array();
for(let i = area.rowStart; i < area.rowEnd; i++) {
for( let j = area.colStart; j < area.colEnd; j++ ) {
part.push( this.grids[i*9+j] );
}
}
return part;
}
/**
* 将某部分格子的值重设为 0
*
* row range: [rowStart, rowEnd) , rowStart is INCLUDED while rowEnd is not
*
* col range: [colStart, colEnd)
* @param {{rowStart: Number, colStart: Number, rowEnd: Number, colEnd: Number}} area
*/
resetPartialGrids(area) {
let part = this.getPartialGrids(area);
part.forEach( grid => {
grid.value = 0;
grid.choice = undefined;
});
}
/**
* 根据 cond 参数获取格子
* @param {{mode: String, row:Number, col: Number}} cond
*/
getGrids(cond) {
let mode = cond.mode;
let grids;
switch (mode) {
case "row":
grids = this.getRowGrids( cond.row );
break;
case "column":
grids = this.getColumnGrids( cond.col );
break;
case "block":
grids = this.getBlockGrids( cond.row, cond.col );
break;
default:
grids = new Array();
break;
}
return grids;
}
/**
* 用户填写第 row 行,第 col 列的值 num
* @param {Number} row
* @param {Number} col
* @param {Number} num
*/
setNumberAt( row, col, num ) {
if( row < 0 || row > 8
|| col < 0 || col > 8
|| num < 0 || num > 9 ) {
console.log( "Position or Number is invalid." )
return;
}
let grid = this.grids[row * 9 + col];
grid.placeValue( num );
}
/**
* 判断当前棋盘是否是有效的数独终盘,若不符合数独的规则,则返回 false
* @param {Boolean} userPlaced 是否判断用户填写的值
*/
isValid(userPlaced) {
let grids;
let sum = 0;
let valid = true;
for(let i = 0; i < 9; i++) {
grids = this.getRowGrids(i);
sum = this.sumGrids( grids, userPlaced );
valid &= ( sum === 45 );
sum = 0;
}
for(let i = 0; i < 9; i++) {
grids = this.getColumnGrids(i);
sum = this.sumGrids( grids, userPlaced );
valid &= ( sum === 45 );
sum = 0;
}
for(let i = 0; i < 3; i++) {
for(let j = 0; j < 3; j++) {
grids = this.getBlockGrids(i, j);
sum = this.sumGrids( grids, userPlaced );
valid &= ( sum === 45 );
sum = 0;
}
}
return valid;
}
/**
*
* @param {Array<Grid>} grids
* @param {Boolean} userPlaced
*/
sumGrids(grids, userPlaced) {
let sum = 0;
grids.forEach(g => {
if( userPlaced && !g.isVisible()) {
sum += g.userValue;
}
else {
sum += g.value;
}
});
return sum;
}
}
Board.width = 9;
Board.height = 9;
class Game {
constructor( digTime ) {
this.board = new Board();
this.board.init();
this.digTimes = digTime;
this.digBoard();
}
/**
* 挖去一部分格子,将属性设为隐藏
*/
digBoard() {
let dig = 0, block;
for(let i = 0; i < 3; i++) {
for(let j = 0; j < 3; j++) {
for( let k = 0; k < this.digTimes; k++) {
block = this.board.getBlockGrids(i, j);
dig = Math.floor( Math.random() * 9 );
if( block[dig].isVisible() ) {
// avoid duplicated hiding
block[dig].setVisible(false);
}
}
}
}
// Utils.printAll(this.board);
}
/**
* 若返回值为 undefined,说明该位置的值不应该显示出来
* @param {Number} row 行索引
* @param {Number} col 列索引
*/
getValueAt(row, col) {
let grid = this.board.grids[ row * 9 + col ];
if( grid.isVisible() )
return grid.value;
return undefined
}
}
Game.DifficutyEasy = 1;
Game.DifficutyNormal = 2;
Game.DifficutyHard = 3;
// export {Game, Board, Grid}