Repository navigation
Expand file tree
/
Copy pathGame.js
More file actions
349 lines (328 loc) · 9.53 KB
/
Copy pathGame.js
File metadata and controls
349 lines (328 loc) · 9.53 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
function Game(){
var exports = this;
var gridIds = {
p1Spawn: 'r',
p2Spawn: 'b',
player1: '1',
player2: '2',
dead1: 'x',
dead2: 'X',
energy: '*',
empty: '.'
};
var distance = {
move: 1,
raze: 1,
attack: 1,
energy: 0,
spawn: 1
};
var spawnFrequency = 5;
var players = {
player1: 'p1',
player2: 'p2'
}
exports.create = function (rows, cols, maxTurns) {
var gameState = {
rows: rows,
cols: cols,
maxTurns: maxTurns || 20,
turnsElapsed: 0
};
gameState.p1 = {
energy: 1,
spawn: coordToIndex(gameState, { x: 1, y: 1 })
};
gameState.p2 = {
energy: 1,
spawn: coordToIndex(gameState, { x: cols - 2, y: rows - 2 })
};
gameState.grid = makeEmptyGrid(gameState.rows, gameState.cols);
return gameState;
};
exports.doTurn = function (state, p1Moves, p2Moves, testing) {
var p1Valid = validMoves(p1Moves);
var p2Valid = validMoves(p2Moves);
if (!p1Valid && !p2Valid) {
state.winner = gridIds.empty;
return state;
}
else if (!p1Valid) {
state.winner = players.player2;
return state;
}
else if (!p2Valid) {
state.winner = players.player1;
return state;
}
// move
var re = new RegExp(gridIds.dead1, 'gi');
state.grid = state.grid.replace(re, gridIds.empty);
re = new RegExp(gridIds.player1 + '|' + gridIds.player2, 'g');
var newState = copyObj(state);
newState.grid = newState.grid.replace(re, gridIds.empty);
p1Moves.forEach(function (move) {
if (adjacent(state, move.to, move.from) && state.grid[move.from] === gridIds.player1) {
setIndex(state, move.from, gridIds.empty);
if (newState.grid[move.to] === gridIds.player1 || newState.grid[move.to] === gridIds.player2) {
setIndex(newState, move.to, gridIds.dead1);
}
else {
setIndex(newState, move.to, gridIds.player1);
}
}
});
p2Moves.forEach(function (move) {
if (adjacent(state, move.to, move.from) && state.grid[move.from] === gridIds.player2) {
setIndex(state, move.from, gridIds.empty);
if (newState.grid[move.to] === gridIds.player1 || newState.grid[move.to] === gridIds.player2) {
setIndex(newState, move.to, gridIds.dead2);
}
else {
setIndex(newState, move.to, gridIds.player2);
}
}
});
var unmovedP1s = getAllIndices(state.grid, gridIds.player1);
var unmovedP2s = getAllIndices(state.grid, gridIds.player2);
unmovedP1s.forEach(function (index) {
if (newState.grid[index] === gridIds.player1 || newState.grid[index] === gridIds.player2) {
setIndex(newState, index, gridIds.dead1);
}
else {
setIndex(newState, index, gridIds.player1);
}
});
unmovedP2s.forEach(function (index) {
if (newState.grid[index] === gridIds.player1 || newState.grid[index] === gridIds.player2) {
setIndex(newState, index, gridIds.dead2);
}
else {
setIndex(newState, index, gridIds.player2);
}
});
state = newState;
// fight
var p1Indices = getAllIndices(state.grid, gridIds.player1);
var p2Indices = getAllIndices(state.grid, gridIds.player2);
var battleData = { p1: {}, p2: {} };
p1Indices.forEach(function (p1Index) {
battleData.p1[p1Index] = 0;
p2Indices.forEach(function (p2Index) {
if (adjacent(state, p1Index, p2Index)) {
battleData.p1[p1Index] += 1;
if (battleData.p2[p2Index]) {
battleData.p2[p2Index] += 1;
}
else {
battleData.p2[p2Index] = 1;
}
}
});
});
var dead1 = [];
var dead2 = [];
p1Indices.forEach(function (p1Index) {
var surroundingP1 = battleData.p1[p1Index];
getAdjacentIndices(state, p1Index).forEach(function (adjIndex) {
if (battleData.p2[adjIndex]) {
var surroundingP2 = battleData.p2[adjIndex];
if (surroundingP1 > surroundingP2) {
if (dead1.indexOf(p1Index) === -1)
dead1.push(p1Index);
}
else if (surroundingP1 === surroundingP2) {
if (dead1.indexOf(p1Index) === -1)
dead1.push(p1Index);
if (dead2.indexOf(adjIndex) === -1)
dead2.push(adjIndex);
}
else {
if (dead2.indexOf(adjIndex) === -1)
dead2.push(adjIndex);
}
}
});
});
dead1.forEach(function (index) {
setIndex(state, index, gridIds.dead1);
});
dead2.forEach(function (index) {
setIndex(state, index, gridIds.dead2);
});
// raze
if (state.grid[state.p1.spawn] === gridIds.player2) {
state.p1.spawnDisabled = true;
}
if (state.grid[state.p2.spawn] === gridIds.player1) {
state.p2.spawnDisabled = true;
}
// spawn
if (!state.p1.spawnDisabled && state.p1.energy > 0 && state.grid[state.p1.spawn] === gridIds.empty) {
state.p1.energy -= 1;
setIndex(state, state.p1.spawn, gridIds.player1);
}
if (!state.p2.spawnDisabled && state.p2.energy > 0 && state.grid[state.p2.spawn] === gridIds.empty) {
state.p2.energy -= 1;
setIndex(state, state.p2.spawn, gridIds.player2);
}
// gather
var energy = getAllIndices(state.grid, '\\' + gridIds.energy);
energy.forEach(function (energyIndex) {
var surroundingP1 = 0;
var surroundingP2 = 0;
getAdjacentIndices(state, energyIndex).forEach(function (adjIndex) {
if (state.grid[adjIndex] === gridIds.player1) {
surroundingP1++;
}
else if (state.grid[adjIndex] === gridIds.player2) {
surroundingP2++;
}
});
if (surroundingP1 && surroundingP2) {
setIndex(state, energyIndex, gridIds.empty);
}
else if (surroundingP1) {
setIndex(state, energyIndex, gridIds.empty);
state.p1.energy++;
}
else if (surroundingP2) {
setIndex(state, energyIndex, gridIds.empty);
state.p2.energy++;
}
});
// spawn energy
if (!testing && state.turnsElapsed % spawnFrequency === 0) {
var generateNum = state.rows * state.cols / 50;
var allEmptyIndices = getAllIndices(state.grid, gridIds.empty);
var available = [];
allEmptyIndices.forEach(function (index) {
var mirrored = getMirroredIndex(state, index);
if (mirrored >= 0) {
if (allEmptyIndices.indexOf(mirrored) >= 0) {
available.push(index);
}
}
});
if (available.length) {
var randomIndex = available[Math.floor(Math.random() * available.length)];
var mirroredIndex = getMirroredIndex(state, randomIndex);
setIndex(state, randomIndex, gridIds.energy);
setIndex(state, mirroredIndex, gridIds.energy);
}
}
state.turnsElapsed++;
// determine whether to continue/end game
var numP1 = getAllIndices(state.grid, gridIds.player1).length;
var numP2 = getAllIndices(state.grid, gridIds.player2).length;
if (state.turnsElapsed >= state.maxTurns || !numP1 || !numP2) {
if (numP1 > numP2) {
state.winner = players.player1;
}
else if (numP2 > numP1) {
state.winner = players.player2;
}
else {
var i = ~~(Math.random() * 2);
if (i) state.winner = players.player1;
else state.winner = players.player2;
}
}
return state;
};
// movement functions
function validMoves(moves) {
if (!Array.isArray(moves)) {
return false;
}
var allMovesValid = true;
moves.forEach(function (move) {
if (typeof move.from !== 'number' || typeof move.to !== 'number') {
allMovesValid = false;
}
});
if (!allMovesValid)
return false;
return true;
}
// grid functions
function getCoord(state, coord) {
var index = coordToIndex(state, coord);
return state.grid[index];
}
function setCoord(state, coord, val) {
var index = coordToIndex(state, coord);
state.grid = state.grid.substr(0, index) + val + state.grid.substr(index + 1);
}
function setIndex(state, index, val) {
state.grid = state.grid.substr(0, index) + val + state.grid.substr(index + 1);
}
function showGrid(state) {
for (var y = 0; y < state.rows; y++) {
var row = '';
for (var x = 0; x < state.cols; x++) {
row += state.grid[coordToIndex(state, { x: x, y: y })];
}
console.log(row + '\n');
}
}
function indexToCoord(state, index) {
var x = index % state.cols;
var y = ~~(index / state.cols);
return { x: x, y: y };
}
function coordToIndex(state, coord) {
return state.cols * coord.y + coord.x;
}
function makeEmptyGrid(rows, cols) {
return Array(rows * cols + 1).join(gridIds.empty);
}
function adjacent(state, index1, index2) {
if (index1 < state.grid.length && index2 < state.grid.length && index1 >= 0 && index2 >= 0) {
var coord1 = indexToCoord(state, index1);
var coord2 = indexToCoord(state, index2);
var horizontal = Math.abs(coord1.x - coord2.x);
var vertical = Math.abs(coord1.y - coord2.y);
if ((horizontal <= distance.move && vertical === 0) || (vertical <= distance.move && horizontal === 0)) {
return true;
}
}
return false;
}
function getAllIndices(grid, search) {
var arr = [];
if (search === '.') search = '\\.';
var re = new RegExp(search, 'g');
while (m = re.exec(grid)) {
arr.push(m.index);
}
return arr;
}
function getAdjacentIndices(state, index) {
var indices = [];
var coord = indexToCoord(state, index);
if (coord.x > 0)
indices.push(coordToIndex(state, { x: coord.x - 1, y: coord.y }));
if (coord.x < state.cols - 1)
indices.push(coordToIndex(state, { x: coord.x + 1, y: coord.y }));
if (coord.y > 0)
indices.push(coordToIndex(state, { x: coord.x, y: coord.y - 1 }));
if (coord.y < state.rows - 1)
indices.push(coordToIndex(state, { x: coord.x, y: coord.y + 1 }));
return indices;
}
function getMirroredIndex(state, index) {
var coord = indexToCoord(state, index);
var mirroredCoord = { x: state.cols - 1 - coord.x, y: state.rows - 1 - coord.y };
var mirroredIndex = coordToIndex(state, mirroredCoord);
if (mirroredIndex !== index) {
return mirroredIndex;
}
else {
return -1;
}
}
function copyObj(object) {
return JSON.parse(JSON.stringify(object));
}
}