-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.jsx
117 lines (106 loc) · 3.01 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/** @jsx React.DOM */
var boards = [
[
[1,0,0,1,1],
[0,0,1,0,0],
[0,1,0,0,1],
[0,1,0,0,0],
[1,1,0,1,0]
]
];
var TransitionGroup = React.addons.TransitionGroup;
var classSet = React.addons.classSet;
var Switch = React.createClass({
render: function() {
// got the props from parent (LightsOut)
var classes = {
'switch': true,
'switch-done': this.props.done,
'switch-on': this.props.isOn,
'switch-off': !this.props.isOn
}
return <div className={classSet(classes)} onClick={this.props.onClick} />
}
});
var LightsOut = React.createClass({
getInitialState: function() {
// component's internal value(s)
return {
board: this.getNewRandomBoard(),
done: false
};
},
getNewRandomBoard: function() {
// clone a board
return boards[Math.floor(Math.random() * boards.length)].map(function(row) {
return row.map(function(cell) {
return cell;
});
});
},
handleReset: function() {
// updating the state auto re-renders the component UI
this.setState({
board: this.getNewRandomBoard(),
done: false
});
},
handleSwitchClick: function(i, j) {
var board = this.state.board;
var lastCellIndex = board.length - 1;
// flip current and ajacent switches
board[i][j] = !board[i][j];
if (i !== 0) board[i - 1][j] = !board[i - 1][j];
if (i !== board[i].length - 1) board[i + 1][j] = !board[i + 1][j];
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, done: done}, function() {
if (done) {
setTimeout(function() {
this.setState({
board: this.getNewRandomBoard(),
done: false
});
}.bind(this), 1500);
}
}.bind(this));
},
render: function() {
return (
<div>
{
this.state.board.map(function(row, i) {
return (
// `TransitionGroup` defaults to a `span` wrapper. We want a `div`
<TransitionGroup
transitionName="switch"
component={React.DOM.div}
>
{
row.map(function(cell, j) {
// 5x5 swiches. Pass some props to each one
return (
<Switch
isOn={!cell}
done={this.state.done}
onClick={this.handleSwitchClick.bind(this, i, j)}
/>
)
}, this)
}
</TransitionGroup>
)
}, this)
}
</div>
);
}
});
React.renderComponent(<LightsOut />, document.getElementById('game'))