Docs
useTicTacToe

A simple Tic Tac Toe game implemented as a React hook, allowing users to play the game with a friend or against the computer.

Loading...

Installation

pnpm dlx shadcn@latest add https://usekit.kiron.dev/k/use-tic-tac-toe

Usage

"use client"
 
import { useTicTacToe } from "@/hooks/use-tic-tac-toe"
 
export function Component() {
  const {
    board,
    currentPlayer,
    winner,
    isDraw,
    gameOver,
    makeMove,
    resetGame,
    playerNames,
  } = useTicTacToe({ X: "Alice", O: "Bob" })
 
  return (
    <div>
      <h1>Tic Tac Toe</h1>
      <div>
        {gameOver
          ? winner
            ? `${playerNames[winner]} wins!`
            : "It's a draw!"
          : `${playerNames[currentPlayer]}'s turn`}
      </div>
      <div
        style={{
          display: "grid",
          gridTemplateColumns: "repeat(3, 40px)",
          gap: "4px",
        }}
      >
        {board.map((cell, idx) => (
          <button
            key={idx}
            onClick={() => makeMove(idx)}
            disabled={!!cell || gameOver}
            style={{
              width: 40,
              height: 40,
              fontSize: 20,
              borderRadius: 4,
              background: cell ? "#3182ce" : "#eee",
              color: cell ? "#fff" : "#222",
              border: "1px solid #ccc",
              cursor: !!cell || gameOver ? "not-allowed" : "pointer",
              margin: 2,
            }}
          >
            {cell}
          </button>
        ))}
      </div>
      {gameOver && (
        <button
          onClick={resetGame}
          style={{
            padding: "8px 16px",
            borderRadius: 4,
            background: "#e53e3e",
            color: "#fff",
            border: "none",
            cursor: "pointer",
            marginTop: 8,
          }}
        >
          Play Again
        </button>
      )}
    </div>
  )
}

API Reference

Parameters

NameTypeDescriptionDefault ValueOptional
playerNames{ X?: string; O?: string }Names for player X and O.{ X: "Player X", O: "Player O" }Yes

Return Values

NameTypeDescription
board(string | null)[]The current board state (9 cells).
currentPlayer"X" | "O"The player whose turn it is.
winner"X" | "O" | nullThe winner, if any.
isDrawbooleanWhether the game ended in a draw.
gameOverbooleanWhether the game is over.
makeMove(index: number) => voidFunction to make a move at a given cell index.
resetGame() => voidFunction to reset the game.
playerNames{ X: string; O: string }The resolved player names.

Notes

  • The hook manages the game state, including board, current player, winner, and draw state.
  • You can provide custom player names via the playerNames parameter.
  • The board is a flat array of 9 cells (row-major order).
  • Designed for React and Next.js client components.