-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
192 lines (173 loc) · 4.61 KB
/
main.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
let size = 8;
for (let i = 0; i < size; i++) {
document.writeln("<div>");
for (let j = 0; j < size; j++) {
document.writeln(`<div id="cell${i}${j}" class="cell"></div>`);
}
document.writeln("</div>");
}
for (let i = 0; i < size; i++) {
document.writeln(`<span id="queen${i}" class="queen">♛</span>`);
}
// the board show
// Main drawer function
function makeBoard() {
for (let i = 0; i < size; i++) {
let id = "queen" + i;
select(id).style.visibility = "hidden";
}
// helpers
draw();
init();
}
// Number of Queens
let k = 0;
/*
Position of the Queens ->
1st 2 3 4 5 6 7 8 */
let queens = [-1, -1, -1, -1, -1, -1, -1, -1];
let playing = null;
// speed of player
let speed = 100;
const select = (selector) => document.getElementById(selector);
const playBTN = document.querySelector(".play");
const resetBTN = document.querySelector(".reset");
const log = select("log");
// the board look
function draw() {
let count = 0;
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
id = "cell" + i + j;
select(id).style.top = (i + 1) * 40 + 30 + "px";
select(id).style.left = (j + 1) * 40 + "px";
if (count++ % 2 == 0) select(id).style.backgroundColor = "#333";
else select(id).style.backgroundColor = "#ddd";
}
count++;
}
}
// check queen position
function search() {
if (k == size) {
clearInterval(playing);
select("solved").style.cssText = "display: inline";
select("log_entry").insertAdjacentHTML(
"afterbegin",
`<li style="color: green">Solved...</li>`
);
return;
}
let j = findPosition(k);
if (j < 0) {
displayQueens();
queens[k] = -1;
k--;
select("log_entry").insertAdjacentHTML(
"afterbegin",
`<li><span style="color: red">Can not be placed in row ${k + 1}</span>
<strong>Backtrack to the row ${k}</strong><li>`
);
console.log(`Can not be placed in row ${k + 1}`);
} else {
queens[k] = j;
k++;
displayQueens();
if (k == size) {
clearInterval(playing);
select("solved").style.cssText = "display: inline";
select("log_entry").insertAdjacentHTML(
"afterbegin",
`<li style="color: green">Solved...</li>`
);
console.log("Solved!");
return;
} else {
select("log_entry").insertAdjacentHTML(
"afterbegin",
`<li>New Queen is placed in position [${k - 1}, ${queens[k - 1]}]</li>`
);
console.log(
`New Queen is placed in position [${k - 1}, ${queens[k - 1]}]`
);
}
}
}
// the position of the queen
function findPosition(k) {
let start = queens[k] + 1;
for (let j = start; j < size; j++) if (isValid(k, j)) return j;
return -1;
}
// is the queen position follows the constraints?
function isValid(row, column) {
for (let i = 1; i <= row; i++)
if (
queens[row - i] == column ||
queens[row - i] == column - i ||
queens[row - i] == column + i
)
return false;
return true;
}
// start moving the queens
function play() {
playing = setInterval(search, speed);
if (k == size) clearInterval(playing);
playBTN.style.cssText = "display: none;";
resetBTN.style.cssText = "display: inline;";
log.style.opacity = 1;
}
// reset the queens & speed
function reset() {
clearInterval(playing);
select("log_entry").innerHTML = "";
k = 0;
queens = [-1, -1, -1, -1, -1, -1, -1, -1];
displayQueens();
playBTN.style.cssText = "display: inline;";
resetBTN.style.cssText = "display: none;";
log.style.opacity = 0;
select("solved").style.cssText = "display: none";
}
// change the Speed
function changeSpeed() {
speed = select("speed").value;
reset();
}
// How to display queens?
function displayQueens() {
for (let i = 0; i < k; i++) {
let id = "queen" + i;
select(id).style.top = y + (i + 0) * 40 + "px";
select(id).style.left = x + (queens[i] + 0) * 40 + "px";
select(id).style.visibility = "visible";
}
for (let i = k < 0 ? 0 : k; i < size; i++) {
let id = "queen" + i;
select(id).style.visibility = "hidden";
}
}
// Position from element
function getElementPos(element) {
let box = element.getBoundingClientRect();
return {
x: box.left,
y: box.top,
w: box.right - box.left - 5,
h: box.bottom - box.top - 5,
};
}
// initalize the scene
function init() {
posLoc = getElementPos(select("main"));
x = posLoc.x;
y = posLoc.y;
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
let id = "cell" + i + j;
select(id).style.top = y + j * 40 + "px";
select(id).style.left = x + i * 40 + "px";
}
}
}