@@ -19037,9 +19037,18 @@ var TwoDots;
19037
19037
return colors[getRandomInt(0, colors.length)];
19038
19038
};
19039
19039
TwoDots.colors = ['red', 'yellow', 'brown', 'blue', 'green'];
19040
+ function shuffleArray(array) {
19041
+ for (var i = array.length - 1; i > 0; i--) {
19042
+ var j = Math.floor(Math.random() * (i + 1));
19043
+ var temp = array[i];
19044
+ array[i] = array[j];
19045
+ array[j] = temp;
19046
+ }
19047
+ return array;
19048
+ }
19049
+ TwoDots.shuffleArray = shuffleArray;
19040
19050
var Cell = (function () {
19041
19051
function Cell(x, y) {
19042
- this.className = 'unselected';
19043
19052
this.color = getRandomColor(TwoDots.colors);
19044
19053
this.x = x;
19045
19054
this.y = y;
@@ -19058,14 +19067,13 @@ var TwoDots;
19058
19067
var TwoDotsState = (function () {
19059
19068
function TwoDotsState(width, height) {
19060
19069
var _this = this;
19061
- if (width === void 0) { width = 10 ; }
19062
- if (height === void 0) { height = 10 ; }
19070
+ if (width === void 0) { width = 5 ; }
19071
+ if (height === void 0) { height = 5 ; }
19063
19072
this.width = width;
19064
19073
this.height = height;
19065
19074
this.Rules = new Rules();
19066
19075
this.mode = 'board';
19067
19076
this.Grid = [];
19068
- this.startDrag = false;
19069
19077
this.turns = 0;
19070
19078
this.score = TwoDots.colors.reduce(function (total, color) {
19071
19079
total[color] = 0;
@@ -19101,8 +19109,39 @@ var Hello = React.createClass({displayName: "Hello",
19101
19109
path: [],
19102
19110
getInitialState: function () {
19103
19111
return new TwoDotsState_1.TwoDots.TwoDotsState(Number(this.props.width), Number(this.props.height));
19112
+ while (this.needsShuffling()) {
19113
+ this.shuffleBoard();
19114
+ }
19104
19115
},
19105
19116
needsShuffling: function () {
19117
+ var thisFlatArray = this.thisArray();
19118
+ for (var cell in thisFlatArray) {
19119
+ if (thisFlatArray.filter(function (c) {
19120
+ return !(c.x == thisFlatArray[cell].x && c.y == thisFlatArray[cell].y)
19121
+ && (Math.abs(c.x - thisFlatArray[cell].x) + Math.abs(c.y - thisFlatArray[cell].y) < 2)
19122
+ && thisFlatArray[cell].color == c.color;
19123
+ }).length > 0) {
19124
+ console.log('does not need shuffling');
19125
+ this.state.mode = 'board';
19126
+ return false;
19127
+ }
19128
+ }
19129
+ console.log('needs shuffling');
19130
+ this.state.mode = 'board needsShuffling';
19131
+ this.setState(this.state);
19132
+ return true;
19133
+ },
19134
+ shuffleBoard: function () {
19135
+ var thisFlatArray = TwoDotsState_1.TwoDots.shuffleArray(this.thisArray());
19136
+ for (var x = 0; x < this.state.width; x++) {
19137
+ for (var y = 0; y < this.state.height; y++) {
19138
+ this.state.Grid[y][x] = thisFlatArray.pop();
19139
+ this.state.Grid[y][x].x = x;
19140
+ this.state.Grid[y][x].y = y;
19141
+ }
19142
+ }
19143
+ this.setState(this.state);
19144
+ this.needsShuffling();
19106
19145
},
19107
19146
thisArray: function () {
19108
19147
return [].concat.apply([], this.state.Grid);
@@ -19149,7 +19188,7 @@ var Hello = React.createClass({displayName: "Hello",
19149
19188
checkResults: function (state) {
19150
19189
//have we lost?
19151
19190
if (state.Rules.maxTurns < state.turns) {
19152
- this.state.mode = 'message';
19191
+ this.state.mode = 'board message';
19153
19192
this.state.message = 'You lost!!!';
19154
19193
this.setState(this.state);
19155
19194
return;
@@ -19158,19 +19197,18 @@ var Hello = React.createClass({displayName: "Hello",
19158
19197
if (Object.keys(state.Rules.amountToCollect).filter(function (key, i) {
19159
19198
return state.Rules.amountToCollect[key] > state.score[key];
19160
19199
}).length == 0) {
19161
- this.state.mode = 'message';
19200
+ this.state.mode = 'board message';
19162
19201
this.state.message = 'You won!!!';
19163
19202
this.setState(this.state);
19164
19203
}
19165
19204
},
19166
19205
onMouseLeave: function () {
19167
- this.state.startDrag = false;
19168
19206
this.path = [];
19169
19207
this.setState(this.state);
19170
19208
},
19171
19209
handleMouseUp: function () {
19172
- this.state.startDrag = false;
19173
19210
this.removeDots(this.state);
19211
+ this.needsShuffling();
19174
19212
this.path = [];
19175
19213
this.setState(this.state);
19176
19214
this.checkResults(this.state);
@@ -19215,15 +19253,12 @@ var Hello = React.createClass({displayName: "Hello",
19215
19253
this.state.mode = 'board';
19216
19254
this.setState(this.state);
19217
19255
},
19218
- updateLevel: function (width, height, MaxTurns, limits) {
19219
- var newState = new TwoDotsState_1.TwoDots.TwoDotsState(Number(width), Number(height));
19220
- newState.Rules.maxTurns = Number(MaxTurns);
19221
- newState.Rules.amountToCollect = limits;
19256
+ updateLevel: function (newState) {
19222
19257
newState.mode = 'board';
19223
19258
this.setState(newState);
19224
19259
},
19225
19260
startNew: function () {
19226
- var newState = new TwoDotsState_1.TwoDots.TwoDotsState();
19261
+ var newState = new TwoDotsState_1.TwoDots.TwoDotsState(Number(this.state.width), Number(this.state.height) );
19227
19262
newState.mode = 'board';
19228
19263
this.setState(newState);
19229
19264
},
@@ -19232,17 +19267,30 @@ var Hello = React.createClass({displayName: "Hello",
19232
19267
var isLoop = this.isLoop();
19233
19268
var lastColor = this.path.length > 0 ? this.path[this.path.length - 1].color : undefined;
19234
19269
var state = this.state;
19270
+ var message;
19235
19271
var body;
19236
- if (this.state.mode == 'editor') {
19272
+ if (this.state.mode.indexOf('message') > -1) {
19273
+ message = React.createElement("section", null,
19274
+ React.createElement("h1", null, this.state.message),
19275
+ React.createElement("button", {className: "btn btn-danger center", onClick: this.startNew}, "Start new")
19276
+ );
19277
+ }
19278
+ if (this.state.mode.indexOf('needsShuffling') > -1) {
19279
+ message = React.createElement("section", null,
19280
+ React.createElement("h1", null, "Needs shuffling"),
19281
+ React.createElement("button", {className: "btn btn-danger center", onClick: this.shuffleBoard}, "Shuffle")
19282
+ );
19283
+ }
19284
+ if (this.state.mode.indexOf('editor') > -1) {
19237
19285
body = React.createElement("div", null,
19238
19286
React.createElement("button", {onClick: this.ShowBoard, className: "btn btn-info"}, "Show board"),
19239
- React.createElement(levelEditor_1.default, {gridState: state, rules: state.Rules, score: state.score, updateLevel: this.updateLevel})
19287
+ React.createElement(levelEditor_1.default, {gridState: state, updateLevel: this.updateLevel})
19240
19288
);
19241
19289
}
19242
- else if (this.state.mode == 'board') {
19290
+ if (this.state.mode.indexOf( 'board') > -1 ) {
19243
19291
body = React.createElement("div", null,
19244
19292
React.createElement("button", {onClick: this.ShowLevelEditor, className: "btn btn-info"}, "Level editor"),
19245
- React.createElement(scoreTable_1.default, {turns: state.turns, maxTurns: state.Rules.maxTurns, rules: state.Rules, score: state.score}),
19293
+ React.createElement(scoreTable_1.default, {turns: state.turns, maxTurns: state.Rules.maxTurns, rules: state.Rules, score: state.score}),
19246
19294
19247
19295
React.createElement("table", {className: "mainGrid", onMouseLeave: this.onMouseLeave},
19248
19296
React.createElement("tbody", null,
@@ -19262,23 +19310,18 @@ var Hello = React.createClass({displayName: "Hello",
19262
19310
);
19263
19311
})
19264
19312
)
19265
- )
19266
- );
19267
- }
19268
- else if (this.state.mode == 'message') {
19269
- body = React.createElement("section", null,
19270
- React.createElement("h1", null, this.state.message),
19271
- React.createElement("button", {className: "btn btn-danger", onClick: this.startNew}, "Start new")
19313
+ ),
19314
+ message
19272
19315
);
19273
19316
}
19274
- return React.createElement("div", null ,
19317
+ return React.createElement("div", {className: "shell"} ,
19275
19318
19276
19319
body
19277
19320
19278
19321
);
19279
19322
}
19280
19323
});
19281
- ReactDOM.render(React.createElement(Hello, {name: "World", width: "10 ", height: "8 "}), document.getElementById('container'));
19324
+ ReactDOM.render(React.createElement(Hello, {name: "World", width: "3 ", height: "3 "}), document.getElementById('container'));
19282
19325
19283
19326
},{"./TwoDotsState":159,"./levelEditor":161,"./scoreTable":162,"react":158,"react-dom":2}],161:[function(require,module,exports){
19284
19327
var __extends = (this && this.__extends) || function (d, b) {
@@ -19293,66 +19336,78 @@ var LevelEditor = (function (_super) {
19293
19336
__extends(LevelEditor, _super);
19294
19337
function LevelEditor(props) {
19295
19338
_super.call(this, props);
19296
- var rules = this.props.rules;
19297
19339
var state = this.props.gridState;
19298
- var amountToCollect = rules.amountToCollect;
19299
19340
this.changed = this.changed.bind(this);
19300
19341
this.update = this.update.bind(this);
19301
19342
this.changedcolorRules = this.changedcolorRules.bind(this);
19302
- this.state = {
19303
- amountToCollect: amountToCollect,
19304
- width: state.width,
19305
- height: state.height,
19306
- MaxTurns: rules.maxTurns };
19343
+ this.changeColor = this.changeColor.bind(this);
19344
+ this.state = state;
19307
19345
}
19308
19346
LevelEditor.prototype.changedcolorRules = function () {
19309
- var limits = {} ;
19347
+ var _this = this ;
19310
19348
var colorLimit = this.refs['colorRules'].value;
19311
19349
TwoDotsState_1.TwoDots.colors.slice(0, Number(colorLimit)).map(function (color) {
19312
- limits [color] = 5;
19350
+ _this.state.Rules.amountToCollect [color] = 5;
19313
19351
});
19314
- this.setState({ amountToCollect: limits } );
19352
+ this.setState(this.state );
19315
19353
};
19316
19354
LevelEditor.prototype.changed = function () {
19317
19355
var width = (this.refs['width']).value;
19318
19356
var height = this.refs['height'].value;
19319
19357
var MaxTurns = this.refs['MaxTurns'].value;
19320
- var limits = {};
19321
- for (var color in this.state.amountToCollect) {
19322
- limits[color] = this.refs[color].value;
19358
+ var newState = new TwoDotsState_1.TwoDots.TwoDotsState(Number(width), Number(height));
19359
+ newState.Rules.maxTurns = Number(MaxTurns);
19360
+ for (var color in this.state.Rules.amountToCollect) {
19361
+ newState.Rules.amountToCollect[color] = Number(this.refs[color].value);
19323
19362
}
19324
- this.setState({
19325
- width: width,
19326
- height: height,
19327
- MaxTurns: MaxTurns,
19328
- amountToCollect: limits
19329
- } );
19363
+ this.setState(newState);
19364
+ };
19365
+ LevelEditor.prototype.changeColor = function (row, coll) {
19366
+ var nextColor = (TwoDotsState_1.TwoDots.colors.indexOf(this.state.Grid[row][coll].color) + 1) % TwoDotsState_1.TwoDots.colors.length;
19367
+ this.state.Grid[row][coll].color = TwoDotsState_1.TwoDots.colors[nextColor];
19368
+ this.setState(this.state );
19330
19369
};
19331
19370
LevelEditor.prototype.update = function () {
19332
- var width = (this.refs['width']).value;
19333
- var height = this.refs['height'].value;
19334
- var MaxTurns = this.refs['MaxTurns'].value;
19335
- var limits = {};
19336
- for (var color in this.state.amountToCollect) {
19337
- limits[color] = this.refs[color].value;
19338
- }
19339
19371
var updateLevel = this.props.updateLevel;
19340
- updateLevel(width, height, MaxTurns, limits );
19372
+ updateLevel(this.state );
19341
19373
};
19342
19374
LevelEditor.prototype.render = function () {
19343
- var colorRules = Object.keys(this.state.amountToCollect).length.toString();
19344
- var limits = this.state.amountToCollect;
19375
+ var _this = this;
19376
+ var colorRules = Object.keys(this.state.Rules.amountToCollect).length.toString();
19377
+ var limits = this.state.Rules.amountToCollect;
19345
19378
var width = this.state.width;
19346
19379
var height = this.state.height;
19347
- var MaxTurns = this.state.MaxTurns ;
19380
+ var MaxTurns = this.state.Rules.maxTurns ;
19348
19381
var selectedColors = Object.keys(limits);
19349
19382
var colorRows = [];
19350
19383
for (var c in selectedColors) {
19351
19384
colorRows.push(React.createElement("tr", {key: selectedColors[c]}, React.createElement("td", {className: "short"}, React.createElement("div", {className: selectedColors[c] + ' cell'})), React.createElement("td", {className: "short2"},
19352
19385
React.createElement("input", {className: "short2", onChange: this.changed, type: "text", ref: selectedColors[c], value: limits[selectedColors[c]]}), " ")));
19353
19386
}
19387
+ var body;
19388
+ var state = this.state;
19389
+ body = React.createElement("div", null,
19390
+
19391
+
19392
+ React.createElement("table", {className: "mainGrid"},
19393
+ React.createElement("tbody", null,
19394
+ Array.apply(0, Array(state.height)).map(function (el, row) {
19395
+ return React.createElement("tr", {className: "border", key: row},
19396
+
19397
+ Array.apply(0, Array(state.width)).map(function (el1, coll) {
19398
+ return React.createElement("td", {key: coll, className: "unselected", onClick: _this.changeColor.bind(null, row, coll)},
19399
+
19400
+ React.createElement("div", {className: state.Grid[row][coll].color + ' cell'})
19401
+ );
19402
+ })
19403
+ );
19404
+ })
19405
+ )
19406
+ )
19407
+
19408
+ );
19354
19409
return React.createElement("div", null,
19355
- React.createElement("div", {className: "levelEditor h4"}, "Level editor"),
19410
+ React.createElement("div", {className: "h4"}, "Level editor"),
19356
19411
19357
19412
React.createElement("div", null,
19358
19413
@@ -19374,6 +19429,7 @@ var LevelEditor = (function (_super) {
19374
19429
)
19375
19430
),
19376
19431
React.createElement("br", null),
19432
+ body,
19377
19433
React.createElement("button", {onClick: this.update, className: "btn"}, "Update")
19378
19434
);
19379
19435
};
@@ -19409,9 +19465,9 @@ var ScoreTable = (function (_super) {
19409
19465
//var short = key + " short"
19410
19466
var key2 = key + "value";
19411
19467
cells.push(React.createElement("td", {className: "short", key: key}, React.createElement("div", {className: key + ' cell'})));
19412
- cells.push(React.createElement("td", {className: " short2" , key: key2}, score[key] + ' of ' + rules.amountToCollect[key]));
19468
+ cells.push(React.createElement("td", {className: ' short2 ' + (score[key] >= rules.amountToCollect[key] ? 'completedColor' : '') , key: key2}, score[key] + ' / ' + rules.amountToCollect[key]));
19413
19469
});
19414
- cells.push(React.createElement("td", {className: "short3", key: "turns"}, " ", 'Turns ' + turns + ' of ' + maxTurns, " "));
19470
+ cells.push(React.createElement("td", {className: "short3", key: "turns"}, " ", 'Turns ' + turns + ' / ' + maxTurns, " "));
19415
19471
return (React.createElement("table", {className: "scoreTable"}, React.createElement("tbody", null, React.createElement("tr", null,
19416
19472
cells
19417
19473
))));
0 commit comments