@@ -36,6 +36,7 @@ class Game extends React.Component {
36
36
}
37
37
38
38
jumpTo ( step ) {
39
+ this . winner = null ;
39
40
this . setState ( {
40
41
stepNumber : step ,
41
42
xIsNext : ( step % 2 ) === 0 ,
@@ -44,9 +45,10 @@ class Game extends React.Component {
44
45
45
46
render ( ) {
46
47
const MAX_LEN = 9 ;
48
+ let winnerResult ;
47
49
this . history = this . state . history ;
48
50
this . current = this . history [ this . state . stepNumber ] ;
49
- this . winner = calculateWinner ( this . current . squares ) ;
51
+ winnerResult = calculateWinner ( this . current . squares ) ;
50
52
51
53
const moves = this . history . map ( ( step , move ) => {
52
54
const desc = move ? 'Go to move #' + move : 'Go to game start' ;
@@ -57,7 +59,8 @@ class Game extends React.Component {
57
59
) ;
58
60
} ) ;
59
61
let status ;
60
- if ( this . winner ) {
62
+ if ( winnerResult ) {
63
+ this . winner = winnerResult . shift ( ) ;
61
64
status = 'Winner: ' + this . winner ;
62
65
} else if ( this . history . length - 1 === MAX_LEN ) {
63
66
status = 'Draw...' ;
@@ -70,6 +73,7 @@ class Game extends React.Component {
70
73
< div className = "game-board" >
71
74
< Board
72
75
squares = { this . current . squares }
76
+ winnerLine = { winnerResult }
73
77
onClick = { ( i ) => this . handleClick ( i ) }
74
78
/>
75
79
</ div >
@@ -87,7 +91,8 @@ class Board extends React.Component {
87
91
88
92
renderSquare ( i ) {
89
93
return (
90
- < Square
94
+ < Square
95
+ winner = { isWinner ( i , this . props . winnerLine ) }
91
96
value = { this . props . squares [ i ] }
92
97
onClick = { ( ) => this . props . onClick ( i ) }
93
98
/>
@@ -118,14 +123,19 @@ class Board extends React.Component {
118
123
}
119
124
120
125
function Square ( props ) {
126
+ var liClasses = "square"
127
+
128
+ if ( props . winner ) { liClasses = liClasses . concat ( " winner" ) }
129
+
121
130
return (
122
- < button className = "square" onClick = { props . onClick } >
131
+ < button className = { liClasses } onClick = { props . onClick } >
123
132
{ props . value }
124
133
</ button >
125
134
) ;
126
135
}
127
136
128
137
function calculateWinner ( squares ) {
138
+ const winnerLine = [ ] ;
129
139
const lines = [
130
140
[ 0 , 1 , 2 ] ,
131
141
[ 3 , 4 , 5 ] ,
@@ -139,12 +149,21 @@ function calculateWinner(squares) {
139
149
for ( let i = 0 ; i < lines . length ; i ++ ) {
140
150
const [ a , b , c ] = lines [ i ] ;
141
151
if ( squares [ a ] && squares [ a ] === squares [ b ] && squares [ a ] === squares [ c ] ) {
142
- return squares [ a ] ;
152
+ winnerLine . push ( a , b , c ) ;
153
+ return [ squares [ a ] , winnerLine ] ;
143
154
}
144
155
}
145
156
return null ;
146
157
}
147
158
159
+ function isWinner ( i , winnerLine ) {
160
+ if ( ! winnerLine ) { return ; }
161
+ return i === winnerLine [ 0 ] [ 0 ] ||
162
+ i === winnerLine [ 0 ] [ 1 ] ||
163
+ i === winnerLine [ 0 ] [ 2 ] ;
164
+
165
+ }
166
+
148
167
// ==================================
149
168
150
169
ReactDOM . render (
0 commit comments