-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMastermindPage.jsx
More file actions
284 lines (263 loc) · 10.7 KB
/
MastermindPage.jsx
File metadata and controls
284 lines (263 loc) · 10.7 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
import React, { useState, useEffect, useCallback } from 'react';
import { Link } from 'react-router-dom';
import {
ArrowLeftIcon,
LightbulbIcon,
TargetIcon,
ClockIcon,
} from '@phosphor-icons/react';
import { motion, AnimatePresence } from 'framer-motion';
import Seo from '../../components/Seo';
import { useToast } from '../../hooks/useToast';
import GenerativeArt from '../../components/GenerativeArt';
import BreadcrumbTitle from '../../components/BreadcrumbTitle';
const MastermindPage = () => {
const appName = 'Mastermind';
const { addToast } = useToast();
const [secretCode, setSecretCode] = useState('');
const [guesses, setGuesses] = useState([]);
const [currentGuess, setCurrentGuess] = useState('');
const [gameOver, setGameOver] = useState(false);
const [message, setMessage] = useState('');
const MAX_GUESSES = 10;
const generateSecretCode = useCallback(() => {
let digits = '0123456789'.split('');
let code = '';
for (let i = 0; i < 4; i++) {
const randomIndex = Math.floor(Math.random() * digits.length);
code += digits.splice(randomIndex, 1)[0];
}
setSecretCode(code);
}, []);
useEffect(() => {
generateSecretCode();
}, [generateSecretCode]);
const handleGuessSubmit = (e) => {
e.preventDefault();
if (gameOver) return;
if (
currentGuess.length !== 4 ||
!/^\d{4}$/.test(currentGuess) ||
new Set(currentGuess).size !== 4
) {
addToast({
title: 'Input Error',
message: 'Enter 4 unique digits.',
duration: 3000,
});
return;
}
let bulls = 0;
let cows = 0;
for (let i = 0; i < 4; i++) {
if (currentGuess[i] === secretCode[i]) {
bulls++;
} else if (secretCode.includes(currentGuess[i])) {
cows++;
}
}
const newGuesses = [
{ guess: currentGuess, bulls, cows, id: Date.now() },
...guesses,
];
setGuesses(newGuesses);
if (bulls === 4) {
setGameOver(true);
setMessage(`ACCESS_GRANTED :: Code identified as ${secretCode}`);
addToast({
title: 'Success',
message: 'Target cracked!',
type: 'success',
});
} else if (newGuesses.length >= MAX_GUESSES) {
setGameOver(true);
setMessage(`ACCESS_DENIED :: Secret code was ${secretCode}`);
addToast({
title: 'System Lock',
message: 'Too many failed attempts.',
type: 'error',
});
}
setCurrentGuess('');
};
const handleResetGame = () => {
generateSecretCode();
setGuesses([]);
setCurrentGuess('');
setGameOver(false);
setMessage('');
};
return (
<div className="min-h-screen bg-[#050505] text-white selection:bg-emerald-500/30 font-sans">
<Seo
title="Mastermind | Fezcodex"
description="Play the classic code-breaking game of Mastermind."
keywords={[
'Fezcodex',
'mastermind',
'bulls and cows',
'game',
'logic game',
]}
/>
<div className="mx-auto max-w-7xl px-6 py-24 md:px-12">
{/* Header Section */}
<header className="mb-24">
<Link
to="/apps"
className="group mb-12 inline-flex items-center gap-2 text-xs font-mono text-gray-500 hover:text-white transition-colors uppercase tracking-[0.3em]"
>
<ArrowLeftIcon
weight="bold"
className="transition-transform group-hover:-translate-x-1"
/>
<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="Mastermind"
slug="mm"
variant="brutalist"
/>
<p className="text-xl text-gray-400 max-w-2xl font-light leading-relaxed">
Heuristic optimization protocol. Decode the sequence via
iterative logical inference.
</p>
</div>
</div>
</header>
<div className="grid grid-cols-1 lg:grid-cols-12 gap-12">
{/* Main Action Column */}
<div className="lg:col-span-5 space-y-8">
<div className="relative border border-white/10 bg-white/[0.02] backdrop-blur-sm p-8 md:p-12 rounded-sm overflow-hidden group">
<div className="absolute inset-0 opacity-5 pointer-events-none">
<GenerativeArt seed={appName} 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" />
<h3 className="font-mono text-[10px] font-bold text-emerald-500 uppercase tracking-widest mb-12 flex items-center gap-2">
<TargetIcon weight="fill" />
Input_Module
</h3>
{!gameOver ? (
<form
onSubmit={handleGuessSubmit}
className="flex flex-col gap-8 relative z-10"
>
<div className="flex flex-col gap-4">
<label className="text-[10px] font-mono text-gray-500 uppercase tracking-widest">
Enter 4 Unique Digits
</label>
<input
type="text"
maxLength="4"
value={currentGuess}
onChange={(e) =>
setCurrentGuess(e.target.value.replace(/[^0-9]/g, ''))
}
className="bg-transparent border-b-2 border-white/10 py-4 text-5xl font-mono text-white focus:border-emerald-500 focus:outline-none transition-colors tracking-[0.5em] text-center"
placeholder="0000"
autoFocus
/>
</div>
<button
type="submit"
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"
>
<LightbulbIcon weight="bold" size={18} />
Validate
</button>
</form>
) : (
<div className="text-center py-8 relative z-10">
<p className="text-xl font-mono text-emerald-400 mb-8 leading-relaxed uppercase">
{message}
</p>
<button
onClick={handleResetGame}
className="w-full py-4 bg-emerald-500 text-black font-black uppercase tracking-[0.3em] hover:bg-white transition-all text-sm"
>
Re-Initialise
</button>
</div>
)}
</div>
<div className="border border-white/5 p-6 rounded-sm bg-black/40 font-mono text-[10px] uppercase tracking-widest text-gray-500 leading-relaxed">
<p>Bulls: Correct digit, correct position.</p>
<p className="mt-1">Cows: Correct digit, incorrect position.</p>
</div>
</div>
{/* History Column */}
<div className="lg:col-span-7 flex flex-col gap-6">
<h3 className="font-mono text-[10px] font-bold text-gray-500 uppercase tracking-widest flex items-center gap-2 px-2">
<ClockIcon weight="fill" className="text-emerald-500" />
Transmission_History
</h3>
<div className="flex-grow border border-white/10 bg-white/[0.01] rounded-sm overflow-hidden">
<div className="max-h-[600px] overflow-y-auto no-scrollbar">
<table className="w-full text-left font-mono">
<thead className="sticky top-0 bg-[#050505] z-10 border-b border-white/10">
<tr>
<th className="px-6 py-4 text-[10px] font-black uppercase text-gray-600 tracking-widest">
#
</th>
<th className="px-6 py-4 text-[10px] font-black uppercase text-gray-600 tracking-widest">
Sequence
</th>
<th className="px-6 py-4 text-[10px] font-black uppercase text-gray-600 tracking-widest text-center">
Bulls
</th>
<th className="px-6 py-4 text-[10px] font-black uppercase text-gray-600 tracking-widest text-center">
Cows
</th>
</tr>
</thead>
<tbody className="divide-y divide-white/5">
<AnimatePresence initial={false}>
{guesses.map((g, index) => (
<motion.tr
key={g.id}
initial={{ opacity: 0, x: 20 }}
animate={{ opacity: 1, x: 0 }}
className="group hover:bg-white/5 transition-colors"
>
<td className="px-6 py-4 text-gray-600 text-xs">
{guesses.length - index}
</td>
<td className="px-6 py-4 text-white font-black text-xl tracking-[0.2em]">
{g.guess}
</td>
<td className="px-6 py-4 text-center">
<span className="inline-flex items-center justify-center w-8 h-8 rounded-full bg-emerald-500/10 border border-emerald-500/20 text-emerald-400 font-black">
{g.bulls}
</span>
</td>
<td className="px-6 py-4 text-center">
<span className="inline-flex items-center justify-center w-8 h-8 rounded-full bg-amber-500/10 border border-amber-500/20 text-amber-400 font-black">
{g.cows}
</span>
</td>
</motion.tr>
))}
</AnimatePresence>
{guesses.length === 0 && (
<tr>
<td
colSpan="4"
className="px-6 py-20 text-center text-gray-700 uppercase tracking-widest text-xs"
>
No transmissions recorded.
</td>
</tr>
)}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
);
};
export default MastermindPage;