File size: 6,038 Bytes
9580dce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";

import { useState, useRef, useEffect, useCallback } from "react";
import { motion } from "framer-motion";

interface SequenceBoardProps {
  sequence: (number | null)[];
  hiddenIndices: number[];
  hint: string;
  type: string;
  onSubmit: (answers: number[]) => void;
  disabled?: boolean;
}

const TYPE_LABELS: Record<string, string> = {
  arithmetic: "Arithmetic",
  geometric: "Geometric",
  fibonacci: "Fibonacci",
  triangular: "Triangular",
  square: "Square Numbers",
  prime: "Primes",
  quadratic: "Quadratic",
  alternating: "Alternating",
};

export function SequenceBoard({
  sequence,
  hiddenIndices,
  hint,
  type,
  onSubmit,
  disabled = false,
}: SequenceBoardProps) {
  const [answers, setAnswers] = useState<Record<number, string>>({});
  const [showHint, setShowHint] = useState(false);
  const inputRefs = useRef<Record<number, HTMLInputElement | null>>({});

  // Focus first input on mount
  useEffect(() => {
    const firstIdx = hiddenIndices[0];
    if (firstIdx !== undefined) {
      inputRefs.current[firstIdx]?.focus();
    }
  }, [hiddenIndices]);

  const handleChange = useCallback(
    (index: number, value: string) => {
      // Allow negative numbers and digits only
      if (value !== "" && value !== "-" && !/^-?\d+$/.test(value)) return;
      setAnswers((prev) => ({ ...prev, [index]: value }));
    },
    [],
  );

  const handleKeyDown = useCallback(
    (e: React.KeyboardEvent, index: number) => {
      if (e.key === "Enter" || e.key === "Tab") {
        e.preventDefault();
        const currentPos = hiddenIndices.indexOf(index);
        const nextIdx = hiddenIndices[currentPos + 1];
        if (nextIdx !== undefined) {
          inputRefs.current[nextIdx]?.focus();
        }
      }
    },
    [hiddenIndices],
  );

  const handleSubmit = useCallback(() => {
    const playerAnswers = hiddenIndices.map((idx) => {
      const val = answers[idx];
      return val ? parseInt(val, 10) : NaN;
    });

    if (playerAnswers.some((v) => isNaN(v))) return;
    onSubmit(playerAnswers);
  }, [answers, hiddenIndices, onSubmit]);

  const allFilled = hiddenIndices.every((idx) => {
    const val = answers[idx];
    return val !== undefined && val !== "" && val !== "-" && !isNaN(parseInt(val, 10));
  });

  return (
    <div className="space-y-6">
      {/* Sequence type badge */}
      <div className="flex items-center justify-between">
        <span className="text-xs font-mono uppercase tracking-wider text-violet-400/70">
          {TYPE_LABELS[type] || type} Sequence
        </span>
        <button
          onClick={() => setShowHint((h) => !h)}
          className="text-xs text-violet-400/50 hover:text-violet-400 transition-colors"
          disabled={disabled}
        >
          {showHint ? "Hide Hint" : "💡 Hint"}
        </button>
      </div>

      {/* Hint */}
      {showHint && (
        <motion.div
          initial={{ opacity: 0, height: 0 }}
          animate={{ opacity: 1, height: "auto" }}
          className="text-sm text-violet-300/80 bg-violet-500/10 rounded-lg px-4 py-2 border border-violet-500/20"
        >
          {hint}
        </motion.div>
      )}

      {/* Sequence display */}
      <div className="flex flex-wrap items-center justify-center gap-2">
        {sequence.map((val, idx) => {
          const isHidden = hiddenIndices.includes(idx);
          const isLast = idx === sequence.length - 1;

          return (
            <div key={idx} className="flex items-center gap-2">
              {isHidden ? (
                <motion.div
                  initial={{ scale: 0.8, opacity: 0 }}
                  animate={{ scale: 1, opacity: 1 }}
                  transition={{ delay: idx * 0.05 }}
                >
                  <input
                    ref={(el) => {
                      inputRefs.current[idx] = el;
                    }}
                    type="text"
                    inputMode="numeric"
                    value={answers[idx] ?? ""}
                    onChange={(e) => handleChange(idx, e.target.value)}
                    onKeyDown={(e) => handleKeyDown(e, idx)}
                    disabled={disabled}
                    className="w-16 h-12 text-center text-lg font-mono font-bold
                      bg-violet-500/20 border-2 border-violet-400/40 rounded-lg
                      text-white placeholder-violet-400/30
                      focus:border-violet-400 focus:ring-2 focus:ring-violet-400/20
                      focus:outline-none transition-all
                      disabled:opacity-50 disabled:cursor-not-allowed"
                    placeholder="?"
                  />
                </motion.div>
              ) : (
                <motion.div
                  initial={{ scale: 0.8, opacity: 0 }}
                  animate={{ scale: 1, opacity: 1 }}
                  transition={{ delay: idx * 0.05 }}
                  className="w-12 h-12 flex items-center justify-center
                    text-lg font-mono font-semibold text-white/90
                    bg-white/5 rounded-lg border border-white/10"
                >
                  {val}
                </motion.div>
              )}

              {/* Separator between terms (except last) */}
              {!isLast && (
                <span className="text-white/20 font-mono text-lg">,</span>
              )}
            </div>
          );
        })}

        {/* Ellipsis to suggest continuation */}
        <span className="text-white/30 font-mono text-lg ml-1">…</span>
      </div>

      {/* Submit */}
      <div className="flex justify-center">
        <button
          onClick={handleSubmit}
          disabled={disabled || !allFilled}
          className="px-6 py-2 rounded-lg font-medium transition-all
            bg-violet-600 hover:bg-violet-500 text-white
            disabled:opacity-40 disabled:cursor-not-allowed
            active:scale-95"
        >
          Check Answer{hiddenIndices.length > 1 ? "s" : ""}
        </button>
      </div>
    </div>
  );
}