-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathnonogram.js
More file actions
111 lines (101 loc) · 2.81 KB
/
nonogram.js
File metadata and controls
111 lines (101 loc) · 2.81 KB
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
export const STATES = {
UNKNOWN:0,
SELECTED:1,
EMPTY:2
}
export default class Nonogram {
constructor(data, name) {
data.reverse()
this.data = data
this.width = this.data[0].length
this.height = this.data.length
this.name = name || "==="
this.state = []
this.reset()
}
getName() {
return this.name
}
reset() {
for(let j=0; j<this.height; j++) {
this.state[j] = []
for(let i=0; i<this.width; i++) {
this.state[j][i] = STATES.UNKNOWN
}
}
}
getCellState(i,j) {
return this.state[j][i]
}
setCellState(i,j, val) {
this.state[j][i] = val
}
rollCellState(i,j) {
let cur = this.getCellState(i,j)
if(cur === STATES.UNKNOWN) this.setCellState(i,j,STATES.SELECTED)
if(cur === STATES.SELECTED) this.setCellState(i,j,STATES.EMPTY)
if(cur === STATES.EMPTY) this.setCellState(i,j,STATES.UNKNOWN)
}
checkValid() {
let correctCount = 0
for(let j=0; j<this.height; j++) {
for(let i=0; i<this.width; i++) {
const ans = this.data[j][i]
const cur = this.state[j][i]
if(ans === 1 && cur === 1) correctCount++
if(ans === 0 && (cur === 0 || cur === 2)) correctCount++
}
}
return correctCount
}
checkInvalid() {
let bad = 0
for(let j=0; j<this.height; j++) {
for (let i = 0; i < this.width; i++) {
const ans = this.data[j][i]
const cur = this.state[j][i]
if(ans === 1 && cur === STATES.EMPTY) bad++
if(ans === 0 && cur === STATES.SELECTED) bad++
}
}
return bad
}
checkFinished() {
return this.checkValid() === this.width * this.height
}
isSolution(x,y) {
return this.data[y][x] === 1
}
calculateColumnCounts(col) {
const counts = []
let count = 0;
let prev = 0
for(let j=0; j<this.height; j++) {
const val = this.data[j][col]
if(val === 1) count++
if(val === 0 && val !== prev) {
counts.push(count)
count = 0
}
prev = val
}
if(count > 0) counts.push(count)
return counts
}
calculateRowCounts(row) {
const counts = []
let count = 0;
let prev = 0
for(let j=0; j<this.width; j++) {
const val = this.data[row][j]
if(val === 1) count++
if(val === 0 && val !== prev) {
counts.push(count)
count = 0
}
prev = val
}
if(count > 0) counts.push(count)
return counts
}
}