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
100 changes: 90 additions & 10 deletions 07week/ticTacToe/script.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,111 @@
'use strict';

class Square extends React.Component{
render() {
return (
<button className="square" onClick={() => this.props.onClick()}>{this.props.value}</button>
);
}
}

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){

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary whitespace

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 (
<Square
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice use of a render function, but remember to code DRY (don't repeat yourself).
I'd recommend instead making this a renderSquares() function that maps through this.state.squares. This way you have access to the index of each square which you can pass into this.state.squares[index] as well as this.handleClick(index).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also you've got some funky indentation here. coughCOPYPASTINGFROMTHEREACTDOCScough

value={this.state.squares[i]}
onClick={() => 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 (
<div>
<div>
<div className="status">{status}</div>
<div className="row">
<div data-cell="0"></div>
<div data-cell="1"></div>
<div data-cell="2"></div>
{/*<div data-cell="0"></div>*/}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get rid of all these

{this.renderSquare(0)}
{/*<div data-cell="0" onClick={() => this.setState({players1: this.handleClick()})}>*/}
{/*{this.playerTurn}</div>*/}
{/*<div data-cell="1"></div>*/}
{this.renderSquare(1)}
{/*<div data-cell="2"></div>*/}
{this.renderSquare(2)}
</div>
<div className="row">
<div data-cell="3"></div>
<div data-cell="4"></div>
<div data-cell="5"></div>
{/*<div data-cell="3"></div>*/}
{this.renderSquare(3)}
{/*<div data-cell="4"></div>*/}
{this.renderSquare(4)}
{/*<div data-cell="5"></div>*/}
{this.renderSquare(5)}
</div>
<div className="row">
<div data-cell="6"></div>
<div data-cell="7"></div>
<div data-cell="8"></div>
{/*<div data-cell="6"></div>*/}
{this.renderSquare(6)}
{/*<div data-cell="7"></div>*/}
{this.renderSquare(7)}
{/*<div data-cell="8"></div>*/}
{this.renderSquare(8)}
</div>

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary whitespace

</div>
);
}
}

ReactDOM.render(<TicTacToe />, document.getElementById('tic-tac-toe'));

function calculateWin(squares){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that the word function?

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 <lines.length; i ++) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a for loop? Why?

const [a, b, c] = lines[i];
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
return squares[a];
}
}
return null;
}
2 changes: 1 addition & 1 deletion 07week/ticTacToe/style.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
div[data-cell] {
.square {
width: 100px;
height: 100px;
background-color: #f2f2f2;
Expand Down