Docs
useMemoryGame

A simple Memory game implemented as a React hook, allowing users to match pairs of cards.

Loading...

Installation

pnpm dlx shadcn@latest add https://usekit.kiron.dev/k/use-memory-game

Usage

"use client"
 
import { useMemoryGame } from "@/hooks/use-memory-game"
 
const symbols = ["🙂", "😌", "😍", "🥰"]
 
export function Component() {
  const {
    cards,
    flipCard,
    startGame,
    resetGame,
    isGameComplete,
    matches,
    totalPairs,
    isStarted,
  } = useMemoryGame(symbols)
 
  return (
    <div>
      <h1>Memory Game</h1>
      {!isStarted && (
        <button
          style={{
            padding: "8px 16px",
            borderRadius: 4,
            background: "#222",
            color: "#fff",
            border: "none",
            cursor: "pointer",
          }}
          onClick={startGame}
        >
          Start Game
        </button>
      )}
      {isStarted && (
        <div>
          Matches: {matches} / {totalPairs}
        </div>
      )}
      <div
        style={{
          display: "grid",
          gridTemplateColumns: "repeat(4, 40px)",
          gap: "4px",
        }}
      >
        {cards.map((card) => (
          <button
            key={card.id}
            onClick={() => flipCard(card.id)}
            disabled={card.isFlipped || card.isMatched || !isStarted}
            style={{
              width: 40,
              height: 40,
              fontSize: 20,
              borderRadius: 4,
              background: card.isFlipped || card.isMatched ? "#3182ce" : "#eee",
              color: card.isFlipped || card.isMatched ? "#fff" : "#222",
              border: "1px solid #ccc",
              cursor:
                card.isFlipped || card.isMatched || !isStarted
                  ? "not-allowed"
                  : "pointer",
              margin: 2,
            }}
          >
            {card.isFlipped || card.isMatched ? card.value : "❓"}
          </button>
        ))}
      </div>
      {isGameComplete && <div>🎉 You won!</div>}
      {isStarted && (
        <button
          style={{
            padding: "8px 16px",
            borderRadius: 4,
            background: "#e53e3e",
            color: "#fff",
            border: "none",
            cursor: "pointer",
          }}
          onClick={resetGame}
        >
          Restart
        </button>
      )}
    </div>
  )
}

API Reference

Parameters

NameTypeDescriptionDefault ValueOptional
valuesstring[]Array of unique values for the card pairs.No

Return Values

NameTypeDescription
cards{ id: number; value: string; isFlipped: boolean; isMatched: boolean }[]Array of cards with their state.
flipCard(cardId: number) => voidFunction to flip a card by its id.
startGame() => voidFunction to start the game.
resetGame() => voidFunction to reset the game.
isGameCompletebooleanWhether all pairs have been matched.
matchesnumberNumber of matched pairs.
totalPairsnumberTotal number of pairs in the game.
isStartedbooleanWhether the game has started.

Notes

  • The hook manages the game state, including card flipping, matching, and completion.
  • Provide an array of unique values (e.g., emojis, strings) to generate pairs.
  • The game starts when you call startGame and can be reset with resetGame.
  • Designed for React and Next.js client components.