-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle.js
227 lines (191 loc) · 6.98 KB
/
puzzle.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
class Puzzle {
constructor() {
this.title = '';
this.cells = [];
this.rowNumbers = [];
this.maxRowNumbers;
this.colNumbers = [];
this.maxColNumbers;
this.currentMouseCol = -1;
this.currentMouseRow = -1;
this.currentPaintCol = null;
this.currentPaintRow = null;
this.currentPaintDirection = null;
this.currentPaintColour = null;
}
import(title, puzzleText) {
this.title = title;
const lines = puzzleText.split("\n");
const rowNumbers = [];
const colNumbers = [];
let row = 0;
while (row < lines.length && lines[row] != '') {
rowNumbers.push(lines[row].split(','));
row++;
}
row++;
while (row < lines.length && lines[row] != '') {
colNumbers.push(lines[row].split(','));
row++;
}
this.setSize(colNumbers, rowNumbers);
}
setSize(colNumbers, rowNumbers) {
this.width = colNumbers.length;
this.height = rowNumbers.length;
this.setColNumbers(colNumbers);
this.setRowNumbers(rowNumbers);
this.cells = [];
for (let row = 0; row < this.height; row++) {
const rowArray = [];
for (let col = 0; col < this.width; col++) {
const colour = Math.random() > 0.5 ? 'empty' : 'filled';
rowArray[col] = new Cell(colour);
}
this.cells[row] = rowArray;
}
}
setColNumbers(colNumbers) {
this.colNumbers = colNumbers;
this.maxColNumbers = 0;
for (const col of colNumbers) {
if (col.length > this.maxColNumbers) {
this.maxColNumbers = col.length;
}
}
}
setRowNumbers(rowNumbers) {
this.rowNumbers = rowNumbers;
this.maxRowNumbers = 0;
for (const row of rowNumbers) {
if (row.length > this.maxRowNumbers) {
this.maxRowNumbers = row.length;
}
}
}
render() {
let result = '';
result += '<table>';
// Top numbers
for (let numRow = 0; numRow < this.maxColNumbers; numRow++) {
result += '<tr>';
result += '<th colspan="' + this.maxRowNumbers + '"> </th>';
for (let numCol = 0; numCol < this.width; numCol++) {
const currentColNumbers = this.colNumbers[numCol];
const numberIndex = numRow + currentColNumbers.length - this.maxColNumbers;
const number = numberIndex >= 0 ? currentColNumbers[numberIndex] : '';
result += '<th>' + number + '</th>';
}
result += '</tr>';
}
// Main body
for(let idx = 0; idx < this.cells.length; idx++) {
const row = this.cells[idx];
result += '<tr>';
// Side numbers
for (let col = 0; col < this.maxRowNumbers; col++) {
const currentRowNumbers = this.rowNumbers[idx];
const numberIndex = col + currentRowNumbers.length - this.maxRowNumbers;
const number = numberIndex >= 0 ? currentRowNumbers[numberIndex] : '';
result += '<th>' + number + '</th>';
}
// Actual picture grid
for(const col in row) {
const cell = row[col];
const cellClass = cell;
result += '<td class="' + cellClass + '" data-row="' + idx + '" data-col="' + col + '"> </td>';
}
result += "</tr>\n";
}
result += '</table>';
return result;
}
getCell(row, col) {
return this.cells[row][col];
}
getCellFromMouseClick(node) {
const row = node.data('row');
const col = node.data('col');
return this.getCell(row, col);
}
leftMouseDown(node) {
const cell = this.getCellFromMouseClick(node);
this.currentMouseCol = node.data('col');
this.currentMouseRow = node.data('row');
if (cell.getColour() == 'empty') {
node.removeClass(cell.getColour());
cell.setColour('filled');
node.addClass(cell.getColour());
} else {
node.removeClass(cell.getColour());
cell.setColour('empty');
node.removeClass(cell.getColour());
}
this.currentPaintColour = cell.getColour();
}
mouseMove(node) {
const mouseRow = node.data('row');
const mouseCol = node.data('col');
if (this.currentPaintColour) {
const dx = mouseCol != this.currentMouseCol;
const dy = mouseRow != this.currentMouseRow;
if (!this.currentPaintDirection) {
if (dx) {
this.currentPaintDirection = 'h';
this.currentPaintRow = mouseRow;
} else if (dy) {
this.currentPaintDirection = 'v';
this.currentPaintCol = mouseCol;
}
}
if ((this.currentPaintDirection == 'h' && this.currentPaintRow == mouseRow && dx)
|| (this.currentPaintDirection == 'v' && this.currentPaintCol == mouseCol && dy)) {
// We have moved to a new cell in the current Row or Col
const cell = this.getCellFromMouseClick(node);
const origColour = cell.getColour();
if (origColour != this.currentPaintColour) {
cell.setColour(this.currentPaintColour);
node.removeClass(origColour);
node.addClass(this.currentPaintColour);
}
}
}
this.currentMouseRow = mouseRow;
this.currentMouseCol = mouseCol;
}
mouseUp(node) {
this.currentPaintColour = null;
this.currentPaintDirection = null;
this.currentPaintCol = null;
this.currentPaintRow = null;
}
unserialize(puzzleData) {
const puzzleVariables = JSON.parse(puzzleData);
this.title = puzzleVariables.title;
this.rowNumbers = puzzleVariables.rowNumbers;
this.height = puzzleVariables.rowNumbers.length;
this.colNumbers = puzzleVariables.ColNumbers;
this.width = puzzleVariables.colNumbers.length;
this.cells = puzzleVariables.cells;
}
serialize() {
const puzzle = {
title : this.title,
width : this.width,
height : this.height,
rowNumbers : this.rowNumbers,
colNumbers : this.colNumbers,
cells : this.cells
};
return JSON.stringify(puzzle);
}
drawLines(container) {
for (let i = 4; i < this.width; i+= 5) {
container.find('td[data-col=' + i + ']').css('border-right', '2px solid gray');
}
for (let i = 4; i < this.height; i+= 5) {
container.find('td[data-row=' + i + ']').css('border-bottom', '2px solid gray');
}
}
}
module.exports = [Cell, Puzzle];