بتل کد Battle Codeدارد باز می‌شود…

مثال JavaScript: <script type="text/jsx"> — پروژه: بازی حافظه

مثال 3 از درس جلسه‌ی 35 آموزش ری‌اکت (React) از صفر برای نوجوانان، قطعه به قطعه: پروژه: بازی حافظه

بازی تمام‌شده این است. بازی کن و بعد کد را بخوان. سه چیز این فاز را در آن پیدا کن: reducer که همه‌ی قانون‌ها داخلش است، هوک useGame که بازی را به App می‌دهد، و useEffect داخل هوک که کارت‌های اشتباه را با setTimeout می‌بندد. App و Tile فقط صفحه را می‌کشند و هیچ قانونی نمی‌دانند.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#board { display: grid; grid-template-columns: repeat(3, 72px); gap: 8px; justify-content: center; }
.tile { width: 72px; height: 72px; margin: 0; font-size: 28px; border: 2px solid #334155; border-radius: 12px; background: #e2e8f0; }
.done { background: #bbf7d0; }
</style>

<div id="root" dir="ltr"></div>

<script type="text/jsx">
import { useReducer, useEffect } from "react";
import { createRoot } from "react-dom/client";

const CARDS = ["🍎", "🍌", "🍎", "🍇", "🍌", "🍇"];
const START = { open: [], done: [], moves: 0 };

function reducer(game, action) {
  if (action.type === "flip") {
    const i = action.index;
    if (game.open.length === 2 || game.open.includes(i) || game.done.includes(i)) {
      return game;
    }
    if (game.open.length === 0) {
      return { ...game, open: [i] };
    }
    const first = game.open[0];
    if (CARDS[first] === CARDS[i]) {
      return { open: [], done: [...game.done, first, i], moves: game.moves + 1 };
    }
    return { ...game, open: [first, i], moves: game.moves + 1 };
  }
  if (action.type === "hide") {
    return { ...game, open: [] };
  }
  if (action.type === "reset") {
    return START;
  }
  return game;
}

function useGame() {
  const [game, dispatch] = useReducer(reducer, START);

  useEffect(() => {
    if (game.open.length !== 2) {
      return;
    }
    const id = setTimeout(() => dispatch({ type: "hide" }), 1000);
    return () => clearTimeout(id);
  }, [game.open]);

  function flip(index) {
    dispatch({ type: "flip", index: index });
  }

  function reset() {
    dispatch({ type: "reset" });
  }

  return { game, flip, reset };
}

function Tile({ index, game, flip }) {
  const done = game.done.includes(index);
  const up = done || game.open.includes(index);
  return (
    <button id={"t" + index} className={done ? "tile done" : "tile"} onClick={() => flip(index)}>
      {up ? CARDS[index] : "?"}
    </button>
  );
}

function App() {
  const { game, flip, reset } = useGame();
  const won = game.done.length === CARDS.length;
  return (
    <div>
      <h1>Memory Game</h1>
      <p id="moves">Moves: {game.moves}</p>
      <div id="board">
        {CARDS.map((card, i) => (
          <Tile key={i} index={i} game={game} flip={flip} />
        ))}
      </div>
      {won && <p id="win">You win in {game.moves} moves!</p>}
      <button id="again" onClick={reset}>Play again</button>
    </div>
  );
}

createRoot(document.getElementById("root")).render(<App />);
</script>

این کد را همین‌جا در ویرایشگر HTML اجرا کن، عددها و متن‌هایش را عوض کن و دوباره اجرا کن تا ببینی چه تغییری می‌کند.

بیشتر بخوان: جاوااسکریپت چیست؟ از دکمه‌ی وب تا Node.js

برگشت به درس جلسه‌ی 35: پروژه: بازی حافظه · چالش‌های این جلسه