-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
272 lines (251 loc) · 8.78 KB
/
server.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
var express = require('express'),
app = express.createServer(express.static(__dirname)),
io = require('socket.io').listen(app);
io.set('log level', 1);
app.listen(8080);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/laser.html');
});
var direction = {
'left' : [0, -1],
'right' : [0, 1],
'up' : [-1, 0],
'down' : [1, 0]
};
var directionName = function(arr){
for (var x in direction){
if (direction[x][0] == arr[0] && direction[x][1] == arr[1]){
return x;
}
}
return false;
};
var getPerps = function(direction){
if(direction[0] === 0){
return ["up","down"];
} else {
return ["right","left"];
}
};
var mirrorTypes = ['lu', 'ru', 'rd', 'ld', 'lu'];
var mirrorTypesExpanded = [['left','up'],['right','up'],['right','down'],['left','down'],['left','up']];
var Mirror = function(arg){ //ALWAYS left/right then up/down
d1 = arg[0];
d2 = arg[1];
this.kind = 'mirror';
if (d1 == 'down' || d1 == 'up'){
this.name = 'mirror-'+d2[0]+d1[0];
this.rotate = mirrorTypesExpanded[mirrorTypes.indexOf(d2[0]+d1[0])+1];
} else if (d2 == 'down' || d2 == 'up'){
this.name = 'mirror-'+d1[0]+d2[0];
this.rotate = mirrorTypesExpanded[mirrorTypes.indexOf(d1[0]+d2[0])+1];
} else {
throw "bad input to Mirror";
}
this.from = {
'up':null,
'down':null,
'left':null,
'right':null
};
this.from[d1] = direction[d2];
this.from[d2] = direction[d1];
};
var getSolvableGame = function(size, numBlocks){
var attempts = 5; // with a given number of blocks before trying numBlocks - 1
console.log('starting to look for a solvable game for ', numBlocks, ' blocks');
for (var attempt = 0; attempt < attempts; attempt++){
g = new Game(size);
for (var i = 0; i < numBlocks; i++){
g.addTile('block');
}
var maxMirrors = 10;
for (var i = 0; i < maxMirrors; i++){
if (g.isValidBoard(i)){
console.log('required mirrors:', i);
for (var m = 0; m < i; m++){
g.addTile(new Mirror(['right', 'up']));
}
return g;
}
}
console.log('game thrown out, trying another');
}
console.log('could not find solvable game with ', numBlocks, 'blocks');
return getSolvableGame(size, numBlocks-1);
}
var Game = function(size){
// board is an array where each array therein is a row, and each item in such inner arrays is a column,
// making a grid of squares that can have stuff in them
this.boardRows = this.boardColumns = size;
this.board = [];
this.win = false;
for(i=0;i<this.boardRows;i++){
this.board.push([]);
for(j=0;j<this.boardColumns;j++){
this.board[i].push("empty");
}
}
this.board[0][0] = "laser";
this.board[this.boardRows - 1][this.boardColumns - 1] = "bullseye";
};
Game.prototype = {
findTile: function(type){
for(i=0;i<this.boardRows;i++){
for(j=0;j<this.boardColumns;j++){
if(this.board[i][j] == type){
return [i,j];
}
}
}
return false;
},
addTile: function(tile){
if (!this.findTile('empty')){return;}
while (true){
tryRow = Math.floor(Math.random() * this.boardRows);
tryColumn = Math.floor(Math.random() * this.boardColumns);
if (this.board[tryRow][tryColumn] == 'empty'){
this.board[tryRow][tryColumn] = tile;
break
}
}
},
toJson: function(){
var simpleBoard = [];
for(i=0;i<this.boardRows;i++){
simpleBoard.push([]);
for(j=0;j<this.boardColumns;j++){
var tile = this.board[i][j];
if (typeof tile == 'string'){
simpleBoard[i].push(tile);
} else {
simpleBoard[i].push(tile.name);
}
}
}
result = {
'laserPath': this.calcLaser(),
'board': simpleBoard,
'win' : this.win
};
return result;
},
calcLaser: function(){
var laserStart = this.findTile("laser");
var searchDirection = direction.right;
var laserPath = [laserStart];
var curPosition = laserStart;
while (true){
curPosition = [curPosition[0] + searchDirection[0],
curPosition[1] + searchDirection[1]];
if (this.isOnBoard(curPosition)){
type = this.board[curPosition[0]][curPosition[1]];
if (type == "bullseye"){
laserPath.push(curPosition);
console.log('win condition');
this.win = true;
break;
} else if (type == "empty"){
continue;
} else if (type == "block"){
laserPath.push(curPosition);
break;
} else if (type == "laser"){
break;
} else if (type.kind == "mirror"){
var fromDirection = [-searchDirection[0], -searchDirection[1]];
searchDirection = type.from[directionName(fromDirection)];
laserPath.push(curPosition);
if (searchDirection === null){
break;
} else {
continue;
}
}
} else {
laserPath.push(curPosition);
break;
}
}
return laserPath;
},
isOnBoard : function(pos){
if (pos[0] < this.boardRows && pos[0] > -1 &&
pos[1] < this.boardColumns && pos[1] > -1){
return true;
} else {
return false;
}
},
isValidBoard : function(mirror_count){
var toCheck = [[0,0,"right",0]]; //starting position, starting direction, 0 mirrors used
var checked = [];
var valid = false;
var getNeighbors = function(node){
//console.log(node);
var searchDirection = direction[node[2]];
var curPosition = [node[0],node[1]];
var neighbors = [];
while (true){
curPosition = [curPosition[0] + searchDirection[0],
curPosition[1] + searchDirection[1]];
if (this.isOnBoard(curPosition)){
type = this.board[curPosition[0]][curPosition[1]];
if (type == "empty"){
if(node[3] < mirror_count){
var perps = getPerps(searchDirection);
neighbors.push(curPosition.concat([perps[0], node[3]+1]));
neighbors.push(curPosition.concat([perps[1], node[3]+1]));
}
continue;
} else if (type == "bullseye"){
valid = true;
break;
} else {
break;
}
} else {
break;
}
}
return neighbors;
};
while(true){
var check = toCheck.pop();
if(valid){
return valid;
} else {
toCheck = toCheck.concat(getNeighbors.call(this,check));
if(toCheck.length === 0){
return valid;
}
}
}
}
};
game1 = getSolvableGame(12, 60);
io.sockets.on('connection', function (socket) {
socket.emit('startGame', game1.toJson());
socket.on('newGame', function (data) {
game1 = getSolvableGame(12, 60);
io.sockets.emit('startGame', game1.toJson());
});
socket.on('rotate', function (data) {
if (game1.board[data.row][data.column].kind == "mirror") {
game1.board[data.row][data.column] = new Mirror(game1.board[data.row][data.column].rotate);
}
io.sockets.emit('gameState', game1.toJson());
});
socket.on('drag', function(data) {
if(data.start.row != data.end.row || data.start.column != data.end.column) {
if(game1.isOnBoard([data.end.row, data.end.column]) &&
game1.isOnBoard([data.start.row, data.start.column]) &&
game1.board[data.end.row][data.end.column] == "empty"){
game1.board[data.end.row][data.end.column] = game1.board[data.start.row][data.start.column];
game1.board[data.start.row][data.start.column] = "empty";
}
}
io.sockets.emit('gameState', game1.toJson());
});
});