1
1
import { useState } from "react" ;
2
2
import Square from "../Square" ;
3
+ import { Button } from "antd" ;
3
4
4
5
export 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 ] ;
7
13
8
14
const calculateWinner = ( ) => {
9
15
const lines = [
@@ -21,8 +27,8 @@ export default function Board(){
21
27
console . log ( "called" ) ;
22
28
const [ a , b , c ] = lines [ i ] ;
23
29
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 ] ;
26
32
}
27
33
28
34
}
@@ -31,13 +37,15 @@ export default function Board(){
31
37
}
32
38
33
39
const handleClick = ( i : number ) => {
34
- const squaresCopy = squares . slice ( ) ;
40
+ const squaresCopy = currentState . slice ( ) ;
35
41
if ( squaresCopy [ i ] || calculateWinner ( ) ) {
36
42
return ;
37
43
}
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 ) ;
41
49
}
42
50
43
51
const status = ( ) => {
@@ -47,24 +55,41 @@ export default function Board(){
47
55
}
48
56
}
49
57
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
+
50
71
return (
51
72
< >
52
73
< 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 ) } />
56
77
</ div >
57
78
< 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 ) } />
61
82
</ div >
62
83
< 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 ) } />
66
87
</ div >
67
88
< div > { status ( ) } </ div >
89
+ < h3 > Game Moves:</ h3 >
90
+ < ol >
91
+ { moves }
92
+ </ ol >
68
93
</ >
69
94
)
70
95
}
0 commit comments