import React, { useState } from 'react'; const App = () => { // State for sequence of moves const [sequence, setSequence] = useState([]); // State for currently selected color const [selectedColorForInput, setSelectedColorForInput] = useState(''); // State for pending input waiting for color const [pendingInput, setPendingInput] = useState(null); // Define colors with their names and values const colorOptions = [ { name: 'Red', value: '#FF0000' }, { name: 'Orange', value: '#FFA500' }, { name: 'Yellow', value: '#FFFF00' }, { name: 'Green', value: '#00FF00' }, { name: 'Turquoise', value: '#40E0D0' }, { name: 'Blue', value: '#0000FF' }, { name: 'Purple', value: '#800080' }, { name: 'Pink', value: '#FFC0CB' } ]; // Handle direction button clicks const handleDirectionClick = (direction) => { if (direction === 'D') { // Add detour immediately without color selection setSequence([...sequence, { letter: 'D', color: '#FFFFFF' }]); } else { // Set pending input for L or R until color is selected setPendingInput(direction === 'left' ? 'L' : 'R'); } }; // Handle color selection const handleColorSelect = (color) => { if (pendingInput) { setSequence([...sequence, { letter: pendingInput, color }]); setPendingInput(null); setSelectedColorForInput(''); } }; // Render a group of 5 letters with its number const renderGroup = (group, groupIndex) => { return (
{groupIndex + 1}
{group.map((item, idx) => ( {item.letter} ))}
); }; // Split sequence into groups of 5 const groupedSequence = sequence.reduce((acc, item, index) => { const groupIndex = Math.floor(index / 5); if (!acc[groupIndex]) { acc[groupIndex] = []; } acc[groupIndex].push(item); return acc; }, []); return (
{/* Direction buttons */}
{/* Color selection */} {pendingInput && (
Select a color for {pendingInput}
{colorOptions.map((color) => (
)} {/* Sequence display */}
{groupedSequence.map((group, index) => renderGroup(group, index))}
); }; export default App;