-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNonogramPage.jsx
More file actions
492 lines (473 loc) · 14.3 KB
/
NonogramPage.jsx
File metadata and controls
492 lines (473 loc) · 14.3 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import React, { useState, useEffect, useContext, useCallback } from 'react';
import { Link } from 'react-router-dom';
import {
ArrowLeftIcon,
XIcon,
EraserIcon,
LightbulbIcon,
ShapesIcon,
} from '@phosphor-icons/react';
import Seo from '../../components/Seo';
import { ToastContext } from '../../context/ToastContext';
import GenerativeArt from '../../components/GenerativeArt';
import BreadcrumbTitle from '../../components/BreadcrumbTitle';
const PUZZLES = [
[
[0, 1, 0, 1, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[0, 1, 1, 1, 0],
[0, 0, 1, 0, 0],
],
[
[1, 1, 1, 1, 1],
[1, 0, 0, 0, 0],
[1, 1, 1, 0, 0],
[1, 0, 0, 0, 0],
[1, 0, 0, 0, 0],
],
[
[0, 0, 1, 0, 0],
[0, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[0, 1, 1, 1, 0],
[0, 0, 1, 0, 0],
],
[
[0, 1, 0, 1, 0],
[1, 1, 1, 1, 1],
[0, 1, 0, 1, 0],
[1, 1, 1, 1, 1],
[0, 1, 0, 1, 0],
],
[
[0, 0, 1, 0, 0],
[0, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
],
[
[0, 1, 1, 1, 0],
[1, 0, 1, 0, 1],
[1, 0, 0, 0, 1],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0],
],
[
[1, 0, 0, 0, 1],
[1, 1, 0, 0, 1],
[1, 0, 1, 0, 1],
[1, 0, 0, 1, 1],
[1, 0, 0, 0, 1],
],
[
[1, 0, 1, 0, 1],
[0, 1, 0, 1, 0],
[1, 0, 1, 0, 1],
[0, 1, 0, 1, 0],
[1, 0, 1, 0, 1],
],
[
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[1, 1, 1, 1, 1],
[0, 0, 1, 0, 0],
[0, 0, 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 0, 1, 0, 0],
[0, 1, 0, 1, 0],
[1, 0, 0, 0, 1],
[0, 1, 0, 1, 0],
[0, 0, 1, 0, 0],
],
[
[0, 0, 0, 0, 0],
[0, 1, 0, 1, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[0, 1, 1, 1, 0],
],
];
const PUZZLENAMES = [
'Heart',
'Fez',
'Diamond',
'Cross',
'House',
'Smile',
'Letter N',
'Checker',
'Plus',
'Arrow',
'Tree',
'Fish',
'Letter U',
'Target',
'Stairs',
'Face',
'Letter A',
'Star',
'Spiral',
'Mushroom',
'Rook',
'Hollow',
'Boat',
];
const generateClues = (puzzle) => {
const numRows = puzzle.length;
const numCols = puzzle[0].length;
const rowClues = [];
const colClues = [];
for (let r = 0; r < numRows; r++) {
const row = puzzle[r];
let current = [];
let size = 0;
for (let c = 0; c < numCols; c++) {
if (row[c] === 1) size++;
else if (size > 0) {
current.push(size);
size = 0;
}
}
if (size > 0) current.push(size);
rowClues.push(current.length > 0 ? current : [0]);
}
for (let c = 0; c < numCols; c++) {
let current = [];
let size = 0;
for (let r = 0; r < numRows; r++) {
if (puzzle[r][c] === 1) size++;
else if (size > 0) {
current.push(size);
size = 0;
}
}
if (size > 0) current.push(size);
colClues.push(current.length > 0 ? current : [0]);
}
return { rowClues, colClues };
};
const NonogramPage = () => {
const appName = 'Nonogram';
const { addToast } = useContext(ToastContext);
const [solvedPuzzle, setSolvedPuzzle] = useState([]);
const [playerGrid, setPlayerGrid] = useState([]);
const [rowClues, setRowClues] = useState([]);
const [colClues, setColClues] = useState([]);
const [gameWon, setGameWon] = useState(false);
const [message, setMessage] = useState('');
const [puzzleName, setPuzzleName] = useState('');
const initGame = useCallback(() => {
const idx = Math.floor(Math.random() * PUZZLES.length);
const puzzle = PUZZLES[idx];
setPuzzleName(PUZZLENAMES[idx]);
setSolvedPuzzle(puzzle);
const { rowClues: r, colClues: c } = generateClues(puzzle);
setRowClues(r);
setColClues(c);
setPlayerGrid(
Array(puzzle.length)
.fill(null)
.map(() => Array(puzzle[0].length).fill(0)),
);
setGameWon(false);
setMessage('');
}, []);
useEffect(() => {
initGame();
}, [initGame]);
const checkWin = useCallback(() => {
if (solvedPuzzle.length === 0) return false;
for (let r = 0; r < solvedPuzzle.length; r++) {
for (let c = 0; c < solvedPuzzle[0].length; c++) {
if (playerGrid[r][c] === 1 && solvedPuzzle[r][c] !== 1) return false;
if (playerGrid[r][c] === 0 && solvedPuzzle[r][c] === 1) return false;
}
}
return true;
}, [playerGrid, solvedPuzzle]);
useEffect(() => {
if (playerGrid.length > 0 && solvedPuzzle.length > 0 && checkWin()) {
setGameWon(true);
setMessage(`Pattern Identified :: ${puzzleName} decrypted.`);
addToast({
title: 'Success',
message: 'Puzzle Solved!',
type: 'success',
});
}
}, [playerGrid, solvedPuzzle, checkWin, addToast, puzzleName]);
const handleCellClick = (row, col, event) => {
if (gameWon) return;
event.preventDefault();
setPlayerGrid((prev) => {
const next = prev.map((r) => [...r]);
if (event.type === 'contextmenu')
next[row][col] = next[row][col] === 2 ? 0 : 2;
else next[row][col] = next[row][col] === 1 ? 0 : 1;
return next;
});
};
return (
<div className="min-h-screen bg-[#050505] text-white selection:bg-emerald-500/30 font-sans">
<Seo
title="Nonogram | Fezcodex"
description="Solve complex picture logic puzzles using numerical identifiers."
keywords={['Fezcodex', 'nonogram', 'puzzle', 'logic', 'game']}
/>
<div className="mx-auto max-w-7xl px-6 py-24 md:px-12">
<header className="mb-20">
<Link
to="/apps"
className="mb-8 inline-flex items-center gap-2 text-xs font-mono text-gray-500 hover:text-white transition-colors uppercase tracking-widest"
>
<ArrowLeftIcon weight="bold" />
<span>Applications</span>
</Link>
<div className="flex flex-col md:flex-row md:items-end justify-between gap-12">
<div className="space-y-4">
<BreadcrumbTitle title="Nonogram" slug="ng" variant="brutalist" />
<p className="text-xl text-gray-400 max-w-2xl font-light leading-relaxed">
Picture logic protocol. Decrypt the pattern through grid-based
identification.
</p>
</div>
<div className="flex gap-12 font-mono">
<div className="flex flex-col">
<span className="text-[10px] text-gray-600 uppercase tracking-widest">
Target
</span>
<span className="text-3xl font-black text-emerald-500">
{puzzleName}
</span>
</div>
<div className="flex flex-col">
<span className="text-[10px] text-gray-600 uppercase tracking-widest">
Status
</span>
<span
className={`text-3xl font-black ${gameWon ? 'text-emerald-500' : 'text-white'}`}
>
{gameWon ? 'SOLVED' : 'ACTIVE'}
</span>
</div>
</div>
</div>
</header>
<div className="grid grid-cols-1 lg:grid-cols-12 gap-12">
{/* Main Action Column */}
<div className="lg:col-span-7 flex flex-col items-center">
<div className="relative border border-white/10 bg-white/[0.02] backdrop-blur-sm p-8 md:p-16 rounded-sm overflow-hidden group w-full flex items-center justify-center">
<div className="absolute inset-0 opacity-5 pointer-events-none">
<GenerativeArt
seed={appName + puzzleName}
className="w-full h-full"
/>
</div>
<div className="absolute top-0 left-0 w-1 h-0 group-hover:h-full bg-emerald-500 transition-all duration-500" />
<div className="relative z-10">
{/* Nonogram Grid Container */}
<div className="flex flex-col items-center">
<div className="flex">
<div className="w-20 h-20" />
<div className="flex gap-1">
{colClues.map((clues, i) => (
<div
key={i}
className="w-10 h-20 flex flex-col-reverse items-center justify-end font-mono text-[10px] text-gray-500 border-b border-white/5 pb-2"
>
{clues.map((c, j) => (
<span key={j}>{c}</span>
))}
</div>
))}
</div>
</div>
<div className="flex">
<div className="flex flex-col gap-1 pr-4">
{rowClues.map((clues, i) => (
<div
key={i}
className="h-10 w-20 flex items-center justify-end font-mono text-[10px] text-gray-500 border-r border-white/5 pr-2"
>
{clues.map((c, j) => (
<span key={j} className="ml-1">
{c}
</span>
))}
</div>
))}
</div>
<div
className="grid gap-1"
style={{
gridTemplateColumns: `repeat(${solvedPuzzle[0]?.length || 5}, 40px)`,
}}
>
{playerGrid.map((row, r) =>
row.map((cell, c) => (
<button
key={`${r}-${c}`}
onClick={(e) => handleCellClick(r, c, e)}
onContextMenu={(e) => handleCellClick(r, c, e)}
className={`w-10 h-10 border border-white/10 rounded-sm transition-all duration-150 flex items-center justify-center
${cell === 1 ? 'bg-emerald-500 border-emerald-400' : 'bg-white/5 hover:bg-white/10'}
${gameWon && solvedPuzzle[r][c] === 1 ? 'bg-white text-black border-white scale-95' : ''}
`}
>
{cell === 2 && (
<XIcon size={16} className="text-gray-600" />
)}
</button>
)),
)}
</div>
</div>
</div>
</div>
</div>
{message && (
<p className="mt-8 text-xl font-mono text-emerald-400 uppercase tracking-widest text-center">
{message}
</p>
)}
</div>
{/* Info Column */}
<div className="lg:col-span-5 space-y-8">
<div className="bg-white/5 border border-white/10 p-8 rounded-sm">
<div className="flex items-center gap-3 mb-6 text-emerald-500">
<LightbulbIcon size={20} weight="bold" />
<h4 className="font-mono text-[10px] font-bold uppercase tracking-widest">
Logic_Manual
</h4>
</div>
<ul className="space-y-4 text-xs font-mono text-gray-500 uppercase tracking-wider leading-relaxed">
<li className="flex gap-3">
<span className="text-emerald-500">01</span> Fill cells based
on row/column numbers.
</li>
<li className="flex gap-3">
<span className="text-emerald-500">02</span> Numbers represent
consecutive filled blocks.
</li>
<li className="flex gap-3">
<span className="text-emerald-500">03</span> Left-click to
fill, Right-click to mark empty space.
</li>
</ul>
</div>
<div className="flex flex-col gap-3">
<button
onClick={initGame}
className="w-full py-4 bg-white text-black font-black uppercase tracking-[0.3em] hover:bg-emerald-400 transition-all text-sm flex items-center justify-center gap-3"
>
<ShapesIcon weight="bold" size={18} />
New Pattern
</button>
<button
onClick={() =>
setPlayerGrid(
Array(solvedPuzzle.length)
.fill(null)
.map(() => Array(solvedPuzzle[0].length).fill(0)),
)
}
className="w-full py-3 border border-white/10 text-gray-500 hover:text-white hover:bg-white/5 transition-all font-mono text-[10px] uppercase tracking-widest flex items-center justify-center gap-2"
>
<EraserIcon />
Clear Current
</button>
</div>
</div>
</div>
</div>
</div>
);
};
export default NonogramPage;