# Glyph match

Turn over cards and find four matching pairs in a shuffled deck.

[View Glyph match on CyberSpec](https://www.cyberspec.dev/sink/glyph-match)

## Installation

Use a React 19 project with shadcn configured. Render the installed components inside the Cyber root, and import your global stylesheet in the app entry point.

```sh
bunx --bun shadcn@latest add https://www.cyberspec.dev/r/glyph-match.json
```

## Usage

```tsx
import { Cyber } from "@/components/ui/cyber"
import Recipe from "@/components/recipes/glyph-match"

export default function App() {
  return (
    <Cyber>
      <Recipe />
    </Cyber>
  )
}
```

## Source

```tsx
"use client"

import { useState } from "react"
import { Asterisk, Diamond, Hexagon, Triangle, Waypoints } from "lucide-react"
import { Button } from "@/components/ui/button"
import { Badge } from "@/components/ui/badge"
import "./recipe-layout.css"

const shapes = [Diamond, Hexagon, Triangle, Waypoints]
const names = ["Diamond", "Hexagon", "Triangle", "Constellation"]
function shuffle() {
  const deck = [0, 1, 2, 3, 0, 1, 2, 3]
  for (let i = deck.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1))
    ;[deck[i], deck[j]] = [deck[j], deck[i]]
  }
  return deck
}

export default function GlyphMatch() {
  const [cards, setCards] = useState(shuffle)
  const [flipped, setFlipped] = useState<number[]>([])
  const [matched, setMatched] = useState<number[]>([])
  const [turns, setTurns] = useState(0)
  function reveal(index: number) {
    const previous = flipped.length === 1 ? flipped[0] : undefined
    if (previous === undefined) { setFlipped([index]); return }
    setTurns(turns + 1)
    if (cards[previous] === cards[index]) {
      setMatched([...matched, previous, index])
      setFlipped([])
    } else setFlipped([previous, index])
  }
  return (
    <div className="sink-stack">
      <div className="sink-between"><span className="sink-label">Memory deck</span><Badge tone="signal">{matched.length / 2} / 4 pairs</Badge></div>
      <div style={{display: "grid", gridTemplateColumns: "repeat(4, minmax(0, 1fr))", gap: 5}}>
        {cards.map((shape, index) => {
          const known = matched.includes(index)
          const visible = known || flipped.includes(index)
          const Icon = visible ? shapes[shape] : Asterisk
          return <Button key={index} tone={known ? "signal" : visible ? "flag" : "secondary"}
            disabled={known || (flipped.length === 1 && flipped[0] === index)} style={{height: 56}}
            aria-label={`Card ${index + 1}, ${visible ? names[shape] : "face down"}${known ? ", matched" : ""}`}
            onClick={() => reveal(index)}><Icon aria-hidden /></Button>
        })}
      </div>
      <div className="sink-between">
        <span className="sink-note" role="status">{matched.length === 8 ? `All pairs in ${turns} turns` : flipped.length === 2 ? "No match. Pick another card." : flipped.length ? "Find its partner." : "Turn over two cards."}</span>
        <Button size="sm" onClick={() => { setCards(shuffle()); setMatched([]); setFlipped([]); setTurns(0) }}>Shuffle</Button>
      </div>
    </div>
  )
}
```

## Components in this recipe

- [Button](https://www.cyberspec.dev/components/sidebar/button.md) — A cut-corner button with a raised tab, five tones, and a press flash. Enable the cipher prop to scramble the label and briefly tear the frame on hover.
- [Badge](https://www.cyberspec.dev/components/sidebar/badge.md) — A small cut label in seven tones, with count and status variants.

## Resources

- [All kitchen sink recipes](https://www.cyberspec.dev/sink.md)
- [Recipe source on GitHub](https://github.com/agnelnieves/cyberspec/blob/main/registry/cyber/recipes/glyph-match.tsx)
- [Recipe layout stylesheet](https://github.com/agnelnieves/cyberspec/blob/main/registry/cyber/recipes/recipe-layout.css)
- [Shadcn registry item](https://www.cyberspec.dev/r/glyph-match.json)
