Spaces:
Sleeping
Sleeping
Upload components/GameBoard.jsx with huggingface_hub
Browse files- components/GameBoard.jsx +124 -0
components/GameBoard.jsx
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useState, useEffect } from 'react';
|
| 2 |
+
import Card from './Card';
|
| 3 |
+
|
| 4 |
+
const GameBoard = () => {
|
| 5 |
+
const emojis = ['๐ฎ', '๐ฒ', '๐ฏ', '๐จ', '๐ญ', '๐ช', '๐ธ', '๐บ'];
|
| 6 |
+
const [cards, setCards] = useState([]);
|
| 7 |
+
const [selectedCards, setSelectedCards] = useState([]);
|
| 8 |
+
const [matchedPairs, setMatchedPairs] = useState(0);
|
| 9 |
+
const [moves, setMoves] = useState(0);
|
| 10 |
+
const [isChecking, setIsChecking] = useState(false);
|
| 11 |
+
const [gameWon, setGameWon] = useState(false);
|
| 12 |
+
|
| 13 |
+
const initializeGame = () => {
|
| 14 |
+
const gameCards = [...emojis, ...emojis]
|
| 15 |
+
.sort(() => Math.random() - 0.5)
|
| 16 |
+
.map((emoji, index) => ({ id: index, emoji, isFlipped: false, isMatched: false }));
|
| 17 |
+
setCards(gameCards);
|
| 18 |
+
setSelectedCards([]);
|
| 19 |
+
setMatchedPairs(0);
|
| 20 |
+
setMoves(0);
|
| 21 |
+
setGameWon(false);
|
| 22 |
+
setIsChecking(false);
|
| 23 |
+
};
|
| 24 |
+
|
| 25 |
+
useEffect(() => {
|
| 26 |
+
initializeGame();
|
| 27 |
+
}, []);
|
| 28 |
+
|
| 29 |
+
useEffect(() => {
|
| 30 |
+
if (selectedCards.length === 2) {
|
| 31 |
+
setIsChecking(true);
|
| 32 |
+
setMoves(moves + 1);
|
| 33 |
+
const [first, second] = selectedCards;
|
| 34 |
+
|
| 35 |
+
if (first.emoji === second.emoji) {
|
| 36 |
+
setTimeout(() => {
|
| 37 |
+
setCards(cards.map(card =>
|
| 38 |
+
card.id === first.id || card.id === second.id
|
| 39 |
+
? { ...card, isMatched: true }
|
| 40 |
+
: card
|
| 41 |
+
));
|
| 42 |
+
setMatchedPairs(matchedPairs + 1);
|
| 43 |
+
setSelectedCards([]);
|
| 44 |
+
setIsChecking(false);
|
| 45 |
+
}, 600);
|
| 46 |
+
} else {
|
| 47 |
+
setTimeout(() => {
|
| 48 |
+
setCards(cards.map(card =>
|
| 49 |
+
card.id === first.id || card.id === second.id
|
| 50 |
+
? { ...card, isFlipped: false }
|
| 51 |
+
: card
|
| 52 |
+
));
|
| 53 |
+
setSelectedCards([]);
|
| 54 |
+
setIsChecking(false);
|
| 55 |
+
}, 1000);
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
}, [selectedCards]);
|
| 59 |
+
|
| 60 |
+
useEffect(() => {
|
| 61 |
+
if (matchedPairs === emojis.length && emojis.length > 0) {
|
| 62 |
+
setGameWon(true);
|
| 63 |
+
}
|
| 64 |
+
}, [matchedPairs]);
|
| 65 |
+
|
| 66 |
+
const handleCardClick = (clickedCard) => {
|
| 67 |
+
if (isChecking || clickedCard.isFlipped || clickedCard.isMatched) return;
|
| 68 |
+
|
| 69 |
+
setCards(cards.map(card =>
|
| 70 |
+
card.id === clickedCard.id
|
| 71 |
+
? { ...card, isFlipped: true }
|
| 72 |
+
: card
|
| 73 |
+
));
|
| 74 |
+
|
| 75 |
+
setSelectedCards([...selectedCards, clickedCard]);
|
| 76 |
+
};
|
| 77 |
+
|
| 78 |
+
return (
|
| 79 |
+
<div className="flex flex-col items-center justify-center min-h-screen bg-gradient-to-br from-indigo-100 to-purple-100 p-4">
|
| 80 |
+
<div className="text-center mb-8">
|
| 81 |
+
<h1 className="text-4xl md:text-5xl font-bold text-gray-800 mb-4">Memory Game</h1>
|
| 82 |
+
<p className="text-lg text-gray-600">Match the pairs to win!</p>
|
| 83 |
+
</div>
|
| 84 |
+
|
| 85 |
+
<div className="bg-white rounded-xl shadow-xl p-6 mb-6">
|
| 86 |
+
<div className="flex justify-between items-center mb-4">
|
| 87 |
+
<div className="text-lg font-semibold text-gray-700">
|
| 88 |
+
Moves: <span className="text-blue-600">{moves}</span>
|
| 89 |
+
</div>
|
| 90 |
+
<div className="text-lg font-semibold text-gray-700">
|
| 91 |
+
Pairs: <span className="text-green-600">{matchedPairs}/{emojis.length}</span>
|
| 92 |
+
</div>
|
| 93 |
+
</div>
|
| 94 |
+
|
| 95 |
+
<div className="grid grid-cols-4 gap-3 md:gap-4">
|
| 96 |
+
{cards.map((card) => (
|
| 97 |
+
<Card
|
| 98 |
+
key={card.id}
|
| 99 |
+
card={card}
|
| 100 |
+
onClick={() => handleCardClick(card)}
|
| 101 |
+
isFlipped={card.isFlipped}
|
| 102 |
+
isMatched={card.isMatched}
|
| 103 |
+
/>
|
| 104 |
+
))}
|
| 105 |
+
</div>
|
| 106 |
+
</div>
|
| 107 |
+
|
| 108 |
+
<button
|
| 109 |
+
onClick={initializeGame}
|
| 110 |
+
className="px-6 py-3 bg-gradient-to-r from-blue-500 to-purple-600 text-white font-semibold rounded-lg shadow-lg hover:from-blue-600 hover:to-purple-700 transform hover:scale-105 transition-all duration-200"
|
| 111 |
+
>
|
| 112 |
+
New Game
|
| 113 |
+
</button>
|
| 114 |
+
|
| 115 |
+
{gameWon && (
|
| 116 |
+
<div className="mt-6 p-4 bg-green-100 border-2 border-green-400 rounded-lg">
|
| 117 |
+
<p className="text-xl font-bold text-green-800">Congratulations! You won in {moves} moves!</p>
|
| 118 |
+
</div>
|
| 119 |
+
)}
|
| 120 |
+
</div>
|
| 121 |
+
);
|
| 122 |
+
};
|
| 123 |
+
|
| 124 |
+
export default GameBoard;
|