File tree 1 file changed +14
-2
lines changed 1 file changed +14
-2
lines changed Original file line number Diff line number Diff line change @@ -14,12 +14,15 @@ class Game extends React.Component {
14
14
history : [ {
15
15
squares : Array ( 9 ) . fill ( null ) , // Now Board Component will not store the state of the game any more.
16
16
} ] ,
17
+ stepNumber : 0 ,
17
18
xIsNext : true ,
18
19
}
19
20
}
20
21
21
22
handleClick ( i ) { /* Due Game Component now have the squares state and Board Component only uses their props this Coponent will
22
23
* handleClicks.*/
24
+ this . history = this . state . history . slice ( 0 , this . state . stepNumber + 1 ) ;
25
+ this . current = this . history [ this . history . length - 1 ] ;
23
26
const squares = this . current . squares . slice ( ) ;
24
27
if ( this . winner || squares [ i ] ) {
25
28
return ;
@@ -29,15 +32,24 @@ class Game extends React.Component {
29
32
history : this . history . concat ( [ {
30
33
squares : squares ,
31
34
} ] ) ,
35
+ stepNumber : this . history . length ,
32
36
xIsNext : ! this . state . xIsNext ,
33
37
} ) ;
34
38
}
35
39
40
+ jumpTo ( step ) {
41
+ this . setState ( {
42
+ stepNumber : step ,
43
+ xIsNext : ( step % 2 ) === 0 ,
44
+ } ) ;
45
+ }
46
+
36
47
render ( ) {
37
48
const MAX_LEN = 9 ;
38
49
this . history = this . state . history ;
39
- this . current = this . history [ this . history . length - 1 ] ;
40
- this . winner = calculateWinner ( this . current . squares ) ; // Due Game Component now have the squares state this Component will calculateWinner.
50
+ this . current = this . history [ this . state . stepNumber ] ;
51
+ this . winner = calculateWinner ( this . current . squares ) ; /* Due Game Component now have the squares state this Component will
52
+ * calculateWinner.*/
41
53
42
54
const moves = this . history . map ( ( step , move ) => {
43
55
const desc = move ? 'Go to move #' + move : 'Go to game start' ;
You can’t perform that action at this time.
0 commit comments