diff --git a/src/App.tsx b/src/App.tsx index 0da7d98..d8e1ce4 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,43 +1,16 @@ -import { useState } from 'react' -import reactLogo from './assets/react.svg' -import viteLogo from './assets/vite.svg' -import heroImg from './assets/hero.png' -import './App.css' +import React from 'react'; +import TicTacToe from './components/TicTacToe'; -// Import the tic-tac-toe game component -import TicTacToe from './components/TicTacToe' - -function App() { - const [count, setCount] = useState(0) +const App: React.FC = () => { + const isPublishedPage = window.location.pathname === '/published'; return ( - <> -
-
- - React logo - Vite logo -
-
-

Get started

-

- Edit src/App.tsx and save to test HMR -

-
- -
- -
- - {/* Add the tic-tac-toe game component */} - - - ) -} +
+ {isPublishedPage && } +

Get started

+ {/* Other components */} +
+ ); +}; -export default App +export default App; diff --git a/src/components/TicTacToe.tsx b/src/components/TicTacToe.tsx index 69e17d8..5349499 100644 --- a/src/components/TicTacToe.tsx +++ b/src/components/TicTacToe.tsx @@ -1,66 +1,52 @@ -import { useState } from 'react'; +import React, { useState } from 'react'; -function TicTacToe() { - const [board, setBoard] = useState(Array(9).fill(null)); - const [currentPlayer, setCurrentPlayer] = useState('X'); - - const handleClick = (index: number) => { - if (board[index] === null) { - const newBoard = [...board]; - newBoard[index] = currentPlayer; - setBoard(newBoard); - setCurrentPlayer(currentPlayer === 'X' ? 'O' : 'X'); - } - }; - - const checkWinner = () => { - const winningLines = [ - [0, 1, 2], - [3, 4, 5], - [6, 7, 8], - [0, 3, 6], - [1, 4, 7], - [2, 5, 8], - [0, 4, 8], - [2, 4, 6], - ]; +interface Square { + value: 'X' | 'O' | null; +} - for (let i = 0; i < winningLines.length; i++) { - const [a, b, c] = winningLines[i]; - if (board[a] && board[a] === board[b] && board[a] === board[c]) { - return board[a]; - } - } +const TicTacToe: React.FC = () => { + const [squares, setSquares] = useState(Array(9).fill({ value: null })); + const [isXNext, setIsXNext] = useState(true); - if (board.every((square) => square !== null)) { - return 'tie'; + const handleClick = (index: number) => { + if (squares[index].value === null) { + const newSquares = [...squares]; + newSquares[index] = { value: isXNext ? 'X' : 'O' }; + setSquares(newSquares); + setIsXNext(!isXNext); } - - return null; }; - const winner = checkWinner(); + const renderSquare = (index: number) => ( +
handleClick(index)} + > + {squares[index].value} +
+ ); return ( -
-
- {board.map((square, index) => ( -
handleClick(index)} - > - {square} -
- ))} -
- {winner && ( -
- {winner === 'tie' ? 'It\'s a tie!' : `Player ${winner} wins!`} +
+
+
+ {renderSquare(0)} + {renderSquare(1)} + {renderSquare(2)}
- )} +
+ {renderSquare(3)} + {renderSquare(4)} + {renderSquare(5)} +
+
+ {renderSquare(6)} + {renderSquare(7)} + {renderSquare(8)} +
+
); -} +}; export default TicTacToe;