From ce1fd6ca5eed2876e8234e39d8dbe8c667ac3ac9 Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Thu, 21 Feb 2019 16:39:48 -0600 Subject: [PATCH 1/3] clueless --- 07week/ticTacToe/package.json | 3 +++ 07week/ticTacToe/script.js | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 07week/ticTacToe/package.json diff --git a/07week/ticTacToe/package.json b/07week/ticTacToe/package.json new file mode 100644 index 000000000..0db3279e4 --- /dev/null +++ b/07week/ticTacToe/package.json @@ -0,0 +1,3 @@ +{ + +} diff --git a/07week/ticTacToe/script.js b/07week/ticTacToe/script.js index d0bc4bd52..ab2b214bc 100644 --- a/07week/ticTacToe/script.js +++ b/07week/ticTacToe/script.js @@ -3,8 +3,12 @@ class TicTacToe extends React.Component { constructor(props) { super(props); + this.state = { + squares: Array(9).fill(null), + xIsNext: true, + } } - + render() { return (
@@ -29,3 +33,4 @@ class TicTacToe extends React.Component { } ReactDOM.render(, document.getElementById('tic-tac-toe')); + From fd6a59bf16340ac5907e9554d64a8036dbf07e28 Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Thu, 21 Feb 2019 18:32:46 -0600 Subject: [PATCH 2/3] confused 2 --- 07week/ticTacToe/script.js | 1 + 1 file changed, 1 insertion(+) diff --git a/07week/ticTacToe/script.js b/07week/ticTacToe/script.js index ab2b214bc..a905435ab 100644 --- a/07week/ticTacToe/script.js +++ b/07week/ticTacToe/script.js @@ -5,6 +5,7 @@ class TicTacToe extends React.Component { super(props); this.state = { squares: Array(9).fill(null), + value: null, xIsNext: true, } } From a491bc7d0e9fc1b349bc901035c4e482f0536035 Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Fri, 22 Feb 2019 04:02:33 -0600 Subject: [PATCH 3/3] did it similar to react tutorial's approach. no reset or history storage yet --- 07week/ticTacToe/script.js | 73 ++++++++++++++++++++++++++++++++------ 07week/ticTacToe/style.css | 15 +++++++- 2 files changed, 77 insertions(+), 11 deletions(-) diff --git a/07week/ticTacToe/script.js b/07week/ticTacToe/script.js index a905435ab..d49480e75 100644 --- a/07week/ticTacToe/script.js +++ b/07week/ticTacToe/script.js @@ -1,32 +1,66 @@ 'use strict'; +function Square(props) { + return ( + + ) +} + class TicTacToe extends React.Component { constructor(props) { super(props); this.state = { squares: Array(9).fill(null), - value: null, xIsNext: true, } } + + renderSquare(i) { + return ( + this.handleClick(i)} + /> + ) + } + + handleClick(i) { + const squares = this.state.squares.slice(); + + if( calculateWinner(squares) || squares[i] ) return; + + squares[i] = this.state.xIsNext ? 'X' : 'O'; + this.setState({ + squares: squares, + xIsNext: !this.state.xIsNext, + }); + } render() { + + let winner = calculateWinner(this.state.squares); + let status; + if(winner) status = 'Winner: ' + winner; + else status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O') return (
+
{status}
+ {/* */}
-
-
-
+ {this.renderSquare(0)} + {this.renderSquare(1)} + {this.renderSquare(2)}
-
-
-
+ {this.renderSquare(3)} + {this.renderSquare(4)} + {this.renderSquare(5)}
-
-
-
+ {this.renderSquare(6)} + {this.renderSquare(7)} + {this.renderSquare(8)}
); @@ -35,3 +69,22 @@ class TicTacToe extends React.Component { ReactDOM.render(, document.getElementById('tic-tac-toe')); +function calculateWinner(squares) { + const lines = [ + [0, 1, 2], + [3, 4, 5], + [6, 7, 8], + [0, 3, 6], + [1, 4, 7], + [2, 5, 8], + [0, 4, 8], + [2, 4, 6] + ]; + for( let i = 0; i < lines.length; i++ ) { + const [a, b, c] = lines[i]; + if( squares[a] && squares[a] === squares[b] && squares[a] === squares[c] ) { + return squares[a]; + } + } + return null; +} \ No newline at end of file diff --git a/07week/ticTacToe/style.css b/07week/ticTacToe/style.css index d550b6372..3d12600af 100644 --- a/07week/ticTacToe/style.css +++ b/07week/ticTacToe/style.css @@ -1,4 +1,16 @@ -div[data-cell] { +body { + font-family: Arial, Helvetica, sans-serif; +} + +.status { + width: 300px; + text-align: center; + margin-bottom: 5px; + font-size: 20pt; + font-weight: 600; +} + +.square { width: 100px; height: 100px; background-color: #f2f2f2; @@ -6,6 +18,7 @@ div[data-cell] { border: 1px solid #808080; font-size: 100px; text-align: center; + outline: none; } .row {