Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,182 +1,93 @@
|
|
| 1 |
-
import
|
| 2 |
|
| 3 |
-
|
| 4 |
-
return <ChineseIdiomGame />;
|
| 5 |
-
}
|
| 6 |
-
|
| 7 |
-
// 成語資料庫
|
| 8 |
const idiomDatabase = [
|
| 9 |
{ mainIdiom: '急如星火', synonyms: ['迫在眉睫', '刻不容緩'], meaning: '形容事情非常緊急' },
|
| 10 |
{ mainIdiom: '一葉知秋', synonyms: ['見微知著', '月暈而風'], meaning: '從小徵兆可以預見未來的發展' },
|
| 11 |
-
{ mainIdiom: '一丘之貉', synonyms: ['沆瀣一氣', '狐群狗黨'], meaning: '形容品性相同的壞人' }
|
| 12 |
-
{ mainIdiom: '一蹴可幾', synonyms: ['一步登天', '唾手可得'], meaning: '一下子就能達到目的' },
|
| 13 |
-
{ mainIdiom: '一暴十寒', synonyms: ['兩天打魚,三天曬網', '虎頭蛇尾'], meaning: '形容學習或做事不能持之以恆' }
|
| 14 |
];
|
| 15 |
|
| 16 |
function ChineseIdiomGame() {
|
| 17 |
-
//
|
| 18 |
-
const [
|
| 19 |
-
const [selectedAnswers, setSelectedAnswers] = useState([]);
|
| 20 |
const [score, setScore] = useState(0);
|
| 21 |
-
const [
|
| 22 |
|
| 23 |
// 當前題目
|
| 24 |
-
const currentQuestion = idiomDatabase[
|
| 25 |
|
| 26 |
-
//
|
| 27 |
-
const
|
| 28 |
-
|
| 29 |
-
const otherOptions = idiomDatabase
|
| 30 |
-
.filter((_, index) => index !== currentQuestionIndex)
|
| 31 |
-
.flatMap(item => item.synonyms)
|
| 32 |
-
.slice(0, 8);
|
| 33 |
-
|
| 34 |
-
return [...correctAnswers, ...otherOptions]
|
| 35 |
-
.sort(() => Math.random() - 0.5);
|
| 36 |
-
};
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
if (selectedAnswers.includes(option)) return;
|
| 41 |
-
|
| 42 |
-
if (currentQuestion.synonyms.includes(option)) {
|
| 43 |
-
const newSelected = [...selectedAnswers, option];
|
| 44 |
setSelectedAnswers(newSelected);
|
| 45 |
|
| 46 |
if (newSelected.length === 2) {
|
| 47 |
-
const newScore = score +
|
| 48 |
setScore(newScore);
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
setSelectedAnswers([]);
|
| 56 |
-
}, 1000);
|
| 57 |
-
}
|
| 58 |
}
|
| 59 |
}
|
| 60 |
};
|
| 61 |
|
| 62 |
-
//
|
| 63 |
-
const
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
if (gameComplete) {
|
| 71 |
-
return (
|
| 72 |
-
<div style={{
|
| 73 |
-
padding: '20px',
|
| 74 |
-
textAlign: 'center',
|
| 75 |
-
maxWidth: '600px',
|
| 76 |
-
margin: '0 auto'
|
| 77 |
-
}}>
|
| 78 |
-
<h2 style={{
|
| 79 |
-
fontSize: '24px',
|
| 80 |
-
color: '#16a34a',
|
| 81 |
-
marginBottom: '20px'
|
| 82 |
-
}}>
|
| 83 |
-
太棒了!你是成語小天才!
|
| 84 |
-
</h2>
|
| 85 |
-
<button
|
| 86 |
-
onClick={restartGame}
|
| 87 |
-
style={{
|
| 88 |
-
backgroundColor: '#16a34a',
|
| 89 |
-
color: 'white',
|
| 90 |
-
padding: '10px 20px',
|
| 91 |
-
borderRadius: '8px',
|
| 92 |
-
border: 'none',
|
| 93 |
-
cursor: 'pointer'
|
| 94 |
-
}}
|
| 95 |
-
>
|
| 96 |
-
再玩一次
|
| 97 |
-
</button>
|
| 98 |
-
</div>
|
| 99 |
-
);
|
| 100 |
-
}
|
| 101 |
|
| 102 |
return (
|
| 103 |
-
<div style={{
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
<h1 style={{
|
| 109 |
-
fontSize: '24px',
|
| 110 |
-
textAlign: 'center',
|
| 111 |
-
marginBottom: '20px'
|
| 112 |
-
}}>
|
| 113 |
-
成語對對碰
|
| 114 |
-
</h1>
|
| 115 |
-
|
| 116 |
-
<div style={{
|
| 117 |
-
backgroundColor: '#f0f9ff',
|
| 118 |
padding: '20px',
|
| 119 |
borderRadius: '8px',
|
| 120 |
marginBottom: '20px'
|
| 121 |
}}>
|
| 122 |
-
<h2 style={{
|
| 123 |
-
|
| 124 |
-
textAlign: 'center',
|
| 125 |
-
marginBottom: '10px'
|
| 126 |
-
}}>
|
| 127 |
-
{currentQuestion.mainIdiom}
|
| 128 |
-
</h2>
|
| 129 |
-
<p style={{
|
| 130 |
-
textAlign: 'center',
|
| 131 |
-
color: '#4b5563'
|
| 132 |
-
}}>
|
| 133 |
-
{currentQuestion.meaning}
|
| 134 |
-
</p>
|
| 135 |
</div>
|
| 136 |
|
| 137 |
-
<div style={{
|
| 138 |
-
display: 'grid',
|
| 139 |
-
gridTemplateColumns: 'repeat(
|
| 140 |
-
gap: '10px'
|
| 141 |
-
marginBottom: '20px'
|
| 142 |
}}>
|
| 143 |
-
{
|
| 144 |
<button
|
| 145 |
key={index}
|
| 146 |
-
onClick={() =>
|
| 147 |
style={{
|
| 148 |
padding: '10px',
|
| 149 |
-
|
| 150 |
-
border: '1px solid #e5e7eb',
|
| 151 |
-
backgroundColor: selectedAnswers.includes(option) ? '#16a34a' : 'white',
|
| 152 |
color: selectedAnswers.includes(option) ? 'white' : 'black',
|
| 153 |
-
|
|
|
|
| 154 |
}}
|
| 155 |
-
disabled={selectedAnswers.includes(option)}
|
| 156 |
>
|
| 157 |
{option}
|
| 158 |
</button>
|
| 159 |
))}
|
| 160 |
</div>
|
| 161 |
|
| 162 |
-
<div style={{
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
height: '20px',
|
| 166 |
-
overflow: 'hidden'
|
| 167 |
}}>
|
| 168 |
-
|
| 169 |
-
style={{
|
| 170 |
-
backgroundColor: '#16a34a',
|
| 171 |
-
height: '100%',
|
| 172 |
-
width: `${score}%`,
|
| 173 |
-
transition: 'width 0.5s ease-in-out'
|
| 174 |
-
}}
|
| 175 |
-
/>
|
| 176 |
</div>
|
| 177 |
-
<p style={{ textAlign: 'center', marginTop: '10px' }}>
|
| 178 |
-
分數:{score}/100
|
| 179 |
-
</p>
|
| 180 |
</div>
|
| 181 |
);
|
| 182 |
}
|
|
|
|
| 1 |
+
import React, { useState } from 'react';
|
| 2 |
|
| 3 |
+
// 簡化的成語資料庫
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
const idiomDatabase = [
|
| 5 |
{ mainIdiom: '急如星火', synonyms: ['迫在眉睫', '刻不容緩'], meaning: '形容事情非常緊急' },
|
| 6 |
{ mainIdiom: '一葉知秋', synonyms: ['見微知著', '月暈而風'], meaning: '從小徵兆可以預見未來的發展' },
|
| 7 |
+
{ mainIdiom: '一丘之貉', synonyms: ['沆瀣一氣', '狐群狗黨'], meaning: '形容品性相同的壞人' }
|
|
|
|
|
|
|
| 8 |
];
|
| 9 |
|
| 10 |
function ChineseIdiomGame() {
|
| 11 |
+
// 基本狀態
|
| 12 |
+
const [questionIndex, setQuestionIndex] = useState(0);
|
|
|
|
| 13 |
const [score, setScore] = useState(0);
|
| 14 |
+
const [selectedAnswers, setSelectedAnswers] = useState([]);
|
| 15 |
|
| 16 |
// 當前題目
|
| 17 |
+
const currentQuestion = idiomDatabase[questionIndex];
|
| 18 |
|
| 19 |
+
// 處理答案選擇
|
| 20 |
+
const handleSelect = (answer) => {
|
| 21 |
+
if (selectedAnswers.includes(answer)) return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
if (currentQuestion.synonyms.includes(answer)) {
|
| 24 |
+
const newSelected = [...selectedAnswers, answer];
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
setSelectedAnswers(newSelected);
|
| 26 |
|
| 27 |
if (newSelected.length === 2) {
|
| 28 |
+
const newScore = score + 10;
|
| 29 |
setScore(newScore);
|
| 30 |
+
|
| 31 |
+
// 延遲後切換到下一題
|
| 32 |
+
setTimeout(() => {
|
| 33 |
+
setQuestionIndex((questionIndex + 1) % idiomDatabase.length);
|
| 34 |
+
setSelectedAnswers([]);
|
| 35 |
+
}, 1000);
|
|
|
|
|
|
|
|
|
|
| 36 |
}
|
| 37 |
}
|
| 38 |
};
|
| 39 |
|
| 40 |
+
// 生成選項
|
| 41 |
+
const options = [
|
| 42 |
+
...currentQuestion.synonyms,
|
| 43 |
+
'望穿秋水',
|
| 44 |
+
'守株待兔',
|
| 45 |
+
'揮金如土',
|
| 46 |
+
'一暴十寒'
|
| 47 |
+
].sort(() => Math.random() - 0.5);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
return (
|
| 50 |
+
<div style={{ padding: '20px' }}>
|
| 51 |
+
<h1 style={{ textAlign: 'center', marginBottom: '20px' }}>成語對對碰</h1>
|
| 52 |
+
|
| 53 |
+
<div style={{
|
| 54 |
+
backgroundColor: '#f0f0f0',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
padding: '20px',
|
| 56 |
borderRadius: '8px',
|
| 57 |
marginBottom: '20px'
|
| 58 |
}}>
|
| 59 |
+
<h2 style={{ textAlign: 'center' }}>{currentQuestion.mainIdiom}</h2>
|
| 60 |
+
<p style={{ textAlign: 'center' }}>{currentQuestion.meaning}</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
</div>
|
| 62 |
|
| 63 |
+
<div style={{
|
| 64 |
+
display: 'grid',
|
| 65 |
+
gridTemplateColumns: 'repeat(2, 1fr)',
|
| 66 |
+
gap: '10px'
|
|
|
|
| 67 |
}}>
|
| 68 |
+
{options.map((option, index) => (
|
| 69 |
<button
|
| 70 |
key={index}
|
| 71 |
+
onClick={() => handleSelect(option)}
|
| 72 |
style={{
|
| 73 |
padding: '10px',
|
| 74 |
+
backgroundColor: selectedAnswers.includes(option) ? '#4CAF50' : 'white',
|
|
|
|
|
|
|
| 75 |
color: selectedAnswers.includes(option) ? 'white' : 'black',
|
| 76 |
+
border: '1px solid #ddd',
|
| 77 |
+
borderRadius: '4px'
|
| 78 |
}}
|
|
|
|
| 79 |
>
|
| 80 |
{option}
|
| 81 |
</button>
|
| 82 |
))}
|
| 83 |
</div>
|
| 84 |
|
| 85 |
+
<div style={{
|
| 86 |
+
marginTop: '20px',
|
| 87 |
+
textAlign: 'center'
|
|
|
|
|
|
|
| 88 |
}}>
|
| 89 |
+
分數: {score}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
</div>
|
|
|
|
|
|
|
|
|
|
| 91 |
</div>
|
| 92 |
);
|
| 93 |
}
|