-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.js
444 lines (396 loc) · 11.6 KB
/
Game.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//---- Common methods ----
/**
* Randomize array element order in-place.
* Using Fisher-Yates shuffle algorithm.
*/
//"use strict"; //Commented due to a Safari bug
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
function repeat(n, value ) {
return Array.apply(null, Array(n)).map(function(_) {return value;});
}
var offsets = {
e: {x: 1, y: 0},
ne: {x: 1, y: -1},
n: {x: 0, y: -1},
nw: {x: -1, y: -1},
w: {x: -1, y: 0},
sw: {x: -1, y: 1},
s: {x: 0, y: 1},
se: {x: 1, y: 1}
};
//---- Board tiles ----
function Tile(desc, color, attrs) {
this.desc = desc;
this.color = color;
for (var attr in attrs)
if (attr != "type")
this[attr] = attrs[attr];
}
var Tiles = {
//Dumb plain grass.
grass: function() {
return new Tile("Трава", "#8DCF54");
},
//Sea. Can be traversed only by ship or cannon-launched pirate.
sea: function() {
return new Tile("Море", "#82F2E2", {sea: true});
},
//Ship belonging to a team.
ship: function(params) {
return new Tile(teams[params.team].name + " корабль", "#CD853F", params);
},
//Chest contains some money.
chest: function(params) {
return new Tile("Сундук", "#CD853F", params);
},
//Cannibal kills any pirate he meets.
cannibal: function() {
return new Tile("Людоед", "#FF4411", {team: 0});
},
//Labyrinths require additional turns to pass.
_labyrinths: [
new Tile("Лес", "#5D9F34", {steps: 1}),
new Tile("Пустыня", "#DDDD55", {steps: 2}),
//{desc: "Болото", },
//{desc: "Горы", },
],
//TODO: clone?
labyrinth: function(params) {
return this._labyrinths[params.steps - 1];
},
//Arrows force a pirate to take an additional turn in any of provided directions.
arrow: function(params) {
var coordOffs = params.offs.map(function(off) {return offsets.off;});
return new Tile("Стрелка " + params.desc, "#DDDD55", {offsets: coordOffs});
},
_horseOffsets: /*{*/ [
/*nww:*/ {x: 2, y: -1},
/*nnw:*/ {x: 1, y: -2},
/*nne:*/ {x: -1, y: -2},
/*nee:*/ {x: -2, y: -1},
/*see:*/ {x: -2, y: 1},
/*sse:*/ {x: -1, y: 2},
/*ssw:*/ {x: 1, y: 2},
/*sww:*/ {x: 2, y: 1}
/*}*/ ],
//Horses are like arrows, and force a pirate to take a turn of a chess knight.
horse: function() {
return new Tile("Конь", "#DDDD55", {offsets: this._horseOffsets});
},
//Ice tile makes the pirate to repeat the last move.
ice: function() {
return new Tile("Каток", "#77AAFF", {offsets: ["last"]});
},
//Parachute allows to "teleport" onto a ship, including carried money.
chute: function() {
return new Tile("Парашют", "#FFAA77", {chute: true});
},
}
//---- Pirates and their teams ----
var teams = [
{name: "Призрачный", pirate: "Призрак"},
{name: "Красный", pirate: "Пират"},
{name: "Белый", pirate: "Корсар"},
];
function makePirate(teamId, id, i, j) {
return {
desc: teams[teamId].pirate, team: teamId, id: id, x: i, y: j,
};
}
//---- Game board "class" ----
function Board(size) {
this.size = size;
this._field = [];
//TODO: configurable teams
this.players = [1, 2];
this.pirates = [];
//
this.turnIndex = 0;
//Gets or sets a tile.
this.tile = function(i, j, what) {
if (what === undefined)
return this._field[this.size * j + i];
else
this._field[this.size * j + i] = what;
};
//Gets or sets a pirate.
//TODO: should it be there?
this.pirate = function(i, what) {
if (what === undefined)
return this.pirates[i];
else
this.pirates[i] = what;
};
//Some observing functions
this._indexPos = function(i) {
return {x: i % this.size, y: Math.floor(i / this.size)};
};
//Returns array of pirates on some tile
this.piratesOn = function(i, j) {
var res = [];
for (var p of this.pirates) {
if (p && p.x == i && p.y == j)
res.push(p);
}
return res;
};
//Return the position of the ship of some team
this.shipPosition = function(teamId) {
//TODO: optimize
for (var i in this._field)
if (this._field[i].team === teamId)
return this._indexPos(i);
};
//Returns an array of what a pirate can do this turn.
//TODO: remove "who" and "where" as long they can be stored in the closure
//POSSIBLE TODO: make polymorphic method in tiles?
this.possibleActions = function(p) {
var res = [];
//Wait for his team's turn
if (p.team !== this.players[this.turnIndex])
return res;
//In the labyrinth
if (p.step) {
res.push({
action: "move", x: p.x, y: p.y,
desc: "Ползти", act: function(who, where) {
return where.move(who, this.x, this.y);
}
});
}
//Can go wherever is possible
else for (var off in offsets) {
var i = p.x + offsets[off].x,
j = p.y + offsets[off].y;
if (i >= 0 && i < this.size &&
j >= 0 && j < this.size &&
//Can swim only if already in the sea
this.tile(i, j).sea == this.tile(p.x, p.y).sea)
res.push({
action: "move", x: i, y: j,
desc: "Идти", act: function(who, where) {
return where.move(who, this.x, this.y);
}
});
}
var tile = this.tile(p.x, p.y);
//Drop the money if the pirate has it
if (p.money)
res.push({
action: "drop", x: p.x, y: p.y,
desc: "Положить", act: function(who, where) {
return where.drop(who);
}
});
//Grab the money if he doesn't
else if (tile.money)
res.push({
action: "grab", x: p.x, y: p.y,
desc: "Взять", act: function(who, where) {
return where.grab(who);
}
});
//Drive the ship
if (tile.team === p.team) {
//TODO: vertical teams
var driveOffsets = [{x: 1, y: 0}, {x: -1, y: 0}];
for (var off of driveOffsets) {
var i = p.x + off.x, j = p.y + off.y;
if (i < 1 || i > this.size - 2)
continue;
res.push({
action: "drive", x: i, y: j,
desc: "Плыть", act: function(who, where) {
return where.drive(who, this.x, this.y);
}
});
}
}
//Flee with a parachute
if (tile.chute) {
var pos = this.shipPosition(p.team);
res = [{
action: "move", x: pos.x, y: pos.y,
desc: "Лететь", act: function(who, where) {
return where.move(who, this.x, this.y);
}
}];
}
return res;
};
//Methods which axtually perform player actions
//Drive a ship with a pirate
this.drive = function(p, i, j) {
var ship = this.tile(p.x, p.y);
this.tile(p.x, p.y, Tiles.sea());
this.tile(i, j, ship);
for (var tp of this.piratesOn(p.x, p.y))
this._move(tp, i, j);
this._completeTurn();
return true;
};
//Move a pirate
this._move = function(p, i, j) {
if (p.x === i && p.y === j && p.step) {
//Make a step in the labyrinth
if (p.step < this.tile(p.x, p.y).steps)
++p.step;
else
//Get out of the labyrinth
p.step = undefined;
}
else {
p.x = i; p.y = j;
p.step = this.tile(i, j).steps ? 1 : undefined;
//Sink the gold
if (this.tile(i, j).sea)
p.money = 0;
}
};
this.move = function(p, i, j) {
var prev_x = p.x, prev_y = p.y;
//TODO: check possibility
this._move(p, i, j);
//Kick all enemy pirates
var board = this;
this.piratesOn(i, j).forEach(function (tp) {
if (tp.team !== p.team && tp.step === p.step)
board.kick(tp);
});
if (this.tile(i, j).offsets !== undefined) {
return this.tile(i, j).offsets.map(function (off) {
var ti, tj;
if (off === "last") {
//Repeat the last step
//TODO: what if from an airplane?
ti = p.x + p.x - prev_x;
tj = p.y + p.y - prev_y;
}
else {
ti = i + off.x; tj = j + off.y;
}
return {action: "move", x: ti, y: tj,
desc: "Прыг", act: function(who, where) {
return where.move(who, this.x, this.y);
}
};
});
}
else {
var teamId = this.tile(p.x, p.y).team;
if (teamId !== undefined && teamId !== p.team)
this.die(p);
this._completeTurn();
return true;
}
};
//Grab a coin by a pirate
this.grab = function(p) {
var c = this.tile(p.x, p.y);
if (c.money && !p.money) {
c.money--;
p.money = 1;
return true;
}
return false;
};
//Drop the coin by a pirate
this.drop = function(p) {
var c = this.tile(p.x, p.y);
if (p.money) {
if (c.money === undefined)
c.money = 0;
c.money++;
p.money = 0;
return true;
}
return false;
};
//Get pirate kicked to the team ship
this.kick = function(p) {
var pos = this.shipPosition(p.team);
this.drop(p);
this._move(p, pos.x, pos.y);
};
//Death from anything
this.die = function(p) {
this.drop(p);
this.pirates[p.id] = undefined;
//TODO: team loses if doesn't have pirates left
};
//(private) Ends the current player turn
this._completeTurn = function() {
this.turnIndex = (this.turnIndex + 1) % this.players.length;
};
this.currentTeam = function() {
return teams[this.players[this.turnIndex]];
};
}
var test = [
[{"type": "sea"}, {"type": "sea"}, {"type": "ship", "team": 1}, {"type": "sea"}, {"type": "sea"}],
[{"type": "sea"}, {"type": "chest", "money": 3}, {"type": "grass"}, {"type": "grass"}, {"type": "sea"}],
[{"type": "sea"}, {"type": "chest", "money": 1}, {"type": "labyrinth", "steps": 2}, {"type": "chute"}, {"type": "sea"}],
[{"type": "sea"}, {"type": "arrow", "desc": "↕︎", "offs": ["n", "s"]}, {"type": "grass"}, {"type": "cannibal"}, {"type": "sea"}],
[{"type": "sea"}, {"type": "sea"}, {"type": "ship", "team": 2}, {"type": "sea"}, {"type": "sea"}],
]
//Makes a board from the server JSON data
function serverBoard(arr) {
var res = new Board(arr.length); //Assuming the data correctness
for (var j = 0; j < arr.length; ++j) {
for (var i = 0; i < arr.length; ++i) {
res.tile(i, j, Tiles[arr[i][j]["type"]](arr[i][j]));
}
}
return res;
}
function testBoard(s) {
var size = 2 * s + 1;
var board = new Board(size);
for (var j = 0; j < size; ++j) {
for (var i = 0; i < size; ++i) {
if (j == 0 || j == size - 1 ||
i == 0 || i == size - 1)
board.tile(i, j, Tiles.sea());
else
board.tile(i, j, Tiles.grass());
}
}
//Some chests
var n = 1;
for (var i = 1; i <= 3; ++i) {
for (var j = 1; j <= 3 - i; ++j) {
board.tile(n++, 1, Tiles.chest({money: j}));
}
}
//Some arrows
board.tile(s, 2, Tiles.arrow({desc:"↔︎", offs: ["w", "e"]}));
board.tile(s + 1, 2, Tiles.arrow({desc: "↕︎", offs: ["n", "s"]}));
board.tile(2, 2, Tiles.ice());
board.tile(3, 3, Tiles.cannibal());
board.tile(4, 4, Tiles.chute());
//Some labyrinths
board.tile(1, 2, Tiles.labyrinth({steps: 2}));
board.tile(2, 5, Tiles.labyrinth({steps: 1}));
//TODO: shuffling
//Ships for two teams
board.tile(s, 0, Tiles.ship({team: 1}));
board.tile(s, size - 1, Tiles.ship({team: 2}));
//One pirate for each team
var pCount = 0;
for (var teamId of board.players) {
var pos = board.shipPosition(teamId);
for (var i = 0; i < 3; ++i) {
var somePirate = makePirate(teamId, ++pCount, pos.x, pos.y);
board.pirate(somePirate.id, somePirate);
}
}
return board;
}