-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku.js
58 lines (49 loc) · 1.52 KB
/
sudoku.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
import { OnStart, findcell } from "./OnStart.js";
const AllSize = 9;
const SmallSize = 3;
export class Sudoku {
constructor(difficulty = 0) {
this.grid = OnStart(difficulty);
}
DuplicatePositions(row, column, value) {
const dup_Column = this.find_dup_column(row, column, value)
const dupl_Row = this.find_dup_row(row, column, value);
const dup_square = this.find_dup_square(row, column, value);
const duplicates = [...dup_Column, ...dupl_Row, ...dup_square];
return duplicates;
}
find_dup_column(row, column, value) {
const duplicates = [];
for (let i = 0; i < AllSize; i++) {
if (this.grid[i][column] === value && i !== row) {
duplicates.push({ row: i, column: column });
}
}
return duplicates;
}
find_dup_row(row, column, value) {
const duplicates = [];
for (let i = 0; i < AllSize; i++) {
if (this.grid[row][i] === value && i !== column) {
duplicates.push({ row: row, column: i });
}
}
return duplicates;
}
find_dup_square(row, column, value) {
const duplicates = [];
const beginRow = row - row % SmallSize;
const beginColumn = column - column % SmallSize;
for (let i = beginRow; i < beginRow + SmallSize; i++) {
for (let j = beginColumn; j < beginColumn + SmallSize; j++) {
if (this.grid[i][j] === value && i !== row && j !== column) {
duplicates.push({ row: i, column: j });
}
}
}
return duplicates;
}
EmptyCells() {
return Boolean(findcell(this.grid));
}
}