-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
305 lines (277 loc) · 6.68 KB
/
game.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
//
// File: game.js
// Desc: Game logic for 4x4 Sudoku
// Author: Yuriy koziy
//
"use strict";
var moveCounter;
var moveHistory = [];
var originalHTML;
// document.getElementById wrapper
var $ = function(id) {
return document.getElementById(id);
};
//check if the board is full
function checkFull(state) {
var array = [], i = 0, j = 0;
for (i = 0; i < state.length; i += 1) {
array = state[i];
for (j = 0; j < state.length; j += 1) {
if (array[j] === 0) {
return 1;
}
}
}
return 0;
}
//disables undo
function disableUndo() {
$("btnUndo").disabled = true;
}
//enables undo
function enableUndo() {
$("btnUndo").disabled = false;
}
//marks a cell with selected number
function markCell(cell, value) {
var element = $('c' + cell),
string = "<table id='" + cell + "'> <td><span>" + value + "</span></td> </table>";
element.innerHTML = string;
}
//reset board to original state
function resetBoard() {
var element = $("board"),
data = $("mcount");
element.innerHTML = originalHTML;
data.innerHTML = 0;
}
// set hint based on number, row wumber and cell number
function setHint(cellNum, num, rowNum) {
var element, cell;
if (rowNum === 0) {
cell = 'c' + cellNum;
} else if (rowNum === 1) {
cellNum = cellNum + 4;
cell = 'c' + cellNum;
} else if (rowNum === 2) {
cellNum = cellNum + 8;
cell = 'c' + cellNum;
} else if (rowNum === 3) {
cellNum = cellNum + 12;
cell = 'c' + cellNum;
}
if (num > 0 && num < 3) {
//element = $(cell).rows[0].cells['n' + num];
element = $(cell).rows[0].getElementsByClassName('n' + num)[0];
if (element !== undefined) {
element.outerHTML = '<td></td>';
}
}
if (num > 2 && num < 5) {
//element = $(cell).rows[1].cells['n' + num];
element = $(cell).rows[1].getElementsByClassName('n' + num)[0];
if (element !== undefined) {
element.outerHTML = '<td></td>';
}
}
}
//check for duplicates in a row and sets hints
function checkRow(board, row) {
var hit = [0, 0, 0, 0, 0],
emptyCells = [],
ret = 1;
for(var col = 0; col < 4; col += 1) {
if(board[row][col] !== 0) {
if(hit[board[row][col]] === 1) {
ret = 0;
}
hit[board[row][col]] = 1;
} else {
emptyCells.push(col);
}
}
//set hints
if (emptyCells.length !== 4) {
for (var m = 0; m < hit.length; m += 1) {
if (hit[m] == 1) {
for (var z = 0; z < emptyCells.length; z += 1) {
setHint(emptyCells[z], m , row);
}
}
}
}
if(ret === 0) {
return false;
}
return true;
}
//check for duplicates in a column and sets hints
function checkColumn(board, col) {
var hit = [0, 0, 0, 0, 0],
emptyCells = [],
ret = 1;
for(var row = 0; row < 4; row += 1) {
if(board[row][col] !== 0) {
if(hit[board[row][col]] === 1) {
ret = 0;
}
hit[board[row][col]] = 1;
} else {
emptyCells.push(row);
}
}
//set hints
if (emptyCells.length !== 4) {
for (var m = 0; m < hit.length; m += 1) {
if (hit[m] == 1) {
for (var z = 0; z < emptyCells.length; z += 1) {
setHint(col, m, emptyCells[z]);
}
}
}
}
if(ret === 0) {
return false;
}
return true;
}
//check for duplicates in quadrants and sets hints
function checkQuadrant(board, row, col) {
var hit = [0, 0, 0, 0, 0],
emptyCells = [],
ret = 1;
for(var i = row; i < row+2; i += 1) {
for(var j = col; j < col+2; j += 1) {
if(board[i][j] !== 0) {
if(hit[board[i][j]] === 1) {
ret = 0;
}
hit[board[i][j]] = 1;
} else {
emptyCells.push({r: i, c: j});
}
}
}
//set hints
if(emptyCells.length !== 4)
{
for(var m = 0; m < hit.length; m += 1) {
if(hit[m] == 1) {
for(i in emptyCells) {
setHint(emptyCells[i].c, m, emptyCells[i].r);
}
}
}
}
if(ret === 0) {
return false;
}
return true;
}
//read board state and returns an array of current board state
function readState() {
var state = [], array = [], i = 0, element, data;
for (i = 0; i < 16; i += 1) {
element = $('c' + i);
data = element.getElementsByTagName("span");
if (data[0] !== undefined) {
array.push(data[0].innerHTML);
if ((i + 1) % 4 === 0) {
state.push(array);
array = [];
}
} else {
array.push(0);
if ((i + 1) % 4 === 0) {
state.push(array);
array = [];
}
}
}
return state;
}
//apply state from array to the board
function applyState(state) {
var cellCount = 0, i = 0, j = 0, array = [];
for (i = 0; i < state.length; i += 1) {
array = state[i];
for (j = 0; j < state.length; j += 1) {
if (array[j] !== 0) {
markCell(cellCount, array[j]);
}
cellCount += 1;
}
}
}
//increment move counter
function addMove() {
moveCounter += 1;
var data = $("mcount");
data.innerHTML = moveCounter;
}
//undo a move and decrement move counter
function undo() {
if (moveCounter > 0) {
moveCounter -= 1;
var data = $("mcount");
data.innerHTML = moveCounter;
if (moveCounter === 0) {
resetBoard();
moveHistory = [];
applyState(initBoard);
checkSudoku(initBoard);
disableUndo();
} else {
moveHistory.pop();
resetBoard();
applyState(moveHistory[moveCounter - 1]);
checkSudoku(moveHistory[moveCounter - 1]);
}
}
}
//initialize the game board
function initialize() {
originalHTML = $("board").innerHTML;
applyState(initBoard);
moveCounter = 0;
disableUndo();
checkSudoku(initBoard);
}
//check if sudoku was completed
function checkBoard(currState) {
if (checkSudoku(currState) === true) {
if (checkFull(currState) === 0) {
alert("Victory!");
resetBoard();
initialize();
}
}
}
function checkSudoku(currState) {
for(var col = 0; col < 4; col += 1) {
if(checkColumn(currState, col) === false) {
return false;
}
}
for(var row = 0; row < 4; row += 1) {
if(checkRow(currState, row) === false) {
return false;
}
}
for(row = 0; row < 4; row += 2) {
for(col = 0; col < 4; col += 2) {
if(checkQuadrant(currState, row, col) === false) {
return false;
}
}
}
return true;
}
//handles click events for each number in a cell
function clickNumber(cell, value) {
markCell(cell, value);
addMove();
enableUndo();
moveHistory.push(readState());
checkBoard(moveHistory[moveHistory.length - 1]);
}