11import { useState } from "react" ;
22import Square from "../Square" ;
3+ import { Button } from "antd" ;
34
45export default function Board ( ) {
5- const [ squares , setSquares ] = useState < Array < String | null > > ( Array ( 9 ) . fill ( null ) ) ;
6- const [ isXNext , setIsXNext ] = useState ( true ) ;
6+ const [ currentMove , setCurrentMove ] = useState ( 0 ) ;
7+
8+ const IsXNext = currentMove % 2 === 0 ;
9+
10+ const [ history , setHistory ] = useState < Array < Array < String | null > > > ( [ Array ( 9 ) . fill ( null ) ] ) ;
11+
12+ const currentState = history [ currentMove ] ;
713
814 const calculateWinner = ( ) => {
915 const lines = [
@@ -21,8 +27,8 @@ export default function Board(){
2127 console . log ( "called" ) ;
2228 const [ a , b , c ] = lines [ i ] ;
2329
24- if ( squares [ a ] && squares [ a ] === squares [ b ] && squares [ a ] === squares [ c ] ) {
25- return squares [ a ] ;
30+ if ( currentState [ a ] && currentState [ a ] === currentState [ b ] && currentState [ a ] === currentState [ c ] ) {
31+ return currentState [ a ] ;
2632 }
2733
2834 }
@@ -31,13 +37,15 @@ export default function Board(){
3137 }
3238
3339 const handleClick = ( i : number ) => {
34- const squaresCopy = squares . slice ( ) ;
40+ const squaresCopy = currentState . slice ( ) ;
3541 if ( squaresCopy [ i ] || calculateWinner ( ) ) {
3642 return ;
3743 }
38- squaresCopy [ i ] = isXNext ? 'X' : 'O' ;
39- setSquares ( squaresCopy ) ;
40- setIsXNext ( ! isXNext ) ;
44+ squaresCopy [ i ] = IsXNext ? 'X' : 'O' ;
45+ const currentHistory = history . slice ( 0 , currentMove + 1 ) ;
46+ currentHistory . push ( squaresCopy ) ;
47+ setHistory ( currentHistory ) ;
48+ setCurrentMove ( currentMove + 1 ) ;
4149 }
4250
4351 const status = ( ) => {
@@ -47,24 +55,41 @@ export default function Board(){
4755 }
4856 }
4957
58+ const jumpTo = ( step : number ) => {
59+ setCurrentMove ( step ) ;
60+ }
61+
62+ const moves = history . map ( ( squares , move ) => {
63+ const desc = move ? "Go to move #" + move : "Go to game start" ;
64+ return (
65+ < li key = { move } >
66+ < Button onClick = { ( ) => jumpTo ( move ) } > { desc } </ Button >
67+ </ li >
68+ )
69+ } )
70+
5071 return (
5172 < >
5273 < div className = 'board-row' >
53- < Square value = { squares [ 0 ] } OnSquareClicked = { ( ) => handleClick ( 0 ) } />
54- < Square value = { squares [ 1 ] } OnSquareClicked = { ( ) => handleClick ( 1 ) } />
55- < Square value = { squares [ 2 ] } OnSquareClicked = { ( ) => handleClick ( 2 ) } />
74+ < Square value = { currentState [ 0 ] } OnSquareClicked = { ( ) => handleClick ( 0 ) } />
75+ < Square value = { currentState [ 1 ] } OnSquareClicked = { ( ) => handleClick ( 1 ) } />
76+ < Square value = { currentState [ 2 ] } OnSquareClicked = { ( ) => handleClick ( 2 ) } />
5677 </ div >
5778 < div className = 'board-row' >
58- < Square value = { squares [ 3 ] } OnSquareClicked = { ( ) => handleClick ( 3 ) } />
59- < Square value = { squares [ 4 ] } OnSquareClicked = { ( ) => handleClick ( 4 ) } />
60- < Square value = { squares [ 5 ] } OnSquareClicked = { ( ) => handleClick ( 5 ) } />
79+ < Square value = { currentState [ 3 ] } OnSquareClicked = { ( ) => handleClick ( 3 ) } />
80+ < Square value = { currentState [ 4 ] } OnSquareClicked = { ( ) => handleClick ( 4 ) } />
81+ < Square value = { currentState [ 5 ] } OnSquareClicked = { ( ) => handleClick ( 5 ) } />
6182 </ div >
6283 < div className = 'board-row' >
63- < Square value = { squares [ 6 ] } OnSquareClicked = { ( ) => handleClick ( 6 ) } />
64- < Square value = { squares [ 7 ] } OnSquareClicked = { ( ) => handleClick ( 7 ) } />
65- < Square value = { squares [ 8 ] } OnSquareClicked = { ( ) => handleClick ( 8 ) } />
84+ < Square value = { currentState [ 6 ] } OnSquareClicked = { ( ) => handleClick ( 6 ) } />
85+ < Square value = { currentState [ 7 ] } OnSquareClicked = { ( ) => handleClick ( 7 ) } />
86+ < Square value = { currentState [ 8 ] } OnSquareClicked = { ( ) => handleClick ( 8 ) } />
6687 </ div >
6788 < div > { status ( ) } </ div >
89+ < h3 > Game Moves:</ h3 >
90+ < ol >
91+ { moves }
92+ </ ol >
6893 </ >
6994 )
7095}
0 commit comments