Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import React, { useState
|
| 2 |
|
| 3 |
// 成語資料庫
|
| 4 |
const idiomDatabase = [
|
|
@@ -6,65 +6,31 @@ const idiomDatabase = [
|
|
| 6 |
{ mainIdiom: '一葉知秋', synonyms: ['見微知著', '月暈而風'], meaning: '從小徵兆可以預見未來的發展' },
|
| 7 |
{ mainIdiom: '一丘之貉', synonyms: ['沆瀣一氣', '狐群狗黨'], meaning: '形容品性相同的壞人' },
|
| 8 |
{ mainIdiom: '一蹴可幾', synonyms: ['一步登天', '唾手可得'], meaning: '一下子就能達到目的' },
|
| 9 |
-
{ mainIdiom: '一暴十寒', synonyms: ['兩天打魚,三天曬網', '虎頭蛇尾'], meaning: '形容學習或做事不能持之以恆' }
|
| 10 |
-
{ mainIdiom: '環堵蕭然', synonyms: ['一貧如洗', '家徒四壁'], meaning: '形容生活很貧困' },
|
| 11 |
-
{ mainIdiom: '一字千金', synonyms: ['字字珠璣', '文不加點'], meaning: '形容文章寫得好,文字珍貴' },
|
| 12 |
-
{ mainIdiom: '一擲千金', synonyms: ['揮金如土', '揮霍無度'], meaning: '形容揮霍錢財' },
|
| 13 |
-
{ mainIdiom: '生靈塗炭', synonyms: ['民不聊生', '民生凋敝'], meaning: '形容人民生活困苦' },
|
| 14 |
-
{ mainIdiom: '瓜熟蒂落', synonyms: ['水到渠成', '順理成章'], meaning: '形容事情自然成功' }
|
| 15 |
];
|
| 16 |
|
| 17 |
function ChineseIdiomGame() {
|
| 18 |
-
|
| 19 |
-
const [
|
| 20 |
const [selectedAnswers, setSelectedAnswers] = useState([]);
|
| 21 |
const [score, setScore] = useState(0);
|
| 22 |
-
const [message, setMessage] = useState('');
|
| 23 |
const [gameComplete, setGameComplete] = useState(false);
|
| 24 |
-
const [usedQuestions, setUsedQuestions] = useState([]);
|
| 25 |
|
| 26 |
-
//
|
| 27 |
-
const
|
| 28 |
-
return [...array].sort(() => Math.random() - 0.5);
|
| 29 |
-
};
|
| 30 |
-
|
| 31 |
-
// 開始新的題目
|
| 32 |
-
const startNewQuestion = () => {
|
| 33 |
-
// 過濾掉已使用的題目
|
| 34 |
-
const availableQuestions = idiomDatabase.filter(
|
| 35 |
-
q => !usedQuestions.includes(q.mainIdiom)
|
| 36 |
-
);
|
| 37 |
-
|
| 38 |
-
// 如果所有題目都用完了,重置使用記錄
|
| 39 |
-
if (availableQuestions.length === 0) {
|
| 40 |
-
setUsedQuestions([]);
|
| 41 |
-
return startNewQuestion();
|
| 42 |
-
}
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
// 生成選項
|
| 48 |
const otherOptions = idiomDatabase
|
| 49 |
-
.filter(
|
| 50 |
.flatMap(item => item.synonyms)
|
| 51 |
-
.
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
setCurrentQuestion(newQuestion);
|
| 57 |
-
setOptions(allOptions);
|
| 58 |
-
setSelectedAnswers([]);
|
| 59 |
-
setMessage('');
|
| 60 |
-
setUsedQuestions([...usedQuestions, newQuestion.mainIdiom]);
|
| 61 |
};
|
| 62 |
|
| 63 |
-
// 初始化遊戲
|
| 64 |
-
useEffect(() => {
|
| 65 |
-
startNewQuestion();
|
| 66 |
-
}, []);
|
| 67 |
-
|
| 68 |
// 處理選項點擊
|
| 69 |
const handleOptionSelect = (option) => {
|
| 70 |
if (selectedAnswers.includes(option)) return;
|
|
@@ -74,13 +40,16 @@ function ChineseIdiomGame() {
|
|
| 74 |
setSelectedAnswers(newSelected);
|
| 75 |
|
| 76 |
if (newSelected.length === 2) {
|
| 77 |
-
const newScore = score +
|
| 78 |
setScore(newScore);
|
| 79 |
|
| 80 |
if (newScore >= 100) {
|
| 81 |
setGameComplete(true);
|
| 82 |
} else {
|
| 83 |
-
setTimeout(
|
|
|
|
|
|
|
|
|
|
| 84 |
}
|
| 85 |
}
|
| 86 |
}
|
|
@@ -90,71 +59,120 @@ function ChineseIdiomGame() {
|
|
| 90 |
const restartGame = () => {
|
| 91 |
setScore(0);
|
| 92 |
setGameComplete(false);
|
| 93 |
-
|
| 94 |
-
|
| 95 |
};
|
| 96 |
|
| 97 |
if (gameComplete) {
|
| 98 |
return (
|
| 99 |
-
<div
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
</div>
|
| 112 |
);
|
| 113 |
}
|
| 114 |
|
| 115 |
return (
|
| 116 |
-
<div
|
| 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 |
</div>
|
|
|
|
|
|
|
|
|
|
| 158 |
</div>
|
| 159 |
);
|
| 160 |
}
|
|
|
|
| 1 |
+
import React, { useState } from 'react';
|
| 2 |
|
| 3 |
// 成語資料庫
|
| 4 |
const idiomDatabase = [
|
|
|
|
| 6 |
{ mainIdiom: '一葉知秋', synonyms: ['見微知著', '月暈而風'], meaning: '從小徵兆可以預見未來的發展' },
|
| 7 |
{ mainIdiom: '一丘之貉', synonyms: ['沆瀣一氣', '狐群狗黨'], meaning: '形容品性相同的壞人' },
|
| 8 |
{ mainIdiom: '一蹴可幾', synonyms: ['一步登天', '唾手可得'], meaning: '一下子就能達到目的' },
|
| 9 |
+
{ mainIdiom: '一暴十寒', synonyms: ['兩天打魚,三天曬網', '虎頭蛇尾'], meaning: '形容學習或做事不能持之以恆' }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
];
|
| 11 |
|
| 12 |
function ChineseIdiomGame() {
|
| 13 |
+
// 狀態管理
|
| 14 |
+
const [currentQuestionIndex, setCurrentQuestionIndex] = useState(0);
|
| 15 |
const [selectedAnswers, setSelectedAnswers] = useState([]);
|
| 16 |
const [score, setScore] = useState(0);
|
|
|
|
| 17 |
const [gameComplete, setGameComplete] = useState(false);
|
|
|
|
| 18 |
|
| 19 |
+
// 當前題目
|
| 20 |
+
const currentQuestion = idiomDatabase[currentQuestionIndex];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
// 生成選項
|
| 23 |
+
const generateOptions = () => {
|
| 24 |
+
const correctAnswers = currentQuestion.synonyms;
|
|
|
|
| 25 |
const otherOptions = idiomDatabase
|
| 26 |
+
.filter((_, index) => index !== currentQuestionIndex)
|
| 27 |
.flatMap(item => item.synonyms)
|
| 28 |
+
.slice(0, 8);
|
| 29 |
|
| 30 |
+
return [...correctAnswers, ...otherOptions]
|
| 31 |
+
.sort(() => Math.random() - 0.5);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
};
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
// 處理選項點擊
|
| 35 |
const handleOptionSelect = (option) => {
|
| 36 |
if (selectedAnswers.includes(option)) return;
|
|
|
|
| 40 |
setSelectedAnswers(newSelected);
|
| 41 |
|
| 42 |
if (newSelected.length === 2) {
|
| 43 |
+
const newScore = score + 20;
|
| 44 |
setScore(newScore);
|
| 45 |
|
| 46 |
if (newScore >= 100) {
|
| 47 |
setGameComplete(true);
|
| 48 |
} else {
|
| 49 |
+
setTimeout(() => {
|
| 50 |
+
setCurrentQuestionIndex((prev) => (prev + 1) % idiomDatabase.length);
|
| 51 |
+
setSelectedAnswers([]);
|
| 52 |
+
}, 1000);
|
| 53 |
}
|
| 54 |
}
|
| 55 |
}
|
|
|
|
| 59 |
const restartGame = () => {
|
| 60 |
setScore(0);
|
| 61 |
setGameComplete(false);
|
| 62 |
+
setCurrentQuestionIndex(0);
|
| 63 |
+
setSelectedAnswers([]);
|
| 64 |
};
|
| 65 |
|
| 66 |
if (gameComplete) {
|
| 67 |
return (
|
| 68 |
+
<div style={{
|
| 69 |
+
padding: '20px',
|
| 70 |
+
textAlign: 'center',
|
| 71 |
+
maxWidth: '600px',
|
| 72 |
+
margin: '0 auto'
|
| 73 |
+
}}>
|
| 74 |
+
<h2 style={{
|
| 75 |
+
fontSize: '24px',
|
| 76 |
+
color: '#16a34a',
|
| 77 |
+
marginBottom: '20px'
|
| 78 |
+
}}>
|
| 79 |
+
太棒了!你是成語小天才!
|
| 80 |
+
</h2>
|
| 81 |
+
<button
|
| 82 |
+
onClick={restartGame}
|
| 83 |
+
style={{
|
| 84 |
+
backgroundColor: '#16a34a',
|
| 85 |
+
color: 'white',
|
| 86 |
+
padding: '10px 20px',
|
| 87 |
+
borderRadius: '8px',
|
| 88 |
+
border: 'none',
|
| 89 |
+
cursor: 'pointer'
|
| 90 |
+
}}
|
| 91 |
+
>
|
| 92 |
+
再玩一次
|
| 93 |
+
</button>
|
| 94 |
</div>
|
| 95 |
);
|
| 96 |
}
|
| 97 |
|
| 98 |
return (
|
| 99 |
+
<div style={{
|
| 100 |
+
padding: '20px',
|
| 101 |
+
maxWidth: '800px',
|
| 102 |
+
margin: '0 auto'
|
| 103 |
+
}}>
|
| 104 |
+
<h1 style={{
|
| 105 |
+
fontSize: '24px',
|
| 106 |
+
textAlign: 'center',
|
| 107 |
+
marginBottom: '20px'
|
| 108 |
+
}}>
|
| 109 |
+
成語對對碰
|
| 110 |
+
</h1>
|
| 111 |
+
|
| 112 |
+
<div style={{
|
| 113 |
+
backgroundColor: '#f0f9ff',
|
| 114 |
+
padding: '20px',
|
| 115 |
+
borderRadius: '8px',
|
| 116 |
+
marginBottom: '20px'
|
| 117 |
+
}}>
|
| 118 |
+
<h2 style={{
|
| 119 |
+
fontSize: '20px',
|
| 120 |
+
textAlign: 'center',
|
| 121 |
+
marginBottom: '10px'
|
| 122 |
+
}}>
|
| 123 |
+
{currentQuestion.mainIdiom}
|
| 124 |
+
</h2>
|
| 125 |
+
<p style={{
|
| 126 |
+
textAlign: 'center',
|
| 127 |
+
color: '#4b5563'
|
| 128 |
+
}}>
|
| 129 |
+
{currentQuestion.meaning}
|
| 130 |
+
</p>
|
| 131 |
+
</div>
|
| 132 |
|
| 133 |
+
<div style={{
|
| 134 |
+
display: 'grid',
|
| 135 |
+
gridTemplateColumns: 'repeat(auto-fill, minmax(150px, 1fr))',
|
| 136 |
+
gap: '10px',
|
| 137 |
+
marginBottom: '20px'
|
| 138 |
+
}}>
|
| 139 |
+
{generateOptions().map((option, index) => (
|
| 140 |
+
<button
|
| 141 |
+
key={index}
|
| 142 |
+
onClick={() => handleOptionSelect(option)}
|
| 143 |
+
style={{
|
| 144 |
+
padding: '10px',
|
| 145 |
+
borderRadius: '8px',
|
| 146 |
+
border: '1px solid #e5e7eb',
|
| 147 |
+
backgroundColor: selectedAnswers.includes(option) ? '#16a34a' : 'white',
|
| 148 |
+
color: selectedAnswers.includes(option) ? 'white' : 'black',
|
| 149 |
+
cursor: 'pointer'
|
| 150 |
+
}}
|
| 151 |
+
disabled={selectedAnswers.includes(option)}
|
| 152 |
+
>
|
| 153 |
+
{option}
|
| 154 |
+
</button>
|
| 155 |
+
))}
|
| 156 |
+
</div>
|
| 157 |
|
| 158 |
+
<div style={{
|
| 159 |
+
backgroundColor: '#e5e7eb',
|
| 160 |
+
borderRadius: '9999px',
|
| 161 |
+
height: '20px',
|
| 162 |
+
overflow: 'hidden'
|
| 163 |
+
}}>
|
| 164 |
+
<div
|
| 165 |
+
style={{
|
| 166 |
+
backgroundColor: '#16a34a',
|
| 167 |
+
height: '100%',
|
| 168 |
+
width: `${score}%`,
|
| 169 |
+
transition: 'width 0.5s ease-in-out'
|
| 170 |
+
}}
|
| 171 |
+
/>
|
| 172 |
</div>
|
| 173 |
+
<p style={{ textAlign: 'center', marginTop: '10px' }}>
|
| 174 |
+
分數:{score}/100
|
| 175 |
+
</p>
|
| 176 |
</div>
|
| 177 |
);
|
| 178 |
}
|