Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234565 unmodified lines12131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201import React, { useState, useEffect } from 'react';// 完整的成語資料庫(50組,之後擴充到200組)const idiomDatabase = [{ mainIdiom: '急如星火', synonyms: ['迫在眉睫', '刻不容緩'], meaning: '形容事情非常緊急' },{ mainIdiom: '一葉知秋', synonyms: ['見微知著', '月暈而風'], meaning: '從小徵兆可以預見未來的發展' },5 unmodified lines{ mainIdiom: '一擲千金', synonyms: ['揮金如土', '揮霍無度'], meaning: '形容揮霍錢財' },{ mainIdiom: '生靈塗炭', synonyms: ['民不聊生', '民生凋敝'], meaning: '形容人民生活困苦' },{ mainIdiom: '瓜熟蒂落', synonyms: ['水到渠成', '順理成章'], meaning: '形容事情自然成功' }].map((item, index) => ({ ...item, id: index + 1 }));export default function ChineseIdiomGame() {const [currentQuestion, setCurrentQuestion] = useState(null);const [options, setOptions] = useState([]);const [selectedAnswers, setSelectedAnswers] = useState([]);const [score, setScore] = useState(0);const [message, setMessage] = useState('');const [errorMessage, setErrorMessage] = useState('');const [gameComplete, setGameComplete] = useState(false);const [incorrectOption, setIncorrectOption] = useState(null);const [availableQuestions, setAvailableQuestions] = useState([...idiomDatabase]);const shuffleArray = (array) => {return [...array].sort(() => Math.random() - 0.5);};const getRandomOptions = (currentQ, count) => {const otherOptions = idiomDatabase.filter(item => item.id !== currentQ.id).flatMap(item => item.synonyms).filter(opt => !currentQ.synonyms.includes(opt));return shuffleArray(otherOptions).slice(0, count);};const startNewQuestion = () => {if (availableQuestions.length === 0) {setAvailableQuestions([...idiomDatabase]);}const randomIndex = Math.floor(Math.random() * availableQuestions.length);const newQuestion = availableQuestions[randomIndex];setAvailableQuestions(prev => prev.filter(q => q.id !== newQuestion.id));const allOptions = [...newQuestion.synonyms,...getRandomOptions(newQuestion, 8)].sort(() => Math.random() - 0.5);setCurrentQuestion(newQuestion);setOptions(allOptions);setSelectedAnswers([]);setMessage('');setErrorMessage('');};const initializeGame = () => {setScore(0);setGameComplete(false);setAvailableQuestions([...idiomDatabase]);startNewQuestion();};useEffect(() => {initializeGame();}, []);const handleOptionSelect = (option) => {if (selectedAnswers.includes(option)) return;if (currentQuestion.synonyms.includes(option)) {const newSelected = [...selectedAnswers, option];setSelectedAnswers(newSelected);setErrorMessage('');if (newSelected.length === 1) {setScore(prev => prev + 5);setMessage('答對了!還有一個答案');} else if (newSelected.length === 2) {const newScore = score + 5;setScore(newScore);if (newScore >= 100) {setGameComplete(true);} else {setMessage('太棒了!準備下一題...');setTimeout(startNewQuestion, 1500);}}} else {setIncorrectOption(option);setErrorMessage('答錯了,再試試看!');setTimeout(() => {setIncorrectOption(null);setErrorMessage('');}, 1000);}};if (gameComplete) {return (<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center"><div className="bg-white rounded-lg p-8 max-w-xl w-full mx-4 flex flex-col items-center"><h2 className="text-3xl font-bold text-green-600 mb-12">太棒了!你是成語小天才!</h2><div className="flex flex-col sm:flex-row gap-8 w-full max-w-md"><buttononClick={initializeGame}className="bg-[#72BFA3] text-white text-xl font-bold px-8 py-4 rounded-lg hover:bg-green-600 transition-colors w-full">繼續挑戰</button><buttononClick={() => window.close()}className="bg-[#FFC54F] text-white text-xl font-bold px-8 py-4 rounded-lg hover:bg-yellow-600 transition-colors w-full">休息一下</button></div></div></div>);}return (<div className="min-h-screen bg-[#F2F1EF] p-4 flex justify-center"><div className="w-full max-w-3xl"><div className="bg-white rounded-lg shadow-lg p-4 sm:p-8"><h1 className="text-2xl sm:text-3xl font-bold text-center mb-6">成語對對碰</h1><div className="text-center mb-8"><div className="flex flex-wrap justify-center items-center gap-4 mb-3"><h2 className="text-xl sm:text-2xl font-bold text-blue-600">題目:{currentQuestion?.mainIdiom}</h2>{selectedAnswers.map((idiom, index) => (<span key={index} className="text-xl sm:text-2xl font-bold text-green-600">= {idiom}</span>))}</div><p className="text-gray-600 mb-4">{currentQuestion?.meaning}</p><p className="text-gray-700 bg-gray-100 p-4 rounded-lg">玩法:這裡有兩個成語是和題目一樣的意思,請你把它找出來!</p></div>{message && (<div className="text-center mb-6 text-green-600 font-bold">{message}</div>)}{errorMessage && (<div className="text-center mb-4 text-red-500 font-bold">{errorMessage}</div>)}<div className="grid grid-cols-2 md:grid-cols-5 gap-2 sm:gap-4 mb-6">{options.map((option, index) => (<buttonkey={`${option}-${index}`}onClick={() => handleOptionSelect(option)}className={`p-2 sm:p-4 rounded-lg text-center transition-all${selectedAnswers.includes(option) ? 'bg-[#72BFA3] text-white' : ''}${incorrectOption === option ? 'bg-[#FFC54F]' : 'bg-white'}border-2 border-gray-200hover:bg-gray-50text-sm sm:text-base`}disabled={selectedAnswers.includes(option)}>{option}</button>))}</div><div className="mt-4 sm:mt-8"><div className="w-full bg-gray-200 rounded-full h-6"><divclassName="bg-green-500 h-full rounded-full transition-all duration-500 flex items-center justify-end pr-2"style={{ width: `${score}%` }}><span className="text-white text-sm font-bold">{score}/100</span></div></div></div></div></div></div>);}1234565 unmodified lines12131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162import React, { useState, useEffect } from 'react';// 成語資料庫const idiomDatabase = [{ mainIdiom: '急如星火', synonyms: ['迫在眉睫', '刻不容緩'], meaning: '形容事情非常緊急' },{ mainIdiom: '一葉知秋', synonyms: ['見微知著', '月暈而風'], meaning: '從小徵兆可以預見未來的發展' },5 unmodified lines{ mainIdiom: '一擲千金', synonyms: ['揮金如土', '揮霍無度'], meaning: '形容揮霍錢財' },{ mainIdiom: '生靈塗炭', synonyms: ['民不聊生', '民生凋敝'], meaning: '形容人民生活困苦' },{ mainIdiom: '瓜熟蒂落', synonyms: ['水到渠成', '順理成章'], meaning: '形容事情自然成功' }];function ChineseIdiomGame() {const [currentQuestion, setCurrentQuestion] = useState(null);const [options, setOptions] = useState([]);const [selectedAnswers, setSelectedAnswers] = useState([]);const [score, setScore] = useState(0);const [message, setMessage] = useState('');const [gameComplete, setGameComplete] = useState(false);const [usedQuestions, setUsedQuestions] = useState([]);// 打亂陣列順序的函數const shuffleArray = (array) => {return [...array].sort(() => Math.random() - 0.5);};// 開始新的題目const startNewQuestion = () => {// 過濾掉已使用的題目const availableQuestions = idiomDatabase.filter(q => !usedQuestions.includes(q.mainIdiom));// 如果所有題目都用完了,重置使用記錄if (availableQuestions.length === 0) {setUsedQuestions([]);return startNewQuestion();}// 隨機選擇一個新題目const newQuestion = availableQuestions[Math.floor(Math.random() * availableQuestions.length)];// 生成選項const otherOptions = idiomDatabase.filter(item => item.mainIdiom !== newQuestion.mainIdiom).flatMap(item => item.synonyms).filter(opt => !newQuestion.synonyms.includes(opt));const shuffledOtherOptions = shuffleArray(otherOptions).slice(0, 8);const allOptions = shuffleArray([...newQuestion.synonyms, ...shuffledOtherOptions]);setCurrentQuestion(newQuestion);setOptions(allOptions);setSelectedAnswers([]);setMessage('');setUsedQuestions([...usedQuestions, newQuestion.mainIdiom]);};// 初始化遊戲useEffect(() => {startNewQuestion();}, []);// 處理選項點擊const handleOptionSelect = (option) => {if (selectedAnswers.includes(option)) return;if (currentQuestion.synonyms.includes(option)) {const newSelected = [...selectedAnswers, option];setSelectedAnswers(newSelected);if (newSelected.length === 2) {const newScore = score + 10;setScore(newScore);if (newScore >= 100) {setGameComplete(true);} else {setTimeout(startNewQuestion, 1000);}}}};// 重新開始遊戲const restartGame = () => {setScore(0);setGameComplete(false);setUsedQuestions([]);startNewQuestion();};if (gameComplete) {return (<div className="min-h-screen bg-gray-100 flex items-center justify-center p-4"><div className="bg-white rounded-lg p-8 text-center max-w-md w-full"><h2 className="text-2xl font-bold text-green-600 mb-6">太棒了!你是成語小天才!</h2><buttononClick={restartGame}className="bg-green-500 text-white px-6 py-2 rounded-lg hover:bg-green-600">再玩一次</button></div></div>);}return (<div className="min-h-screen bg-gray-100 p-4"><div className="max-w-2xl mx-auto bg-white rounded-lg shadow p-6"><h1 className="text-2xl font-bold text-center mb-6">成語對對碰</h1>{currentQuestion && (<><div className="bg-blue-100 p-4 rounded-lg mb-6"><h2 className="text-xl font-bold text-center mb-2">{currentQuestion.mainIdiom}</h2><p className="text-gray-600 text-center">{currentQuestion.meaning}</p></div><div className="grid grid-cols-2 sm:grid-cols-3 gap-4 mb-6">{options.map((option, index) => (<buttonkey={index}onClick={() => handleOptionSelect(option)}className={`p-3 rounded-lg text-center${selectedAnswers.includes(option) ? 'bg-green-500 text-white' : 'bg-gray-100'}hover:bg-gray-200`}disabled={selectedAnswers.includes(option)}>{option}</button>))}</div><div className="bg-gray-200 rounded-full h-4 mb-4"><divclassName="bg-green-500 h-full rounded-full transition-all duration-500"style={{ width: `${score}%` }}/></div><p className="text-center text-gray-600">分數:{score}/100</p></>)}</div></div>);}export default ChineseIdiomGame;
app.py+86-125