Skip to content

Commit

Permalink
fix game done logic
Browse files Browse the repository at this point in the history
  • Loading branch information
chenglou committed Nov 3, 2013
1 parent 1ad7527 commit 42b14f8
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions index.jsx
Expand Up @@ -77,17 +77,15 @@ var LightsOut = React.createClass({
if (j !== 0) board[i][j - 1] = !board[i][j - 1];
if (j !== board.length - 1) board[i][j + 1] = !board[i][j + 1];

var done = this.state.board.every(function(row) {
return row.every(function(cell) {
return !!cell;
});
});
// setState is asynchronous. Pass a callback that verifies if all the lights
// are on; if so, create new game
this.setState({board: this.state.board}, function() {
var done = this.state.board.every(function(row) {
return row.every(function(cell) {
return !!cell;
});
});

this.setState({board: this.state.board, done: done}, function() {
if (done) {
this.setState({done: true});
setTimeout(function() {
this.setState({
board: this.getNewRandomBoard(),
Expand Down

0 comments on commit 42b14f8

Please sign in to comment.