parent
979a096b95
commit
62eb682d0c
@ -1,103 +1,12 @@
|
|||||||
import Image from "next/image";
|
import MemoryGame from '@/components/MemoryGame'
|
||||||
|
import React from 'react'
|
||||||
|
|
||||||
export default function Home() {
|
const page = () => {
|
||||||
return (
|
return (
|
||||||
<div className="font-sans grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-screen p-8 pb-20 gap-16 sm:p-20">
|
<div>
|
||||||
<main className="flex flex-col gap-[32px] row-start-2 items-center sm:items-start">
|
<MemoryGame/>
|
||||||
<Image
|
|
||||||
className="dark:invert"
|
|
||||||
src="/next.svg"
|
|
||||||
alt="Next.js logo"
|
|
||||||
width={180}
|
|
||||||
height={38}
|
|
||||||
priority
|
|
||||||
/>
|
|
||||||
<ol className="font-mono list-inside list-decimal text-sm/6 text-center sm:text-left">
|
|
||||||
<li className="mb-2 tracking-[-.01em]">
|
|
||||||
Get started by editing{" "}
|
|
||||||
<code className="bg-black/[.05] dark:bg-white/[.06] font-mono font-semibold px-1 py-0.5 rounded">
|
|
||||||
app/page.js
|
|
||||||
</code>
|
|
||||||
.
|
|
||||||
</li>
|
|
||||||
<li className="tracking-[-.01em]">
|
|
||||||
Save and see your changes instantly.
|
|
||||||
</li>
|
|
||||||
</ol>
|
|
||||||
|
|
||||||
<div className="flex gap-4 items-center flex-col sm:flex-row">
|
|
||||||
<a
|
|
||||||
className="rounded-full border border-solid border-transparent transition-colors flex items-center justify-center bg-foreground text-background gap-2 hover:bg-[#383838] dark:hover:bg-[#ccc] font-medium text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5 sm:w-auto"
|
|
||||||
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
|
||||||
target="_blank"
|
|
||||||
rel="noopener noreferrer"
|
|
||||||
>
|
|
||||||
<Image
|
|
||||||
className="dark:invert"
|
|
||||||
src="/vercel.svg"
|
|
||||||
alt="Vercel logomark"
|
|
||||||
width={20}
|
|
||||||
height={20}
|
|
||||||
/>
|
|
||||||
Deploy now
|
|
||||||
</a>
|
|
||||||
<a
|
|
||||||
className="rounded-full border border-solid border-black/[.08] dark:border-white/[.145] transition-colors flex items-center justify-center hover:bg-[#f2f2f2] dark:hover:bg-[#1a1a1a] hover:border-transparent font-medium text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5 w-full sm:w-auto md:w-[158px]"
|
|
||||||
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
|
||||||
target="_blank"
|
|
||||||
rel="noopener noreferrer"
|
|
||||||
>
|
|
||||||
Read our docs
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</main>
|
|
||||||
<footer className="row-start-3 flex gap-[24px] flex-wrap items-center justify-center">
|
|
||||||
<a
|
|
||||||
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
|
|
||||||
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
|
||||||
target="_blank"
|
|
||||||
rel="noopener noreferrer"
|
|
||||||
>
|
|
||||||
<Image
|
|
||||||
aria-hidden
|
|
||||||
src="/file.svg"
|
|
||||||
alt="File icon"
|
|
||||||
width={16}
|
|
||||||
height={16}
|
|
||||||
/>
|
|
||||||
Learn
|
|
||||||
</a>
|
|
||||||
<a
|
|
||||||
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
|
|
||||||
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
|
||||||
target="_blank"
|
|
||||||
rel="noopener noreferrer"
|
|
||||||
>
|
|
||||||
<Image
|
|
||||||
aria-hidden
|
|
||||||
src="/window.svg"
|
|
||||||
alt="Window icon"
|
|
||||||
width={16}
|
|
||||||
height={16}
|
|
||||||
/>
|
|
||||||
Examples
|
|
||||||
</a>
|
|
||||||
<a
|
|
||||||
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
|
|
||||||
href="https://nextjs.org?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
|
||||||
target="_blank"
|
|
||||||
rel="noopener noreferrer"
|
|
||||||
>
|
|
||||||
<Image
|
|
||||||
aria-hidden
|
|
||||||
src="/globe.svg"
|
|
||||||
alt="Globe icon"
|
|
||||||
width={16}
|
|
||||||
height={16}
|
|
||||||
/>
|
|
||||||
Go to nextjs.org →
|
|
||||||
</a>
|
|
||||||
</footer>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default page
|
@ -0,0 +1,147 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import React, { useEffect, useState } from "react";
|
||||||
|
|
||||||
|
const MemoryGame = () => {
|
||||||
|
const [gridSize, setGridSize] = useState(2);
|
||||||
|
|
||||||
|
const [array, setArray] = useState([]);
|
||||||
|
const [flipped, setFlipped] = useState([]);
|
||||||
|
const [slectedPairs, setSelectedPairs] = useState([]);
|
||||||
|
const [disabled, setDisabled] = useState(false);
|
||||||
|
|
||||||
|
const [won, setWon] = useState(false);
|
||||||
|
|
||||||
|
const handleGridSize = (e) => {
|
||||||
|
const size = parseInt(e.target.value);
|
||||||
|
//console.log(size)
|
||||||
|
if (2 <= size && size <= 10) {
|
||||||
|
setGridSize(size);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const initalizeGame = () => {
|
||||||
|
const totalCards = gridSize * gridSize;
|
||||||
|
const pairCount = Math.floor(totalCards / 2);
|
||||||
|
|
||||||
|
const numbers = [...Array(pairCount).keys()].map((n) => n + 1);
|
||||||
|
const suffledCards = [...numbers, ...numbers]
|
||||||
|
.sort(() => Math.random() - 0.5)
|
||||||
|
.map((number, index) => ({
|
||||||
|
id: index,
|
||||||
|
number,
|
||||||
|
}));
|
||||||
|
|
||||||
|
setArray(suffledCards);
|
||||||
|
setFlipped([]);
|
||||||
|
setSelectedPairs([]);
|
||||||
|
setDisabled(false);
|
||||||
|
setWon(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleMatch = (secondId) => {
|
||||||
|
const [firstId] = flipped;
|
||||||
|
|
||||||
|
if (array[firstId].number == array[secondId].number) {
|
||||||
|
setSelectedPairs([...slectedPairs, firstId, secondId]);
|
||||||
|
setFlipped([]);
|
||||||
|
setDisabled(false);
|
||||||
|
} else {
|
||||||
|
setTimeout(() => {
|
||||||
|
setDisabled(false);
|
||||||
|
setFlipped([]);
|
||||||
|
}, 1000);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
initalizeGame();
|
||||||
|
}, [gridSize]);
|
||||||
|
|
||||||
|
const handleClick = (id) => {
|
||||||
|
if (disabled || won) return;
|
||||||
|
|
||||||
|
if (flipped.length === 0) {
|
||||||
|
setFlipped([id]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flipped.length === 1) {
|
||||||
|
setDisabled(true); //so we cant choose another one
|
||||||
|
if (id !== flipped[0]) {
|
||||||
|
setFlipped([...flipped, id]);
|
||||||
|
//check match logic
|
||||||
|
handleMatch(id);
|
||||||
|
} else {
|
||||||
|
setFlipped([]);
|
||||||
|
setDisabled(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const isFlipped = (id) => flipped.includes(id) || slectedPairs.includes(id);
|
||||||
|
const isselectedpairs = (id) => slectedPairs.includes(id);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (slectedPairs.length === array.length && array.length > 0) {
|
||||||
|
setWon(true);
|
||||||
|
}
|
||||||
|
}, [slectedPairs, array]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="h-[100vh] flex flex-col justify-center items-center p-4 bg-gray-100 ">
|
||||||
|
{/* Heading */}
|
||||||
|
<h1 className="text-3xl font-bold mb-6 ">Memory Game</h1>
|
||||||
|
{/* Grid Size */}
|
||||||
|
<div className="mb-4">
|
||||||
|
<label htmlFor="gridSize">Grid Size:(max 10)</label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
className="w-[50px] ml-3 rounded border-2 px-1.5 py-1"
|
||||||
|
min="2"
|
||||||
|
max="10"
|
||||||
|
value={gridSize}
|
||||||
|
onChange={handleGridSize}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
{/* Cards */}
|
||||||
|
<div
|
||||||
|
className="grid gap-2 mb-4"
|
||||||
|
style={{
|
||||||
|
gridTemplateColumns: `repeat(${gridSize}, minmax(0,1fr))`,
|
||||||
|
width: `min(100%,${gridSize * 5.5}rem)`,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{array.map((array) => (
|
||||||
|
<div
|
||||||
|
key={array.id}
|
||||||
|
onClick={() => handleClick(array.id)}
|
||||||
|
className={`aspect-square flex items-center justify-center text-xl transition-all duration-300 font-bold rounded-lg cursor-pointer ${
|
||||||
|
isFlipped(array.id)
|
||||||
|
? isselectedpairs(array.id)
|
||||||
|
? "bg-green-500 text-white"
|
||||||
|
: "bg-blue-500 text-white"
|
||||||
|
: "bg-gray-300 text-gray-400 "
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{isFlipped(array.id) ? array.number : "?"}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
{/* Result */}
|
||||||
|
<div className="text-2xl text-green-500 font-bold">
|
||||||
|
{won ? "You Won!" : ""}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Rest Button */}
|
||||||
|
<button
|
||||||
|
className="px-5 py-2 bg-green-500 rounded text-white mt-5"
|
||||||
|
onClick={initalizeGame}
|
||||||
|
>
|
||||||
|
Reset
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default MemoryGame;
|
Loading…
Reference in new issue