Add: Time-Travel Function.
This commit is contained in:
90
src/App.tsx
90
src/App.tsx
@@ -5,15 +5,38 @@ interface SquareProps {
|
||||
onSquareClick: () => void;
|
||||
}
|
||||
|
||||
interface BoardProps {
|
||||
XIsNext: boolean;
|
||||
squares: Array<SquareProps["value"]>;
|
||||
onPlay: (nextSquares: Array<SquareProps["value"]>) => void;
|
||||
}
|
||||
|
||||
function Square({
|
||||
value, onSquareClick}: SquareProps) {
|
||||
return <button type="button" onClick={onSquareClick} className="square">{value}</button>
|
||||
}
|
||||
|
||||
export default function Board() {
|
||||
const [XIsNext, setXIsNext] = useState(true);
|
||||
const [squares, setSquares] = useState(Array(9).fill(null));
|
||||
function calculateWinner(squares: BoardProps["squares"]) {
|
||||
const lines = [
|
||||
[0, 1, 2],
|
||||
[3, 4, 5],
|
||||
[6, 7, 8],
|
||||
[0, 3, 6],
|
||||
[1, 4, 7],
|
||||
[2, 5, 8],
|
||||
[0, 4, 8],
|
||||
[2, 4, 6]
|
||||
];
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const [a, b, c] = lines[i];
|
||||
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
|
||||
return squares[a];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function Board({XIsNext, squares, onPlay}: BoardProps) {
|
||||
const winner = calculateWinner(squares);
|
||||
let status;
|
||||
if (winner) {
|
||||
@@ -32,8 +55,7 @@ export default function Board() {
|
||||
} else {
|
||||
nextSquares[i] = "O";
|
||||
}
|
||||
setSquares(nextSquares);
|
||||
setXIsNext(!XIsNext);
|
||||
onPlay(nextSquares)
|
||||
}
|
||||
|
||||
return(
|
||||
@@ -58,22 +80,48 @@ export default function Board() {
|
||||
)
|
||||
}
|
||||
|
||||
function calculateWinner(squares: Array<SquareProps["value"]>) {
|
||||
const lines = [
|
||||
[0, 1, 2],
|
||||
[3, 4, 5],
|
||||
[6, 7, 8],
|
||||
[0, 3, 6],
|
||||
[1, 4, 7],
|
||||
[2, 5, 8],
|
||||
[0, 4, 8],
|
||||
[2, 4, 6]
|
||||
];
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const [a, b, c] = lines[i];
|
||||
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
|
||||
return squares[a];
|
||||
export default function Game() {
|
||||
const [history, setHistory] = useState([Array(9).fill(null)]);
|
||||
const [currentMove, setCurrentMove] = useState(0);
|
||||
|
||||
const XIsNext = currentMove % 2 === 0;
|
||||
const currentSquares = history[currentMove];
|
||||
|
||||
function handlePlay(nextSquares: Array<SquareProps["value"]>) {
|
||||
const nextHistory = [...history.slice(0, currentMove + 1), nextSquares];
|
||||
|
||||
setHistory(nextHistory);
|
||||
setCurrentMove(nextHistory.length - 1);
|
||||
}
|
||||
|
||||
function jumpTo(nextMove: number) {
|
||||
setCurrentMove(nextMove);
|
||||
}
|
||||
return null;
|
||||
|
||||
const moves = history.map((_squares, move) => {
|
||||
let description;
|
||||
if (move > 0) {
|
||||
description = 'Go to move #' + move;
|
||||
} else {
|
||||
description = 'Go to game start';
|
||||
}
|
||||
return (
|
||||
<li key={move}>
|
||||
<button onClick={() => jumpTo(move)}>{description}</button>
|
||||
</li>
|
||||
);
|
||||
})
|
||||
|
||||
return (
|
||||
<div className="game">
|
||||
<div className="game-board">
|
||||
<Board XIsNext={XIsNext} squares={currentSquares} onPlay={handlePlay}/>
|
||||
</div>
|
||||
<div className="game-info">
|
||||
<ol>
|
||||
{moves}
|
||||
</ol>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import {createRoot,} from 'react-dom/client';
|
||||
import App from './App';
|
||||
import './App.css'
|
||||
|
||||
const root = createRoot(document.getElementById("root"))
|
||||
const root = createRoot(document.getElementById("root") as HTMLElement)
|
||||
|
||||
root.render(
|
||||
<StrictMode>
|
||||
|
||||
Reference in New Issue
Block a user