UNPKG

tmemory

Version:

A terminal-based Memory card game built with React Ink. Features multiple grid sizes, AI opponent, and high scores.

19 lines (18 loc) 763 B
import React, { createContext, useContext, useReducer } from 'react'; import { gameReducer, initialState } from "./reducer.js"; const GameContext = createContext(undefined); export const GameProvider = ({ children, initialMode, initialGrid, }) => { const [state, dispatch] = useReducer(gameReducer, { ...initialState, ...(initialMode && { gameMode: initialMode }), ...(initialGrid && { gridDimension: initialGrid }), }); return (React.createElement(GameContext.Provider, { value: { state, dispatch } }, children)); }; export const useGame = () => { const context = useContext(GameContext); if (context === undefined) { throw new Error('useGame must be used within a GameProvider'); } return context; };