-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
205 lines (183 loc) · 5.1 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
// Game object
function SurvivalGame(blockSize, mapSize) {
this.blockSize = blockSize;
this.mapSize = mapSize;
this.map = makeSquareArray(this.mapSize);
this.currentTurn = 0; // Default value
this.maxTurn = 500; // Default value
}
SurvivalGame.prototype.newGame = function() {
this.currentTurn = 0;
this.map = makeSquareArray(this.mapSize);
// TODO: This should be elsewhere?
$.each(animalsBaseCount, function(k, v){
animalsBaseCount[k] = 0;
});
}
SurvivalGame.prototype.populate = function(activeAnimalList, count) {
var g = this;
$.each(activeAnimalList, function(k, animalType){
var animalName = '';
for(var animalCount = 0; animalCount < count; animalCount++){
var x = Math.floor(Math.random() * g.mapSize);
var y = Math.floor(Math.random() * g.mapSize);
g.map[x][y] = new animalType;
if (animalCount == 0) animalName = g.map[x][y].name();
}
animalsBaseCount[animalName] += count;
});
}
SurvivalGame.prototype.next = function() {
// Clear the temp map used to find fights
var halfStepMap = {};
// Go through all the animals
for (var x = 0; x < this.mapSize; x++) {
for (var y = 0; y < this.mapSize; y++) {
if (this.map[x][y] != null) {
// Get the animal's surroundings and ask for it's move
var surroundings = getSurroundings(x,y);
var animal = this.map[x][y];
var move = animal.move(surroundings);
// Move the animal
switch (move) {
case 'h':
moveAnimal(halfStepMap, x, y, animal);
break;
case 'l':
moveAnimal(halfStepMap, getSafeCoordinate(x-1), y, animal);
break;
case 'u':
moveAnimal(halfStepMap, x, getSafeCoordinate(y-1), animal);
break;
case 'r':
moveAnimal(halfStepMap, getSafeCoordinate(x+1), y, animal);
break;
case 'd':
moveAnimal(halfStepMap, x, getSafeCoordinate(y+1), animal);
break;
}
// Remove the animal from the old position
// Animals that don't move shouldn't be removed
if (move != 'h'){
this.map[x][y] = null;
}
}
}
}
// Resolve any fights that occur
resolveAllFights(halfStepMap, this.map);
this.currentTurn++;
function moveAnimal(temporaryMap, x, y, animal) {
if (temporaryMap[x] != undefined) {
if (temporaryMap[x][y] != undefined) {
// Animal already there!
temporaryMap[x][y].push(animal);
} else {
// No animal in this grid square
temporaryMap[x][y] = [animal];
}
} else {
// No animal in this row!
temporaryMap[x] = {};
temporaryMap[x][y] = [animal]
}
}
// Resolve fights on the map
function resolveAllFights(temporaryMap, finalMap) {
$.each(temporaryMap, function(x, columns){
$.each(columns, function(y, animals){
if (animals.length > 1) {
// fight the animals!
finalMap[x][y] = resolveFight(animals);
} else {
// only one animal
finalMap[x][y] = animals[0]
}
});
});
}
// Takes an array of animals, pseudo-randomly fights pairs of animals
// until only one remains
function resolveFight(animals) {
while(animals.length > 1){
// Shuffle list of animals
shuffle(animals);
// Make the first two animals fight!
var move0 = animals[0].fight(animals[1].name());
var move1 = animals[1].fight(animals[0].name());
var winner = playRockPaperScissorsSuicide(move0, move1);
if (winner === -1) {
// Both animals died, remove both from the list
animals.splice(0, 2)
} else {
// Remove the loser
animalsBaseCount[animals[winner].name()]--;
animals.splice(winner, 1);
}
}
return animals[0];
}
// Returns 0 if first move won, 1 if second move won. -1 for double suicide
function playRockPaperScissorsSuicide(move0, move1){
var winner = -1; // No winners!
if (move0 === move1) {
// A draw!
if (move0 === 'x') {
// Double Suicide! Return -1
} else {
// Return a random winner
return Math.floor(Math.random()*2);
}
} else {
// Suicides
if (move0 === 'x') { return 1; }
if (move1 === 'x') { return 0; }
switch (move0 + move1) {
case 'rp':
winner = 1;
break;
case 'rs':
winner = 0;
break;
case 'pr':
winner = 0;
break;
case 'ps':
winner = 1;
break;
case 'sr':
winner = 1;
break;
case 'sp':
winner = 0;
break;
}
}
return winner;
}
// http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
function shuffle(array) {
var currentIndex = array.length
, temporaryValue
, randomIndex
;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
}
SurvivalGame.prototype.isFinished = function() {
return this.currentTurn >= this.maxTurn;
}
SurvivalGame.prototype.getProgress = function() {
// Returns the percentage of the game that has completed as an int
return Number((100 * (this.currentTurn / this.maxTurn)).toFixed());
}