diff --git a/src/App.jsx b/src/App.jsx index 62b11d4..e29daf2 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -19,6 +19,7 @@ import TabTest from "./pages/tree-view/custom-tabs/Tab-test"; import { ModalPopup } from "./pages/CustomModalPopup/ModalPopup"; import { GithubProfileFinder } from "./pages/GithubProfileFinder/GithubProfileFinder"; import { SearchAutoComplete } from "./pages/SearchAutoComplete/SearchAutoComplete"; +import { TicTacToe } from "./pages/TicTacToe"; function App() { return ( @@ -47,15 +48,20 @@ function App() { {/* generate QR code */} } /> {/* Light Dark Mode */} - } /> + } /> {/* Custom tabs component */} } /> {/* Custom Modal Popup */} - } /> + } /> {/* Github Profile Finder */} - } /> + } + /> {/* Search Auto-Complete */} } /> + {/* Tic Tac Toe */} + } /> {/* Error Page */} } /> diff --git a/src/data/projects.js b/src/data/projects.js index 69ac788..d8a125a 100644 --- a/src/data/projects.js +++ b/src/data/projects.js @@ -58,6 +58,11 @@ export const projects = [ id: 12, name: 'Search Auto-Complete', path: 'search-auto-complete' + }, + { + id: 13, + name: 'Tic Tac Toe', + path: 'tic-tac-toe' } ]; diff --git a/src/index.css b/src/index.css index e3d2cb8..3ab9809 100644 --- a/src/index.css +++ b/src/index.css @@ -102,7 +102,7 @@ box-sizing: border-box; background: linear-gradient(theme(colors.foreground) 0 0) 0 0/8px 20px, - linear-gradient(theme(colors.foreground)0 0) 100% 0/8px 20px, + linear-gradient(theme(colors.foreground) 0 0) 100% 0/8px 20px, radial-gradient(farthest-side, theme(colors.foreground) 90%, #0000) 0 5px/8px 8px content-box, theme(colors.background); background-repeat: no-repeat; diff --git a/src/pages/Home.jsx b/src/pages/Home.jsx index 9ef4630..0a55b66 100644 --- a/src/pages/Home.jsx +++ b/src/pages/Home.jsx @@ -7,7 +7,7 @@ function Home() { const [cursorVariant, setCursorVariant] = useState("default"); return ( -
+

this page under{" "} { + const [squares, setSquares] = useState(Array(9).fill("")); + const [isXTurn, setIsXTurn] = useState(true); + const [winner, setWinner] = useState(null); + const [isDraw, setIsDraw] = useState(false); + + function handleClick(currentSquare) { + if (winner || squares[currentSquare] || isDraw) return; + + let cpySquares = [...squares]; + cpySquares[currentSquare] = isXTurn ? "X" : "O"; + setIsXTurn(!isXTurn); + setSquares(cpySquares); + + const gameWinner = checkWinner(cpySquares); + if (gameWinner) { + setWinner(gameWinner); + } else if (!cpySquares.includes("")) { + // Check for draw if all squares are filled + setIsDraw(true); + } + } + + function checkWinner(board) { + // Check rows + for (let i = 0; i < 3; i++) { + if ( + board[i * 3] !== "" && + board[i * 3] === board[i * 3 + 1] && + board[i * 3] === board[i * 3 + 2] + ) { + return board[i * 3]; + } + console.log(board[i * 3 + 1]); + } + + // Check columns + for (let i = 0; i < 3; i++) { + if ( + board[i] !== "" && + board[i] === board[i + 3] && + board[i] === board[i + 6] + ) { + return board[i]; + } + } + + // Check diagonals + if (board[0] !== "" && board[0] === board[4] && board[0] === board[8]) { + return board[0]; + } + if (board[2] !== "" && board[2] === board[4] && board[2] === board[6]) { + return board[2]; + } + + return null; + } + + function handleRestart() { + setIsXTurn(true); + setSquares(Array(9).fill("")); + setWinner(null); + setIsDraw(false); + } + + return ( +

+
+ {winner ? ( +
+ Winner is{" "} + {winner} +
+ ) : isDraw ? ( +
It's a draw!
+ ) : null}{" "} +
+
+
+ handleClick(0)} /> + handleClick(1)} /> + handleClick(2)} /> +
+
+ handleClick(3)} /> + handleClick(4)} /> + handleClick(5)} /> +
+
+ handleClick(6)} /> + handleClick(7)} /> + handleClick(8)} /> +
+
+ +
+ {winner || isDraw ? ( + + ) : null} +
+
+ ); +}; + +export const Square = ({ value, onClick }) => { + return ( + + ); +};