|
|
@@ -0,0 +1,158 @@
|
|
|
+import { useState, useEffect } from 'react';
|
|
|
+
|
|
|
+// 宠物图标(可替换为你自己的图片)
|
|
|
+const pets = [
|
|
|
+ '🐶', '🐱', '🐭', '🐹', '🐰', '🦊', '🐻', '🐼',
|
|
|
+ '🐨', '🐯', '🦁', '🐮', '🐷', '🐸', '🐵', '🐔'
|
|
|
+];
|
|
|
+
|
|
|
+// 生成随机配对卡片
|
|
|
+const generateCards = () => {
|
|
|
+ // 取前5对(10张),可调整数量
|
|
|
+ const selected = pets.slice(0, 5);
|
|
|
+ const pairs = [...selected, ...selected]; // 复制一份形成配对
|
|
|
+ return pairs.sort(() => Math.random() - 0.5); // 打乱顺序
|
|
|
+};
|
|
|
+
|
|
|
+export default function Setting() {
|
|
|
+ const [cards, setCards] = useState([]);
|
|
|
+ const [flipped, setFlipped] = useState([]); // 已翻开的卡片索引
|
|
|
+ const [matched, setMatched] = useState([]); // 已配对成功的索引
|
|
|
+ const [moves, setMoves] = useState(0); // 移动次数
|
|
|
+ const [remaining, setRemaining] = useState(0); // 剩余配对数
|
|
|
+
|
|
|
+ // 初始化游戏
|
|
|
+ useEffect(() => {
|
|
|
+ const newCards = generateCards();
|
|
|
+ setCards(newCards);
|
|
|
+ setFlipped([]);
|
|
|
+ setMatched([]);
|
|
|
+ setMoves(0);
|
|
|
+ setRemaining(newCards.length / 2); // 总配对数
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ // 处理卡片点击
|
|
|
+ const handleClick = (index) => {
|
|
|
+ // 已翻开或已配对,不处理
|
|
|
+ if (flipped.includes(index) || matched.includes(index)) return;
|
|
|
+
|
|
|
+ let newFlipped = [...flipped, index];
|
|
|
+ setFlipped(newFlipped);
|
|
|
+ setMoves(moves + 1);
|
|
|
+
|
|
|
+ // 翻开两张时判断是否配对
|
|
|
+ if (newFlipped.length === 2) {
|
|
|
+ const [first, second] = newFlipped;
|
|
|
+ if (cards[first] === cards[second]) {
|
|
|
+ // 配对成功
|
|
|
+ setMatched([...matched, first, second]);
|
|
|
+ setRemaining(remaining - 1);
|
|
|
+ }
|
|
|
+ // 1秒后翻回
|
|
|
+ setTimeout(() => setFlipped([]), 1000);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ // 重新开始
|
|
|
+ const restart = () => {
|
|
|
+ const newCards = generateCards();
|
|
|
+ setCards(newCards);
|
|
|
+ setFlipped([]);
|
|
|
+ setMatched([]);
|
|
|
+ setMoves(0);
|
|
|
+ setRemaining(newCards.length / 2);
|
|
|
+ };
|
|
|
+
|
|
|
+ // 计算准确率
|
|
|
+ const accuracy = moves === 0
|
|
|
+ ? 0
|
|
|
+ : Math.round((matched.length / 2 / moves) * 100);
|
|
|
+
|
|
|
+ return (
|
|
|
+ <div style={{
|
|
|
+ width: '100%',
|
|
|
+ minHeight: '100vh',
|
|
|
+ padding: '20px',
|
|
|
+ boxSizing: 'border-box'
|
|
|
+ }}>
|
|
|
+ {/* 顶部导航 */}
|
|
|
+ <div style={{
|
|
|
+ display: 'flex',
|
|
|
+ justifyContent: 'space-between',
|
|
|
+ fontSize: '20px',
|
|
|
+ fontWeight: 'bold',
|
|
|
+ marginBottom: '30px'
|
|
|
+ }}>
|
|
|
+ <span></span>
|
|
|
+ <span style={{ color: '#a0f' }}></span>
|
|
|
+ <span></span>
|
|
|
+ </div>
|
|
|
+
|
|
|
+ {/* 游戏标题 */}
|
|
|
+ <h1 style={{ textAlign: 'center', margin: '20px 0' }}>
|
|
|
+ 宠物配对
|
|
|
+ </h1>
|
|
|
+
|
|
|
+ {/* 统计信息 */}
|
|
|
+ <div style={{
|
|
|
+ textAlign: 'center',
|
|
|
+ fontSize: '16px',
|
|
|
+ marginBottom: '20px'
|
|
|
+ }}>
|
|
|
+ 移动次数: {moves} | 剩余配对: {remaining} | 准确率: {accuracy}%
|
|
|
+ </div>
|
|
|
+
|
|
|
+ {/* 卡片网格 */}
|
|
|
+ <div style={{
|
|
|
+ display: 'grid',
|
|
|
+ gridTemplateColumns: 'repeat(6, 1fr)',
|
|
|
+ gap: '10px',
|
|
|
+ maxWidth: '600px',
|
|
|
+ margin: '0 auto'
|
|
|
+ }}>
|
|
|
+ {cards.map((pet, index) => (
|
|
|
+ <div
|
|
|
+ key={index}
|
|
|
+ onClick={() => handleClick(index)}
|
|
|
+ style={{
|
|
|
+ width: '80px',
|
|
|
+ height: '80px',
|
|
|
+ backgroundColor: '#09f',
|
|
|
+ color: '#fff',
|
|
|
+ display: 'flex',
|
|
|
+ alignItems: 'center',
|
|
|
+ justifyContent: 'center',
|
|
|
+ fontSize: '30px',
|
|
|
+ borderRadius: '8px',
|
|
|
+ cursor: 'pointer',
|
|
|
+ userSelect: 'none'
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ {/* 已配对或已翻开 → 显示宠物;否则显示 ? */}
|
|
|
+ {matched.includes(index) || flipped.includes(index)
|
|
|
+ ? pet
|
|
|
+ : '?'}
|
|
|
+ </div>
|
|
|
+ ))}
|
|
|
+ </div>
|
|
|
+
|
|
|
+ {/* 重新开始按钮 */}
|
|
|
+ <div style={{ textAlign: 'center', marginTop: '30px' }}>
|
|
|
+ <button
|
|
|
+ onClick={restart}
|
|
|
+ style={{
|
|
|
+ padding: '10px 20px',
|
|
|
+ fontSize: '16px',
|
|
|
+ backgroundColor: '#0c6',
|
|
|
+ color: '#fff',
|
|
|
+ border: 'none',
|
|
|
+ borderRadius: '8px',
|
|
|
+ cursor: 'pointer'
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ 重新开始
|
|
|
+ </button>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ );
|
|
|
+}
|