From 3705152d341826fc58bad2b8bf1b21546bd4a5ef Mon Sep 17 00:00:00 2001 From: Neeldhara Misra Date: Tue, 22 Jul 2025 02:40:47 +0530 Subject: [PATCH] feat: add standalone Nim game page, route, and container layout; remove global header/footer from /games/nim --- src/App.tsx | 2 ++ src/components/InteractiveIndex.tsx | 9 ++++++ src/components/NimGame.tsx | 47 ++++++++++++++++++++++++----- src/pages/Index.tsx | 7 +++++ src/pages/NimGamePage.tsx | 12 ++++++++ 5 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 src/pages/NimGamePage.tsx diff --git a/src/App.tsx b/src/App.tsx index fd21542..69df7cb 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -29,6 +29,7 @@ import TernarySearchTrickPage from "./pages/TernarySearchTrickPage"; import TernarySearchTrickGreenScreen from "./pages/TernarySearchTrickGreenScreen"; import NorthcottsGamePage from "./pages/NorthcottsGamePage"; import KnightsPuzzlePage from "./pages/KnightsPuzzlePage"; +import NimGamePage from "./pages/NimGamePage"; import Foundations from "./pages/theme-pages/discrete-math/Foundations"; import Proofs from "./pages/theme-pages/discrete-math/Proofs"; import Counting from "./pages/theme-pages/discrete-math/Counting"; @@ -84,6 +85,7 @@ const App = () => ( } /> } /> } /> + } /> {/* ADD ALL CUSTOM ROUTES ABOVE THE CATCH-ALL "*" ROUTE */} } /> diff --git a/src/components/InteractiveIndex.tsx b/src/components/InteractiveIndex.tsx index 4836a38..2780529 100644 --- a/src/components/InteractiveIndex.tsx +++ b/src/components/InteractiveIndex.tsx @@ -90,6 +90,15 @@ const allInteractives: Interactive[] = [ path: '/knights-puzzle', theme: 'Puzzles', dateAdded: '2024-07-21' + }, + { + id: 'nim', + title: 'Game of Nim', + description: 'A classic two-player game of strategy. Take turns removing stones from heaps. The player to take the last stone wins!', + tags: ['strategy', 'two-player', 'math', 'logic', 'game-theory'], + path: '/games/nim', + theme: 'Games', + dateAdded: '2024-07-21' } ]; diff --git a/src/components/NimGame.tsx b/src/components/NimGame.tsx index e8771a2..924a523 100644 --- a/src/components/NimGame.tsx +++ b/src/components/NimGame.tsx @@ -41,6 +41,8 @@ const NimGame: React.FC = () => { const initialMaxHeapSizeRef = useRef(0); // Store the pixel width for the entire heaps group container (label + stones) const [heapsGroupWidth, setHeapsGroupWidth] = useState('0px'); + // Store the initial heaps for reset + const initialHeapsRef = useRef([]); // When a new game starts, record the initial heap sizes and max size and compute the group width React.useEffect(() => { @@ -58,6 +60,14 @@ const NimGame: React.FC = () => { // eslint-disable-next-line }, [mode, heaps.length]); + // When a new game starts, record the initial heaps + React.useEffect(() => { + if (mode !== null && heaps.length > 0) { + initialHeapsRef.current = [...heaps]; + } + // eslint-disable-next-line + }, [mode, heaps.length]); + // Start a new game const startGame = (selectedMode: Mode) => { setMode(selectedMode); @@ -103,6 +113,21 @@ const NimGame: React.FC = () => { setCurrentPlayer(0); }; + // Reset the game to the initial heaps + const handleResetGame = () => { + setHeaps([...initialHeapsRef.current]); + setWinner(null); + setCurrentPlayer(0); + }; + + // Start a new game (go back to mode selection) + const handleNewGame = () => { + setMode(null); + setHeaps([]); + setWinner(null); + setCurrentPlayer(0); + }; + // User heap count slider change const handleHeapCountChange = (val: number) => { setUserHeapCount(val); @@ -166,15 +191,21 @@ const NimGame: React.FC = () => { {mode !== null && ( <> - {/* Current Turn Indicator */} -
+ {/* Current Turn Indicator and Reset Button */} +
{winner === null ? ( -
-

Current Turn:

- - Player {currentPlayer + 1} - -
+ <> +
+

Current Turn:

+ + Player {currentPlayer + 1} + +
+
+ + +
+ ) : (
Winner: Player {winner + 1}! diff --git a/src/pages/Index.tsx b/src/pages/Index.tsx index fe6413c..2026e9b 100644 --- a/src/pages/Index.tsx +++ b/src/pages/Index.tsx @@ -62,6 +62,13 @@ const allInteractives = [{ tags: ['chess', 'knights', 'puzzle'], path: '/knights-puzzle', theme: 'Puzzles' +}, { + id: 'nim', + title: 'Game of Nim', + description: 'A classic two-player game of strategy. Take turns removing stones from heaps. The player to take the last stone wins!', + tags: ['strategy', 'two-player', 'math', 'logic'], + path: '/games/nim', + theme: 'Games' }]; // Featured interactives (3 fixed ones) diff --git a/src/pages/NimGamePage.tsx b/src/pages/NimGamePage.tsx new file mode 100644 index 0000000..aa202f6 --- /dev/null +++ b/src/pages/NimGamePage.tsx @@ -0,0 +1,12 @@ +import React from 'react'; +import NimGame from '@/components/NimGame'; + +const NimGamePage = () => { + return ( +
+ +
+ ); +}; + +export default NimGamePage; \ No newline at end of file