-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
355 lines (355 loc) · 12.9 KB
/
Copy pathindex.js
File metadata and controls
355 lines (355 loc) · 12.9 KB
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
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var ROWS = 5;
var COLS = 5;
var TICK = 1000;
var randomNumberInt = function (n) { return Math.floor(Math.random() * n) + 1; };
var generateGUID = function () {
return 'xxxxxxxx'.replace(/[xy]/g, function (char) {
var random = Math.random() * 16 | 0;
var value = char === 'x' ? random : (random & 0x3 | 0x8);
return value.toString(16);
});
};
var generateTeam = function (teams, rows, colls) {
var color = generateHexColor();
var totalShips = randomNumberInt(teams);
var ships = Array.from({ length: totalShips }, function () { return ({ x: randomNumberInt(colls), y: randomNumberInt(rows) }); });
var id = generateGUID();
var accuracy = randomNumberInt(10);
return {
id: id,
color: color,
sightRadius: teams,
attackRadius: teams,
accuracy: accuracy,
coolDown: teams,
ships: ships
};
};
var generateHexColor = function () {
return "#".concat(Math.floor(Math.random() * 16777215).toString(16).padStart(6, '0'));
};
var createPlayBoardCell = function (id) {
var div = document.createElement('div');
div.id = id;
div.className = 'cell';
return div;
};
var Cell = (function () {
function Cell(color) {
this.color = color;
this.id = generateGUID();
this.element = createPlayBoardCell(this.id);
}
Cell.prototype.getColor = function () {
return this.color;
};
Cell.prototype.setColor = function (newColor) {
return this.color = newColor;
};
Cell.prototype.getElement = function () {
return this.element;
};
Cell.prototype.explode = function () {
return this.element;
};
Cell.prototype.draw = function () {
this.element.style.backgroundColor = this.color;
};
return Cell;
}());
var PlayBoard = (function () {
function PlayBoard(element) {
this.playBoard = element;
this.playCells = [];
}
PlayBoard.prototype.init = function (rows, cols) {
var _this = this;
this.rows = rows;
this.cols = cols;
this.playBoard.style.gridTemplateColumns = "repeat(".concat(cols, ",1fr)");
this.playBoard.style.gridTemplateRows = "repeat(".concat(rows, ",1fr)");
this.playCells = Array.from({ length: rows * cols }, function () { return new Cell('white'); });
this.playCells.map(function (cell) {
_this.playBoard.appendChild(cell.getElement());
});
return this;
};
PlayBoard.prototype.update = function () {
};
PlayBoard.prototype.draw = function () {
this.playCells.forEach(function (cell) { return cell.draw(); });
};
PlayBoard.prototype.clear = function () {
this.playCells.forEach(function (cell) { return cell.setColor('white'); });
};
PlayBoard.prototype.getCellByCoords = function (x, y) {
var cell = this.playCells[x + y * this.cols];
if (cell) {
return cell;
}
};
PlayBoard.prototype.setCell = function (x, y, color) {
var cell = this.getCellByCoords(x, y);
if (cell) {
cell.setColor(color);
}
};
return PlayBoard;
}());
var getParams = function () {
var _a, _b, _c, _d;
var params = new URLSearchParams(location.search);
var width = (_a = parseInt(params.get('width'))) !== null && _a !== void 0 ? _a : ROWS;
var height = (_b = parseInt(params.get('height'))) !== null && _b !== void 0 ? _b : COLS;
var tick = (_c = params.get('tick')) !== null && _c !== void 0 ? _c : TICK;
var teamsParam = (_d = parseInt(params.get('teams'))) !== null && _d !== void 0 ? _d : 3;
var teams = Array.from({ length: teamsParam }, function () { return generateTeam(teamsParam, width, height); });
return { width: width, height: height, tick: tick, teams: teams };
};
var Ship = (function () {
function Ship() {
this.id = generateGUID();
this.isDead = false;
}
Ship.prototype.draw = function (board, color) {
board.setCell(this.x, this.y, this.isDead ? 'black' : color);
};
Ship.prototype.sees = function (otherShip, sightRadius) {
var distance = Math.max(Math.abs(this.x - otherShip.x), Math.abs(this.y - otherShip.y));
return distance <= sightRadius;
};
Ship.prototype.reduceCooldown = function () {
this.coolDown = Math.max(this.coolDown - 1, 0);
};
return Ship;
}());
var DecisionsState = (function () {
function DecisionsState() {
}
return DecisionsState;
}());
var SkipAction = (function () {
function SkipAction() {
this.type = "Skip";
}
return SkipAction;
}());
var MoveAction = (function () {
function MoveAction(ship, dx, dy) {
this.type = "Move";
this.ship = ship;
this.dx = dx;
this.dy = dy;
}
return MoveAction;
}());
var ShootAction = (function () {
function ShootAction(ship, shootAt) {
this.type = "Shoot";
this.ship = ship;
this.shootAt = shootAt;
}
return ShootAction;
}());
function randomDir() {
var a = Math.floor(Math.random() * 3);
switch (a) {
case 0: return 1;
case 1: return -1;
case 2: return 0;
}
}
var basicAgent = function () {
var lastDx = randomDir();
var lastDy = randomDir();
var lastTurned = 10;
return function (state) {
if (state.visibleShips.length) {
var target = null;
var possibleTargets = state.ourShips.reduce(function (targets, ship) {
if (ship.coolDown > 0 || ship.isDead)
return targets;
var additionalTargets = state.visibleShips
.filter(function (testShip) { return ship.sees(testShip, state.attackRadius) && !testShip.isDead; })
.map(function (targetShip) { return ({ shootFrom: ship, shootAt: targetShip }); });
return __spreadArray(__spreadArray([], targets, true), additionalTargets, true);
}, []);
if (possibleTargets.length > 0) {
var index = Math.floor(Math.random() * possibleTargets.length);
var target_1 = possibleTargets[index];
return new ShootAction(target_1.shootFrom.id, target_1.shootAt.id);
}
}
if (state.ourShips.length > 0) {
var i = Math.floor(Math.random() * state.ourShips.length);
var action = new MoveAction(state.ourShips[i].id, lastDx, lastDy);
lastTurned -= 1;
if (lastTurned == 0) {
lastTurned = 10;
lastDx = randomDir();
lastDy = randomDir();
}
return action;
}
else {
return new SkipAction();
}
};
};
var Team = (function () {
function Team(_a) {
var color = _a.color, sightRadius = _a.sightRadius, attackRadius = _a.attackRadius, coolDown = _a.coolDown, accuracy = _a.accuracy, id = _a.id;
this.id = id;
this.color = color;
this.ships = [];
this.sightRadius = sightRadius;
this.attackRadius = attackRadius;
this.coolDown = coolDown;
this.accuracy = accuracy;
}
Team.prototype.draw = function (board) {
var _this = this;
this.ships.forEach(function (ship) { return ship.draw(board, _this.color); });
};
Team.prototype.seesShip = function (ship) {
var _this = this;
return this.ships.reduce(function (sees, testShip) {
return sees || testShip.sees(ship, _this.sightRadius);
}, false) || false;
};
return Team;
}());
var Game = (function () {
function Game(playBoard) {
this.teams = [];
this.deadShips = [];
this.playBoard = playBoard;
this.losers = [];
this.session = null;
}
Game.prototype.registerSession = function (session) {
this.session = session;
};
Game.prototype.init = function (teamsParam) {
var _this = this;
this.teams = [];
this.deadShips = [];
teamsParam.forEach(function (param) {
var team = new Team({
id: param.id,
color: param.color,
sightRadius: param.sightRadius,
attackRadius: param.attackRadius,
coolDown: param.coolDown,
accuracy: param.accuracy,
});
team.agent = basicAgent();
param.ships.forEach(function (shipParam) {
var ship = new Ship();
ship.x = shipParam.x;
ship.y = shipParam.y;
ship.coolDown = 0;
team.ships.push(ship);
});
_this.teams.push(team);
});
};
Game.prototype.update = function () {
var _this = this;
var allShips = this.teams.reduce(function (ships, team) { return __spreadArray(__spreadArray([], ships, true), team.ships, true); }, __spreadArray([], this.deadShips, true));
allShips.forEach(function (ship) { return ship.reduceCooldown(); });
this.teams.forEach(function (team) {
var isAllShipsDead = team.ships.filter(function (ship) { return !ship.isDead; }).length === 0;
if (isAllShipsDead && !_this.losers.includes(team.id)) {
_this.losers.push(team.id);
}
var visibleShips = allShips.filter(function (testShip) {
var seesShip = team.seesShip(testShip);
var sameTeam = team.ships.includes(testShip);
return seesShip && !sameTeam && !testShip.isDead;
});
var state = {
ourShips: team.ships,
visibleShips: visibleShips,
teams: [],
sightRadius: team.sightRadius,
attackRadius: team.attackRadius,
};
var action = team.agent(state);
if (action instanceof MoveAction) {
var ship_1 = team.ships.find(function (ship) { return ship.id == action.ship; });
if (!ship_1 || ship_1.isDead)
return;
var newX_1 = ship_1.x + action.dx;
var newY_1 = ship_1.y + action.dy;
if (newX_1 < 0)
newX_1 = 0;
if (newX_1 >= _this.playBoard.cols)
newX_1 = _this.playBoard.cols - 1;
if (newY_1 < 0)
newY_1 = 0;
if (newY_1 >= _this.playBoard.rows)
newY_1 = _this.playBoard.rows - 1;
var collidesWith = allShips.find(function (testShip) { return testShip.x == newX_1 && testShip.y == newY_1 && testShip.id !== ship_1.id; });
if (!collidesWith) {
ship_1.x = newX_1;
ship_1.y = newY_1;
}
}
if (action instanceof ShootAction) {
var shootFrom = team.ships.find(function (ship) { return ship.id == action.ship; });
var shootAt = allShips.find(function (ship) { return ship.id == action.shootAt; });
if (!shootFrom || shootFrom.isDead || !shootAt)
return;
if (shootFrom.coolDown > 0)
return;
var canShoot = shootFrom.sees(shootAt, team.attackRadius);
var success = Math.random() <= team.accuracy;
if (canShoot) {
shootFrom.coolDown = team.coolDown;
if (success) {
shootAt.isDead = true;
}
}
}
});
if (this.losers.length === this.teams.length - 1) {
clearInterval(this.session);
}
};
Game.prototype.draw = function () {
var _this = this;
this.playBoard.clear();
this.teams.forEach(function (team) { return team.draw(_this.playBoard); });
this.deadShips.forEach(function (ship) { return ship.draw(_this.playBoard, 'black'); });
};
return Game;
}());
(function () {
var _a;
var params = getParams();
var root = (_a = document.getElementById('play-board')) !== null && _a !== void 0 ? _a : document.createElement('div');
root.id = 'play-board';
root.classList.add('playboard');
var playBoard = new PlayBoard(root).init(params.height, params.width);
var game = new Game(playBoard);
game.init(params.teams);
game.draw();
playBoard.draw();
var session = setInterval(function () {
game.update();
game.draw();
playBoard.draw();
}, params.tick);
game.registerSession(session);
})();
//# sourceMappingURL=index.js.map