forked from OPCODE-Open-Spring-Fest/Algo-Visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNQueensVisualizer.jsx
More file actions
277 lines (256 loc) · 9.17 KB
/
NQueensVisualizer.jsx
File metadata and controls
277 lines (256 loc) · 9.17 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
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
import React, { useState, useEffect, useRef } from "react";
import { nQueensVisualizerSteps } from "../../algorithms/Recursion/NQueens";
const DEFAULT_N = 5;
const MIN_N = 4;
const MAX_N = 10;
const DEFAULT_SPEED = 500;
function Chessboard({ board, step, N }) {
if (
!board ||
board.length !== N ||
board.some((row) => !row || row.length !== N)
) {
return (
<div className="text-red-400 text-center p-10">
Loading chessboard...
</div>
);
}
const BOARD_SIZE = 440;
const cellSize = BOARD_SIZE / N;
return (
<div
className="grid mx-auto border-[6px] border-gray-700 rounded-xl shadow-lg overflow-hidden bg-gray-800"
style={{
width: BOARD_SIZE,
height: BOARD_SIZE,
gridTemplateRows: `repeat(${N}, ${cellSize}px)`,
gridTemplateColumns: `repeat(${N}, ${cellSize}px)`,
}}
>
{Array.from({ length: N }).map((_, row) =>
Array.from({ length: N }).map((_, col) => {
const cell = board[row][col];
const isQueen = cell === 1;
let color = (row + col) % 2 === 0 ? "bg-gray-100" : "bg-gray-400";
if (step) {
if (step.type === "try" && step.row === row && step.col === col)
color = "bg-blue-300";
else if (step.type === "check" && step.row === row && step.col === col)
color = step.safe === false ? "bg-yellow-300" : "bg-green-300";
else if (step.type === "place" && step.row === row && step.col === col)
color = "bg-green-500";
else if (step.type === "remove" && step.row === row && step.col === col)
color = "bg-gray-500";
else if (step.type === "solution" && board[row][col] === 1)
color = "bg-lime-400";
}
return (
<div
key={`${row}-${col}`}
className={`flex items-center justify-center border border-gray-600 ${color}`}
>
{isQueen && (
<span
className="text-gray-800"
style={{ fontSize: `${cellSize * 0.7}px` }}
>
♛
</span>
)}
</div>
);
})
)}
</div>
);
}
function StackTrace({ stack }) {
return (
<div className="bg-gray-800 p-4 rounded-lg border border-gray-700 mb-3 shadow-sm w-100">
<h3 className="text-indigo-400 font-semibold text-lg mb-2">
Recursion Stack
</h3>
{stack.length === 0 ? (
<div className="text-gray-400 text-sm italic">Stack empty</div>
) : (
<div className="flex flex-wrap gap-2">
{stack.map((pos, idx) => (
<span
key={idx}
className="px-3 py-1 bg-indigo-600 text-white rounded-md text-sm"
>
Row-{pos.row}, Col-{pos.col}
</span>
))}
</div>
)}
</div>
);
}
function Legend({ color, text, symbol }) {
return (
<div className="flex items-center gap-2 mb-1">
<div
className={`w-5 h-5 rounded border border-gray-600 flex items-center justify-center ${color}`}
>
{symbol}
</div>
<span className="text-gray-300 text-sm">{text}</span>
</div>
);
}
export default function NQueensVisualizer() {
const [N, setN] = useState(DEFAULT_N);
const [speed, setSpeed] = useState(DEFAULT_SPEED);
const [steps, setSteps] = useState([]);
const [currentStepIdx, setCurrentStepIdx] = useState(0);
const [isPlaying, setIsPlaying] = useState(false);
const [solutionCount, setSolutionCount] = useState(0);
const [solvable, setSolvable] = useState(null);
const timerRef = useRef(null);
const runSolver = () => {
const result = nQueensVisualizerSteps(N);
setSteps(result.steps);
setSolutionCount(result.solutionCount);
setSolvable(result.solvable);
setCurrentStepIdx(0);
setIsPlaying(false);
};
useEffect(() => {
setSteps([]);
setCurrentStepIdx(0);
setSolvable(null);
setSolutionCount(0);
runSolver();
}, [N]);
useEffect(() => {
if (isPlaying && currentStepIdx < steps.length - 1) {
timerRef.current = setTimeout(() => {
setCurrentStepIdx((idx) => idx + 1);
}, speed);
} else {
clearTimeout(timerRef.current);
setIsPlaying(false);
}
return () => clearTimeout(timerRef.current);
}, [isPlaying, currentStepIdx, steps, speed]);
const step = steps[currentStepIdx] || {};
const board = step.board || Array.from({ length: N }, () => Array(N).fill(0));
return (
<div className="p-6 min-h-screen bg-gray-900 text-gray-100 font-sans">
<div className="max-w-7xl mx-auto">
<h1 className="text-4xl font-bold mb-6 text-center text-indigo-400">
♛ N-Queens Visualizer
</h1>
{/* Controls */}
<div className="flex flex-wrap gap-6 mb-10 p-6 rounded-lg bg-gray-800 border border-gray-700 justify-center items-center">
<div>
<label className="text-gray-300 text-sm">Board Size (N):</label>
<input
type="number"
min={MIN_N}
max={MAX_N}
value={N}
onChange={(e) =>
setN(Math.max(MIN_N, Math.min(MAX_N, Number(e.target.value))))
}
className="w-20 mt-1 p-2 rounded-md bg-gray-700 text-white border border-gray-600"
/>
</div>
<div>
<label className="text-gray-300 text-sm">Speed (ms):</label>
<input
type="number"
min={100}
max={2000}
value={speed}
onChange={(e) =>
setSpeed(Math.max(100, Math.min(2000, Number(e.target.value))))
}
className="w-20 mt-1 p-2 rounded-md bg-gray-700 text-white border border-gray-600"
/>
</div>
<button
onClick={runSolver}
className="bg-indigo-600 hover:bg-indigo-700 text-white font-medium px-6 py-2 rounded-md"
>
Start
</button>
<button
onClick={() => setIsPlaying((p) => !p)}
className={`px-5 py-2 rounded-md font-medium ${
isPlaying ? "bg-red-600" : "bg-green-600"
} text-white`}
>
{isPlaying ? "Pause" : "Play"}
</button>
<button
onClick={() => setCurrentStepIdx((idx) => Math.max(0, idx - 1))}
disabled={currentStepIdx === 0}
className={`px-4 py-2 rounded-md font-medium ${
currentStepIdx > 0
? "bg-gray-700 hover:bg-gray-600 text-white"
: "bg-gray-800 text-gray-500"
}`}
>
Prev
</button>
<button
onClick={() =>
setCurrentStepIdx((idx) => Math.min(steps.length - 1, idx + 1))
}
disabled={currentStepIdx === steps.length - 1}
className={`px-4 py-2 rounded-md font-medium ${
currentStepIdx < steps.length - 1
? "bg-gray-700 hover:bg-gray-600 text-white"
: "bg-gray-800 text-gray-500"
}`}
>
Next
</button>
</div>
{/* Main Layout*/}
<div className="flex flex-col lg:flex-row gap-8 items-start justify-center">
{/*Chessboard*/}
<div className="flex flex-col items-center ml-16">
<Chessboard board={board} step={step} N={N} />
<div className="mt-4 text-center text-gray-300 text-sm">
Step {currentStepIdx + 1}/{steps.length} • Solutions Found:{" "}
<b className="text-green-400">{solutionCount}</b>
{solvable === false && (
<p className="text-red-400 font-medium mt-1">
No solution for N={N}
</p>
)}
</div>
</div>
<div className="flex-1 flex flex-col gap-5 ml-26">
<div className="bg-gray-800 p-4 rounded-lg border border-gray-700 w-100">
<h3 className="text-indigo-400 font-semibold mb-2 text-lg">
Step Explanation
</h3>
<p className="text-gray-200 text-base leading-relaxed">
{step.message || "Start solving N-Queens..."}
</p>
</div>
<StackTrace stack={step.stack || []} />
<div className="bg-gray-800 border border-gray-700 rounded-lg p-4 shadow-sm w-100">
<h3 className="text-indigo-400 font-semibold mb-3 text-lg">
Color Meanings
</h3>
<div className="grid grid-cols-2 gap-2">
<Legend color="bg-blue-300" text="Trying this cell" />
<Legend color="bg-yellow-300" text="Unsafe position" />
<Legend color="bg-green-300" text="Safe position" />
<Legend color="bg-green-500" text="Queen placed" symbol="♛" />
<Legend color="bg-gray-500" text="Queen removed (backtrack)" />
<Legend color="bg-lime-400" text="Final solution cell" symbol="♛" />
</div>
</div>
</div>
</div>
</div>
</div>
);
}