-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueen.js
144 lines (124 loc) · 5.58 KB
/
queen.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
let queensPlaced = 0;
let boardSize = 8;
function createBoard(size) {
const board = document.getElementById('board');
board.innerHTML = '';
board.style.gridTemplateColumns = `repeat(${size}, 1fr)`;
for (let row = 0; row < size; row++) {
for (let col = 0; col < size; col++) {
const cell = document.createElement('div');
cell.className = `cell ${(row + col) % 2 === 0 ? 'white' : 'black'}`;
cell.dataset.row = row;
cell.dataset.col = col;
cell.addEventListener('click', handleClick);
board.appendChild(cell);
}
}
}
function startGame() {
boardSize = parseInt(document.getElementById('queenCount').value);
queensPlaced = 0;
updateQueenCounter(); // Reset the counter
createBoard(boardSize);
document.getElementById('message').textContent = '';
}
function handleClick(event) {
const cell = event.target;
const rect = cell.getBoundingClientRect();
createParticles(rect.left + 25, rect.top + 25);
const row = parseInt(cell.dataset.row);
const col = parseInt(cell.dataset.col);
if (cell.classList.contains('queen')) {
cell.textContent = '';
cell.classList.remove('queen');
clearAttacked(row, col);
queensPlaced--;
} else if (!cell.classList.contains('attacked')) {
cell.textContent = '♛';
cell.classList.add('queen');
markAttacked(row, col);
queensPlaced++;
}
updateQueenCounter(); // Update the counter after placing or removing a queen
if (queensPlaced === boardSize) {
document.getElementById('message').textContent = `Congratulations! ${boardSize} Queens placed successfully!`;
celebrate();
} else {
document.getElementById('message').textContent = '';
}
}
function createParticles(x, y) {
for (let i = 0; i < 8; i++) {
const particle = document.createElement('div');
particle.className = 'particle';
particle.style.left = x + 'px';
particle.style.top = y + 'px';
particle.style.background = `hsl(${Math.random() * 360}, 70%, 50%)`;
particle.style.width = '8px';
particle.style.height = '8px';
particle.style.borderRadius = '50%';
document.body.appendChild(particle);
setTimeout(() => particle.remove(), 1000);
}
}
function markAttacked(row, col) {
const cells = document.querySelectorAll('.cell');
cells.forEach(cell => {
const r = parseInt(cell.dataset.row);
const c = parseInt(cell.dataset.col);
if (r === row || c === col || (r - c) === (row - col) || (r + c) === (row + col)) {
cell.classList.add('attacked');
}
});
}
function clearAttacked(row, col) {
const cells = document.querySelectorAll('.cell');
cells.forEach(cell => {
const r = parseInt(cell.dataset.row);
const c = parseInt(cell.dataset.col);
let isStillAttacked = false;
document.querySelectorAll('.queen').forEach(queenCell => {
const qr = parseInt(queenCell.dataset.row);
const qc = parseInt(queenCell.dataset.col);
if (qr === r || qc === c || (qr - qc) === (r - c) || (qr + qc) === (r + c)) {
isStillAttacked = true;
}
});
if (!isStillAttacked) {
cell.classList.remove('attacked');
}
});
}
function resetBoard() {
const cells = document.querySelectorAll('.cell');
cells.forEach(cell => {
cell.classList.remove('queen', 'attacked');
cell.textContent = '';
});
queensPlaced = 0;
updateQueenCounter(); // Reset the counter
document.getElementById('message').textContent = '';
}
function celebrate() {
const count = 200;
const defaults = {
origin: { y: 0.7 }
};
function fire(particleRatio, opts) {
confetti(Object.assign({}, defaults, opts, {
particleCount: Math.floor(count * particleRatio)
}));
}
fire(0.25, { spread: 26, startVelocity: 55 });
fire(0.2, { spread: 60 });
fire(0.35, { spread: 100, decay: 0.91, scalar: 0.8 });
fire(0.1, { spread: 120, startVelocity: 25, decay: 0.92, scalar: 1.2 });
fire(0.1, { spread: 120, startVelocity: 45 });
}
function updateQueenCounter() {
const queenCounterElement = document.getElementById('queenCounter');
if (queenCounterElement) {
queenCounterElement.textContent = `Queens Placed: ${queensPlaced}/8`;
}
}
createBoard(8);