import React, { useState, useEffect, useRef } from 'react'; import { createRoot } from 'react-dom/client'; // --- Data from the PDF --- const QUESTIONS = [ { q: "What does the Saffron colour in our National Flag stand for?", options: ["Peace and Truth", "Growth and Prosperity", "Strength and Courage", "Duty (Dharma)"], correct: "Strength and Courage" }, { q: "When did India adopt its Constitution (Republic Day)?", options: ["15 August 1947", "26 January 1950", "2 October 1869", "26 November 1949"], correct: "26 January 1950" }, { q: "What does the Ashoka Chakra in the center of the flag represent?", options: ["Wealth", "Duty (Dharma)", "Nature", "Victory"], correct: "Duty (Dharma)" }, { q: "Which colour in the flag stands for Growth and Prosperity?", options: ["Saffron", "White", "Green", "Blue"], correct: "Green" }, { q: "Where are the lions of the National Emblem found in the wild?", options: ["Sundarbans", "Jim Corbett", "Gir Forests (Gujarat)", "Kaziranga"], correct: "Gir Forests (Gujarat)" }, { q: "What is the National Animal of India?", options: ["Lion", "Elephant", "Tiger", "Leopard"], correct: "Tiger" }, { q: "What is the National Bird of India?", options: ["Parrot", "Eagle", "Peacock", "Swan"], correct: "Peacock" }, { q: "Whose image is found on Indian currency notes?", options: ["Jawaharlal Nehru", "Subhash Chandra Bose", "Mahatma Gandhi", "Sardar Patel"], correct: "Mahatma Gandhi" }, { q: "What symbol on the currency note promotes the 'Clean India Mission'?", options: ["A Broom", "Spectacles", "A Dustbin", "A Tree"], correct: "Spectacles" }, { q: "What is the world's tallest statue, located in India?", options: ["Statue of Liberty", "Spring Temple Buddha", "Statue of Unity", "Christ the Redeemer"], correct: "Statue of Unity" }, { q: "What is the 'book of rules' for our country called?", options: ["The Law Book", "The Constitution", "The Penal Code", "The Rule Book"], correct: "The Constitution" }, { q: "The National Emblem represents Strength, Courage, and...?", options: ["Confidence", "Peace", "Kindness", "Honesty"], correct: "Confidence" }, { q: "In which state do men traditionally wear a colorful 'Saafa' or 'Pagri'?", options: ["Kerala", "Rajasthan", "Assam", "Goa"], correct: "Rajasthan" }, { q: "India has the world's largest digital ID system known as...?", options: ["PAN Card", "Voter ID", "Aadhaar", "Passport"], correct: "Aadhaar" }, { q: "When is Independence Day celebrated in India?", options: ["26 January", "15 August", "2 October", "14 November"], correct: "15 August" } ]; // --- Components --- const Modal = ({ isOpen, question, onClose, onAnswer }) => { if (!isOpen || !question) return null; return (

Quiz Time!

{question.q}

{question.options.map((opt, idx) => ( ))}
); }; export default function App() { const [board, setBoard] = useState(Array(9).fill(null)); const [isXNext, setIsXNext] = useState(true); const [gameMode, setGameMode] = useState('single'); // 'single' or 'multi' const [winner, setWinner] = useState(null); const [winningLine, setWinningLine] = useState([]); const [activeQuestion, setActiveQuestion] = useState(null); const [pendingIndex, setPendingIndex] = useState(null); const [toast, setToast] = useState(null); const containerRef = useRef(null); useEffect(() => { checkWinner(board); }, [board]); useEffect(() => { if (gameMode === 'single' && !isXNext && !winner) { // Computer's turn const timer = setTimeout(() => { makeComputerMove(); }, 1000); return () => clearTimeout(timer); } }, [isXNext, gameMode, winner]); const checkWinner = (currentBoard) => { const lines = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], // Rows [0, 3, 6], [1, 4, 7], [2, 5, 8], // Cols [0, 4, 8], [2, 4, 6] // Diagonals ]; for (let i = 0; i < lines.length; i++) { const [a, b, c] = lines[i]; if (currentBoard[a] && currentBoard[a] === currentBoard[b] && currentBoard[a] === currentBoard[c]) { setWinner(currentBoard[a]); setWinningLine(lines[i]); return currentBoard[a]; } } if (!currentBoard.includes(null)) { setWinner('draw'); return 'draw'; } return null; }; const getRandomQuestion = () => { const randomIndex = Math.floor(Math.random() * QUESTIONS.length); return QUESTIONS[randomIndex]; }; const handleCellClick = (index) => { if (board[index] || winner || (gameMode === 'single' && !isXNext)) return; // Set pending move and open question modal setPendingIndex(index); setActiveQuestion(getRandomQuestion()); }; const handleAnswer = (answer) => { if (answer === activeQuestion.correct) { // Correct Answer const newBoard = [...board]; newBoard[pendingIndex] = isXNext ? 'X' : 'O'; setBoard(newBoard); setIsXNext(!isXNext); setToast({ msg: "Correct! Good Job!", type: "success" }); } else { // Incorrect Answer setIsXNext(!isXNext); // Turn passes setToast({ msg: "Incorrect! Turn passed.", type: "error" }); } setActiveQuestion(null); setPendingIndex(null); // Clear toast after 2s setTimeout(() => setToast(null), 2000); }; const makeComputerMove = () => { // Simple AI: 1. Win, 2. Block, 3. Random const emptyIndices = board.map((val, idx) => val === null ? idx : null).filter(val => val !== null); if (emptyIndices.length === 0) return; let moveIndex = -1; // Helper to check if a move results in a win for 'player' const checkPotentialWin = (player) => { const lines = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]]; for (let line of lines) { const [a, b, c] = line; const cells = [board[a], board[b], board[c]]; if (cells.filter(c => c === player).length === 2 && cells.includes(null)) { if (board[a] === null) return a; if (board[b] === null) return b; if (board[c] === null) return c; } } return -1; }; // 1. Try to win moveIndex = checkPotentialWin('O'); // 2. Block X if (moveIndex === -1) { moveIndex = checkPotentialWin('X'); } // 3. Random if (moveIndex === -1) { moveIndex = emptyIndices[Math.floor(Math.random() * emptyIndices.length)]; } const newBoard = [...board]; newBoard[moveIndex] = 'O'; setBoard(newBoard); setIsXNext(true); }; const resetGame = () => { setBoard(Array(9).fill(null)); setIsXNext(true); setWinner(null); setWinningLine([]); setToast(null); }; // Confetti Effect for Winner useEffect(() => { if (winner && winner !== 'draw' && containerRef.current) { const colors = ['#FF9933', '#FFFFFF', '#138808', '#000080']; // Saffron, White, Green, Blue for (let i = 0; i < 50; i++) { const confetti = document.createElement('div'); confetti.className = 'confetti'; confetti.style.left = Math.random() * 100 + '%'; // Relative to container confetti.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)]; confetti.style.animationDuration = Math.random() * 3 + 2 + 's'; containerRef.current.appendChild(confetti); // Cleanup individual confetti setTimeout(() => { if (confetti && confetti.parentNode) confetti.remove(); }, 5000); } } }, [winner]); return (
{/* Header */}
🇮🇳

Vibrant India

🇮🇳

Republic Day Edition • Tic-Tac-Toe

{/* Mode Selection */}
{/* Turn Indicator */} {!winner && (
Turn:
{isXNext ? 'Player X' : (gameMode === 'single' ? 'Computer (O)' : 'Player O')}
)} {/* Toast Notification */} {toast && (
{toast.msg}
)} {/* Board */}
{board.map((cell, idx) => { const isWinningCell = winningLine.includes(idx); return ( ); })}
{/* Winner Overlay */} {winner && (

{winner === 'draw' ? "It's a Draw!" : `${winner === 'X' ? 'Player X' : (gameMode === 'single' ? 'Computer' : 'Player O')} Wins!`}

{winner === 'X' ? "Great knowledge of India!" : winner === 'draw' ? "Well played both!" : "Better luck next time!"}

)}
{/* Footer Fact */}
Did you know? The Constitution of India is the longest written constitution in the world!
{/* Question Modal */} { setActiveQuestion(null); setPendingIndex(null); setIsXNext(!isXNext); }} onAnswer={handleAnswer} />
); }