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