-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
191 lines (164 loc) * 5.64 KB
/
script.js
File metadata and controls
191 lines (164 loc) * 5.64 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
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
let N = 8;
let board = [];
let isDarkTheme = true;
let queensLeft = N;
let history = [];
function createBoard(size) {
const boardElement = document.getElementById('board');
boardElement.innerHTML = '';
boardElement.style.gridTemplateColumns = `repeat(${size}, 1fr)`;
boardElement.style.gridTemplateRows = `repeat(${size}, 1fr)`;
board = Array.from({ length: size }, () => Array(size).fill(0));
queensLeft = size;
updateQueensLeft();
history = []; // Clear history when creating a new board
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
const cell = document.createElement('div');
cell.className = `cell ${(i + j) % 2 === 0 ? 'light' : 'dark'}`;
cell.addEventListener('click', () => toggleQueen(i, j));
boardElement.appendChild(cell);
}
}
}
function updateBoardSize(value) {
document.getElementById('boardSizeValue').innerText = value;
N = parseInt(value);
updateQueenFontSize(N);
createBoard(N);
}
function updateQueenFontSize(boardSize) {
const minSize = 3;
const maxSize = 12;
const isMobile = window.innerWidth <= 768;
const minFontSize = isMobile ? 1.3 : 1.7;
const maxFontSize = isMobile ? 2.3 : 3;
const fontSize = maxFontSize - (boardSize - minSize) * (maxFontSize - minFontSize) / (maxSize - minSize);
document.documentElement.style.setProperty(
isMobile ? '--queen-font-size-mobile' : '--queen-font-size',
`${fontSize}em`
);
}
function toggleQueen(row, col) {
history.push(board.map(row => [...row])); // Save current state to history
if (board[row][col] === 0 && queensLeft > 0) {
board[row][col] = 1;
queensLeft--;
} else if (board[row][col] === 1) {
board[row][col] = 0;
queensLeft++;
}
updateQueensLeft();
drawBoard();
highlightUnsafeQueens();
}
function updateQueensLeft() {
document.getElementById('queensLeft').textContent = queensLeft;
}
function drawBoard() {
const boardElement = document.getElementById('board');
const cells = boardElement.children;
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
const cell = cells[i * N + j];
cell.innerHTML = board[i][j] === 1 ? 'black queen' : '';
cell.className = `cell ${(i + j) % 2 === 0 ? 'light' : 'dark'}`;
cell.style.color = 'black';
}
}
}
function highlightUnsafeQueens() {
const boardElement = document.getElementById('board');
const unsafePositions = new Set();
// Find unsafe positions
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
if (board[i][j] === 1) {
// Check row and column
for (let k = 0; k < N; k++) {
if (k !== j) unsafePositions.add(`${i},${k}`);
if (k !== i) unsafePositions.add(`${k},${j}`);
}
// Check diagonals
for (let k = 1; k < N; k++) {
if (i + k < N && j + k < N) unsafePositions.add(`${i+k},${j+k}`);
if (i + k < N && j - k >= 0) unsafePositions.add(`${i+k},${j-k}`);
if (i - k >= 0 && j + k < N) unsafePositions.add(`${i-k},${j+k}`);
if (i - k >= 0 && j - k >= 0) unsafePositions.add(`${i-k},${j-k}`);
}
}
}
}
// Highlight unsafe queens
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
const cell = boardElement.children[i * N + j];
if (board[i][j] === 1 && unsafePositions.has(`${i},${j}`)) {
cell.classList.add('unsafe');
} else {
cell.classList.remove('unsafe');
}
}
}
}
function solveNQUtil(col) {
if (col >= N) return true;
for (let i = 0; i < N; i++) {
if (isSafe(i, col)) {
board[i][col] = 1;
if (solveNQUtil(col + 1)) return true;
board[i][col] = 0;
}
}
return false;
}
function isSafe(row, col) {
for (let i = 0; i < N; i++) {
if (board[row][i] === 1 || board[i][col] === 1) return false;
}
for (let i = row, j = col; i >= 0 && j >= 0; i--, j--) {
if (board[i][j] === 1) return false;
}
for (let i = row, j = col; i < N && j >= 0; i++, j--) {
if (board[i][j] === 1) return false;
}
return true;
}
function solve() {
history.push(board.map(row => [...row])); // Save current state to history
createBoard(N);
if (solveNQUtil(0)) {
drawBoard();
queensLeft = 0;
updateQueensLeft();
highlightUnsafeQueens(); // This will actually do nothing for a solved board
} else {
alert("Solution does not exist");
}
}
function resetBoard() {
history.push(board.map(row => [...row])); // Save current state to history
createBoard(N);
}
function toggleTheme() {
isDarkTheme = !isDarkTheme;
document.body.classList.toggle('light-theme', !isDarkTheme);
document.body.classList.toggle('dark-theme', isDarkTheme);
}
function undo() {
if (history.length > 0) {
board = history.pop();
drawBoard();
highlightUnsafeQueens();
queensLeft = N - board.flat().filter(cell => cell === 1).length;
updateQueensLeft();
}
}
window.onload = function() {
createBoard(N);
updateQueenFontSize(N);
document.getElementById('themeToggle').addEventListener('click', toggleTheme);
document.body.classList.toggle('dark-theme', isDarkTheme);
document.getElementById('undoBtn').addEventListener('click', undo);
document.body.classList.toggle('dark-theme', isDarkTheme);
};