diff --git a/07week/ticTacToe/script.js b/07week/ticTacToe/script.js index d0bc4bd52..ec2868982 100644 --- a/07week/ticTacToe/script.js +++ b/07week/ticTacToe/script.js @@ -1,31 +1,111 @@ 'use strict'; +class Square extends React.Component{ + render() { + return ( + + ); + } +} + class TicTacToe extends React.Component { constructor(props) { super(props); + this.state = { + //if in state no need for this.something...Player turn goes in the state because the state is changeing + // players1: null, + squares: [ + "", "", "", "", "", "", "", "", "" + ], + xNext: true, + }; } + handleClick(i){ + + const squares = this.state.squares.slice(); + if(calculateWin(squares) || squares[i]) { + return; + } + squares[i] = this.state.xNext ? 'X' : 'O'; + this.setState({ + squares: squares, + xNext: !this.state.xNext, + }) + // this.playerTurn = this.playerTurn === 'X' ? 'O' : 'X' + // return div[data-cell] = playerTurn + // console.log(this.renderSquare()); + } + renderSquare(i) { + return ( + this.handleClick(i)} + /> + ); + } render() { + const winner = calculateWin(this.state.squares); + let status; + if(winner) { + status = 'Winner: ' + winner; + } else { + status = 'Next Player: ' + (this.state.xNext ? 'X' : 'O'); + } + return ( -
+
+
{status}
-
-
-
+ {/*
*/} + {this.renderSquare(0)} + {/*
this.setState({players1: this.handleClick()})}>*/} + {/*{this.playerTurn}
*/} + {/*
*/} + {this.renderSquare(1)} + {/*
*/} + {this.renderSquare(2)}
-
-
-
+ {/*
*/} + {this.renderSquare(3)} + {/*
*/} + {this.renderSquare(4)} + {/*
*/} + {this.renderSquare(5)}
-
-
-
+ {/*
*/} + {this.renderSquare(6)} + {/*
*/} + {this.renderSquare(7)} + {/*
*/} + {this.renderSquare(8)}
+
); } } ReactDOM.render(, document.getElementById('tic-tac-toe')); + +function calculateWin(squares){ + const lines = [ + [0, 1, 2], + [3, 4, 5], + [6, 7, 8], + [0, 3, 6], + [1, 4, 7], + [2, 5, 8], + [0, 4, 8], + [6, 4, 2], + ]; + for(let i = 0; i