-
Notifications
You must be signed in to change notification settings - Fork 6
/
script.js
executable file
·162 lines (135 loc) · 4.25 KB
/
script.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.Recall = factory();
}
}(this, function () {
var Grid = React.createClass({
handleClick: function (rowIdx, colIdx) {
this.props.onTileClick(rowIdx, colIdx);
},
render: function () {
return (
<div className="recall-grid">
{this.props.matrix.map(function (row, rowIdx) {
return (
<div key={rowIdx} className="recall-grid-row">
{row.map(function (col, colIdx) {
return (
<div key={colIdx} className={'recall-grid-tile ' + (col ? 'is-selected' : '')} onClick={this.handleClick.bind(this, rowIdx, colIdx)}></div>
);
}.bind(this))}
</div>
);
}.bind(this))}
</div>
);
}
});
var Recall = React.createClass({
start: function () {
this.numSelectedTiles = 0;
this.isDisabled = true;
this.expectedMatrix = this._generateExpectedMatrix();
this.setState({
matrix: this.expectedMatrix
});
setTimeout(function () {
this.setState({ matrix: generateSquareMatrix(this.props.size, false) });
this.isDisabled = false;
}.bind(this), 5000);
},
restart: function () {
this.setState({ userWon: null });
this.start();
},
selectTile: function (rowIdx, colIdx) {
if(this.isDisabled) return;
var clone = this.state.matrix.slice(0);
clone[rowIdx][colIdx] = true;
this.numSelectedTiles = this.numSelectedTiles + 1;
if(this.numSelectedTiles === this.props.numToChoose) this.checkAnswer();
this.setState({
matrix: clone
});
},
checkAnswer: function () {
if(this._isCorrectSelection()) {
this.setState({ userWon: true });
} else {
this.setState({ userWon: false });
}
this.isDisabled = true;
},
_isCorrectSelection: function () {
var row, col, num = this.props.size;
for(row = 0; row < num; row++) {
for(col = 0; col < num; col++) {
if(this.expectedMatrix[row][col] !== this.state.matrix[row][col]) return false;
}
}
return true;
},
_generateExpectedMatrix: function () {
var matrix = generateSquareMatrix(this.props.size, false),
numSelected = 0,
randRow = 0, randCol = 0;
while(numSelected < this.props.numToChoose) {
randRow = randomWithinRange(0, matrix.length);
randCol = randomWithinRange(0, matrix[0].length);
if(!matrix[randRow][randCol]) {
matrix[randRow][randCol] = true;
numSelected += 1;
}
}
return matrix;
},
componentDidMount: function () {
this.start();
},
getDefaultProps: function () {
return {
numToChoose: 9,
size: 5
};
},
getInitialState: function() {
return {
userWon: null,
matrix: generateSquareMatrix(this.props.size, false)
};
},
render: function () {
return (
<div className="recall">
<div className={'recall-message recall-win-message ' + (!this.state.userWon ? 'is-hidden' : '')}>
You win!
<button className="recall-restart-btn" onClick={this.restart}>Do it again!</button>
</div>
<div className={'recall-message recall-win-message ' + ((this.state.userWon === null || this.state.userWon) ? 'is-hidden' : '')}>
Whomp... Sorry
<button className="recall-restart-btn" onClick={this.restart}>Try again...</button>
</div>
<Grid matrix={this.state.matrix} onTileClick={this.selectTile} />
</div>
);
}
});
function generateSquareMatrix(num, value) {
var arr = [], row, col;
for(row = 0; row < num; row++) {
arr[row] = [];
for(col = 0; col < num; col++) {
arr[row][col] = value;
}
}
return arr;
}
function randomWithinRange(min, range) {
return min + Math.floor(Math.random() * range);
}
return Recall;
}));