đž Game UI
Matching Game UI
App
import { useState } from 'react';
import Card from './components/Card';
import shuffle from './utilities/shuffle';
function App() {
const [cards, setCards] = useState(shuffle); // Cards array from assets
return (
<>
<div className="grid">
{cards.map((card) => {
const { image, id, matched } = card;
return (
<Card
key={id}
image={image}
selected={false}
onClick={() => {}}
/>
);
})}
</div>
</>
);
}
export default App;
Card
const Card = ({ image, selected, onClick }) => {
return (
<div className="card">
<div className={selected && 'selected'}>
<img alt="" src={image} className="card-face" />
<img
alt=""
className="card-back"
src={'/assets/fireship.png'}
onClick={onClick}
/>
</div>
</div>
);
};
export default Card;
Shuffle
// Combine and shuffle two arrays
const shuffle = () => {
const assets = [
{ image: '/assets/css.png' },
{ image: '/assets/html5.png' },
{ image: '/assets/jquery.png'},
{ image: '/assets/js.png' },
{ image: '/assets/next.png' },
{ image: '/assets/node.png' },
{ image: '/assets/react.png'},
{ image: '/assets/ts.png' },
];
return [...assets, ...assets]
.sort(() => Math.random() - 0.5)
.map((card) => ({ ...card, id: Math.random() }));
};
export default shuffle;