-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknight_tour_animation.html
247 lines (220 loc) · 8.38 KB
/
knight_tour_animation.html
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
<html>
<head>
<style>
.row {
display: flex;
}
body {
background-color: #eee;
}
.square {
width: 100px;
height: 100px;
border: 1px solid black;
}
#knight {
color: red;
font-weight: bold;
margin-top: 40px;
}
#alertText {
font-size: 2rem;
}
</style>
</head>
<body>
<center>
<input type="text" name="start" id="dimen" placeholder="Enter here">
<button id="start">START</button>
<p id="alertText">Enter the dimension of chess board and click START</p>
</center>
<script>
var dimen = null;
var startButton = document.getElementById('start');
var knight = null;
var board = null;
var body = document.getElementsByTagName('body')[0];
var inter = null;
var squareColor = "white";
var setPositionButton = document.getElementById('setPosition');
var tours = null;
var counter = 0;
let path_found = false;
const DIRECTIONS = [
[-1, 2],
[-1, -2],
[-2, 1],
[-2, -1],
[1, 2],
[1, -2],
[2, 1],
[2, -1]
];
function styleBoard(board, dimen) {
board.setAttribute('id', 'board');
board.style.width = `${dimen*100} px`;
board.style.height = `${dimen*100} px`;
board.style.diplay = 'flex';
board.style.marginTop = '50px';
board.style.marginLeft = '550px';
return board;
}
function buildSquaresOnBoard(board, dimen) {
for(let i=0;i<dimen;i++) {
let row = createRow(dimen);
for(let j=0;j<dimen;j++) {
square = createSquare(i, j, dimen);
row.appendChild(square);
}
board.appendChild(row);
}
return board;
}
function createBoard(dimen) {
var board = document.getElementById('board');
if(board !== null && board !== undefined) {
board.remove();
}
board = document.createElement('div');
board = styleBoard(board, dimen);
board = buildSquaresOnBoard(board, dimen);
document.getElementsByTagName('center')[0].appendChild(board);
return board;
}
function createRow(dimen) {
let row = document.createElement('div');
row.style.width = `${dimen*100} px`;
row.style.height = '100 px';
row.setAttribute('class', 'row');
return row;
}
function createPathNumber(number) {
let h3 = document.createElement('h3');
h3.setAttribute('class', 'squareNumber');
h3.style.position = "center";
h3.innerHTML = number;
h3.style.color = 'grey';
h3.style.marginTop = "40px";
h3.style.marginLeft = "20px";
return h3;
}
function createSquare(row, col, dimen) {
let square = document.createElement('div');
square.style.backgroundColor = getColor(row, col, dimen);
square.setAttribute('class', 'square');
square.setAttribute('id', `${row}${col}`);
let p = document.createElement('p');
p.innerHTML = `${row}${col}`;
p.style.float = 'right';
p.style.color = 'grey';
square.appendChild(p)
return square;
}
function toggleColor(color) {
if(color == "white") {
color = "black";
}else {
color = "white";
}
return color;
}
function createKnight() {
let knight = document.createElement('h2');
knight.setAttribute("id", "knight");
knight.innerHTML = "K";
return knight;
}
function getColor(row, col, dimen) {
return (row + col)%2 == 0 ? 'white':'black';
}
function setKnightToSquare(knight, square) {
square.appendChild(knight);
square.style.backgroundColor = '#EAB2F4';
}
function resetCounter() {
counter = 0;
}
function get2DSquares(n) {
const squares = [];
for (let i = 0; i < n; i++) {
const row = new Array(n).fill(false);
squares.push(row);
}
return squares;
}
function isValidPosition(row, col, squares) {
let dimen = squares.length;
return (row >= 0 && row < dimen) && (col >= 0 && col < dimen) && !squares[row][col];
}
function visit(squares, rowPos, colPos, depth = 1) {
squares[rowPos][colPos] = true;
const pos = `${rowPos}${colPos}`;
if (depth === squares.length * squares.length) {
return [true, [pos]];
}
for (const [dr, dc] of DIRECTIONS) {
const nextRow = rowPos + dr;
const nextCol = colPos + dc;
if (isValidPosition(nextRow, nextCol, squares)) {
const [pathFound, pth] = visit(squares, nextRow, nextCol, depth + 1);
if (pathFound) {
return [true, [pos, ...pth]];
}
}
}
squares[rowPos][colPos] = false;
return [false, null];
}
function setAlertText(content) {
var alertText = document.getElementById('alertText');
alertText.innerHTML = content;
}
function findKnightTours(dimen) {
for (let i = 0; i < dimen; i++) {
for (let j = 0; j < dimen; j++) {
const squares = get2DSquares(dimen);
const [pathFound, path] = visit(squares, i, j);
if (pathFound) {
return [pathFound, path];
}
}
}
return [false, null];
}
function setPathNumber(square, number) {
let h3 = createPathNumber(number);
square.appendChild(h3);
}
startButton.onclick = function() {
if(inter !== null) {
clearInterval(inter);
}
var dimen = document.getElementById('dimen').value;
dimen = parseInt(dimen);
if(isNaN(dimen) || dimen < 3 || dimen > 8){
alert("Please enter value between 3 and 8");
return;
}
board = createBoard(dimen);
knight = createKnight();
[path_found, tours] = findKnightTours(dimen);
if(path_found){
setAlertText('Path exists from ' + tours[0]);
inter = setInterval(function() {
let square = document.getElementById(tours[counter]);
setKnightToSquare(knight, square);
setPathNumber(square, counter);
counter = counter + 1;
if(counter >= tours.length) {
resetCounter();
clearInterval(inter);
return
}
}, 500)
}else {
setAlertText('No path found from any position');
}
}
</script>
</body>
</html>