@@ -3,9 +3,9 @@ import "./App.css";
3
3
4
4
function App ( ) {
5
5
const NUMBER_OF_BOXES = 9 ;
6
- const [ xTurn , setXTurn ] = useState ( true )
7
- const [ xInput , setXInput ] = useState ( [ ] )
8
- const [ oInput , setOInput ] = useState ( [ ] )
6
+ const [ xTurn , setXTurn ] = useState ( true ) ;
7
+ const [ xInput , setXInput ] = useState ( [ ] ) ;
8
+ const [ oInput , setOInput ] = useState ( [ ] ) ;
9
9
10
10
const winnings = [
11
11
[ 0 , 1 , 2 ] ,
@@ -27,28 +27,53 @@ function App() {
27
27
item [ 2 ] === array [ 2 ]
28
28
) {
29
29
didWin = true ;
30
+ alert ( "win" ) ;
30
31
}
31
32
} ) ;
32
33
return didWin ;
33
34
} ;
34
35
35
36
const handleClick = ( i ) => {
36
- let tempX = [ ...xInput , i ]
37
- setXInput ( tempX )
38
- if ( tempX . length >= 3 ) {
39
- let didWin = checkWins ( tempX ) ;
40
- console . log ( "didWin" , didWin ) ;
37
+ setXTurn ( ! xTurn ) ;
38
+
39
+ if ( xTurn ) {
40
+ let tempX = [ ...xInput , i ] ;
41
+ setXInput ( tempX ) ;
42
+
43
+ if ( tempX . length >= 3 ) {
44
+ let didWin = checkWins ( tempX ) ;
45
+ console . log ( "didWin" , didWin ) ;
46
+ }
47
+ } else {
48
+ let tempO = [ ...oInput , i ] ;
49
+ setOInput ( tempO ) ;
50
+
51
+ if ( tempO . length >= 3 ) {
52
+ let didWin = checkWins ( tempO ) ;
53
+ console . log ( "didWin" , didWin ) ;
54
+ }
41
55
}
42
56
} ;
43
57
44
- console . log ( 'xInput' , xInput )
58
+ console . log ( "xInput" , xInput ) ;
59
+ console . log ( "oInput" , oInput ) ;
60
+ console . log ( "xTurn" , xTurn ) ;
61
+
62
+ const renderBoardValue = ( i ) => {
63
+ if ( xInput . includes ( i ) ) {
64
+ return "X" ;
65
+ } else if ( oInput . includes ( i ) ) {
66
+ return "0" ;
67
+ }
68
+ return "" ;
69
+ } ;
45
70
46
71
const renderBoard = ( ) => {
47
72
let arr = [ ] ;
48
73
for ( let i = 0 ; i < NUMBER_OF_BOXES ; i ++ ) {
49
74
arr . push (
50
75
< div onClick = { ( ) => handleClick ( i ) } className = "box" key = { i } >
51
- { i }
76
+ { renderBoardValue ( i ) }
52
77
</ div >
53
78
) ;
54
79
}
0 commit comments