Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions 07week/ticTacToe/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
77 changes: 68 additions & 9 deletions 07week/ticTacToe/script.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,90 @@
'use strict';

function Square(props) {
return (
<button className="square" onClick={props.onClick}>
{props.value}
</button>
)
}

class TicTacToe extends React.Component {
constructor(props) {
super(props);
this.state = {
squares: Array(9).fill(null),
xIsNext: true,
}
}

renderSquare(i) {
return (
<Square value={this.state.squares[i]}
onClick={() => 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 (
<div>
<div className="status">{status}</div>
{/* <button className="reset" onClick={this.reset}>Reset</button> */}
<div className="row">
<div data-cell="0"></div>
<div data-cell="1"></div>
<div data-cell="2"></div>
{this.renderSquare(0)}
{this.renderSquare(1)}
{this.renderSquare(2)}
</div>
<div className="row">
<div data-cell="3"></div>
<div data-cell="4"></div>
<div data-cell="5"></div>
{this.renderSquare(3)}
{this.renderSquare(4)}
{this.renderSquare(5)}
</div>
<div className="row">
<div data-cell="6"></div>
<div data-cell="7"></div>
<div data-cell="8"></div>
{this.renderSquare(6)}
{this.renderSquare(7)}
{this.renderSquare(8)}
</div>
</div>
);
}
}

ReactDOM.render(<TicTacToe />, 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;
}
15 changes: 14 additions & 1 deletion 07week/ticTacToe/style.css
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
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;
float: left;
border: 1px solid #808080;
font-size: 100px;
text-align: center;
outline: none;
}

.row {
Expand Down