-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
305 lines (258 loc) · 10.4 KB
/
main.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
function isPositiveInt(mynumber) {
return (Number.isInteger(mynumber) && mynumber > 0);
}
function arePositiveInts(mynumber1, mynumber2) {
return (isPositiveInt(mynumber1) && isPositiveInt(mynumber2));
}
// Roll the dice for one round of battle
function rollDice(unitsAttack, unitsDefend, roundInfoDict) {
// Determine the rolls for attacker and defender and sort them
// from highest to lowest
let rollsAttack = Array(unitsAttack).fill(0);
let rollsDefend = Array(unitsDefend).fill(0);
for (i=0; i<rollsAttack.length; i++)
rollsAttack[i] = Math.floor(Math.random() * 6) + 1;
for (i=0; i<rollsDefend.length; i++)
rollsDefend[i] = Math.floor(Math.random() * 6) + 1;
rollsAttack.sort().reverse();
rollsDefend.sort().reverse();
// Add the rolls to the info about the round
roundInfoDict.rollsAttack = rollsAttack;
roundInfoDict.rollsDefend = rollsDefend;
return [rollsAttack, rollsDefend];
}
// Determine the losses from one round of battle
function battleOneRound(unitsAttack, unitsDefend, roundInfoDict) {
let [rollsAttack, rollsDefend] = rollDice(unitsAttack, unitsDefend,
roundInfoDict);
// Determine the losses to both the attacker and defender
let lossAttack = 0;
let lossDefend = 0;
let wins = []
for (i=0; i<Math.max(rollsAttack.length, rollsDefend.length); i++) {
wins[i] = 'n';
};
for (i=0; i<Math.min(rollsAttack.length, rollsDefend.length); i++) {
if (rollsAttack[i] > rollsDefend[i]) {
lossDefend++;
wins[i] = 'a';
}
else {
lossAttack++;
wins[i] = 'd'
}
}
return [lossAttack, lossDefend, wins];
}
function getDieImage(roll, color) {
let img_div = document.createElement("div");
let img_color = document.createElement("img");
let img = document.createElement("img");
img_div.className = "die-image-parent";
img_color.className = "die-image-color";
img.className = "die-image";
img_color.src = "img/" + color + ".png";
img.src = "img/die_" + roll.toString() + ".png";
img_div.append(img_color, img);
return img_div;
}
function getDieImageInBattle(roll, attackOrDefend, result) {
let color = null;
if (attackOrDefend == "attack") {
if (result == "a") {color = "green"};
if (result == "d") {color = "red"};
if (result == "n") {color = "grey"};
}
else if (attackOrDefend == "defend") {
if (result == "a") {color = "red"};
if (result == "d") {color = "green"};
if (result == "n") {color = "grey"};
}
let img_div = getDieImage(roll, color);
return img_div;
}
function getSoldierImage(isLost) {
let img = document.createElement("img");
if (isLost == false) {img.className = "soldier-image"}
else if (isLost == true) {img.className = "soldier-image-lost"}
img.src = "img/soldier.png";
return img
}
// Render the results from a single set of rolls
function renderRoundInfo(roundInfoDict) {
let d = roundInfoDict; // Make code concise
let results_row = document.createElement("div");
results_row.className = "results-row";
let roundTitle = document.createElement("div");
roundTitle.className = "round-title"
roundTitle.append(document.createElement("br"),
document.createTextNode("Round " + d.roundNumber));
// First column, labels
let c1 = document.createElement("div");
c1.className = "col-1";
c1.append(document.createElement("br"),
document.createTextNode("Attackers"),
document.createElement("br"),
document.createElement("br"),
document.createTextNode("Defenders"),
document.createElement("br"));
// Second column, dice rolls
let c2 = document.createElement("div");
c2.className = "col-2";
for (i=0; i<d.rollsAttack.length; i++) {
c2.append(getDieImageInBattle(d.rollsAttack[i], "attack", wins[i]));
}
c2.append(document.createElement("br"));
for (i=0; i<d.rollsDefend.length; i++) {
c2.append(getDieImageInBattle(d.rollsDefend[i], "defend", wins[i]));
}
// Third column, armies
let c3 = document.createElement("div");
c3.className = "col-3";
for (i=0; i<d.unitsAttackLeftAtEnd; i++) {
c3.append(getSoldierImage(false));
}
for (i=0; i<d.unitsAttackLeftAtStart-d.unitsAttackLeftAtEnd; i++) {
c3.append(getSoldierImage(true));
}
c3.append(document.createElement("br"));
for (i=0; i<d.unitsDefendLeftAtEnd; i++) {
c3.append(getSoldierImage(false));
}
for (i=0; i<d.unitsDefendLeftAtStart-d.unitsDefendLeftAtEnd; i++) {
c3.append(getSoldierImage(true));
}
// Put it all together
results_row.append(roundTitle);
results_row.appendChild(c1);
results_row.appendChild(c2);
results_row.appendChild(c3);
document.getElementById("div_results").appendChild(results_row);
document.getElementById("div_results").append(
document.createElement("br"),
document.createElement("br"),
);
}
// Render the summary at the end of the battle
function renderSummaryInfo(roundInfoDict,
unitsAttackTotal,
unitsDefendTotal) {
let parentDiv = document.createElement("div");
parentDiv.className = "summary-parent";
let title = document.createElement("div");
title.className = "summary-title";
title.append(document.createTextNode("Summary (units left)"));
let d = roundInfoDict; // Make code concise
let results_row = document.createElement("div");
results_row.className = "results-row";
// First column, labels
let c1 = document.createElement("div");
c1.className = "summary-col-1";
c1.append(document.createElement("br"),
document.createTextNode("Attackers"),
document.createElement("br"),
document.createElement("br"),
document.createTextNode("Defenders"),
document.createElement("br"));
// Second column, units left and lost
let unitsAttackLost = unitsAttackTotal - d.unitsAttackLeftAtEnd;
let unitsDefendLost = unitsDefendTotal - d.unitsDefendLeftAtEnd;
let c2 = document.createElement("div");
c2.className = "summary-col-2";
let boldUnitsAttackLeftTxt = document.createElement("strong");
let unitsAttackLeftTxt = document.createTextNode(d.unitsAttackLeftAtEnd);
boldUnitsAttackLeftTxt.appendChild(unitsAttackLeftTxt);
let boldUnitsDefendLeftTxt = document.createElement("strong");
let unitsDefendLeftTxt = document.createTextNode(d.unitsDefendLeftAtEnd);
boldUnitsDefendLeftTxt.appendChild(unitsDefendLeftTxt);
let unitsAttackLostTxt = document.createTextNode(
" (lost " + unitsAttackLost + ")"
);
let unitsDefendLostTxt = document.createTextNode(
" (lost " + unitsDefendLost + ")"
);
c2.append(document.createElement("br"),
boldUnitsAttackLeftTxt,
unitsAttackLostTxt,
document.createElement("br"),
document.createElement("br"),
boldUnitsDefendLeftTxt,
unitsDefendLostTxt,
document.createElement("br"));
// Third column, armies
let c3 = document.createElement("div");
c3.className = "col-3";
for (i=0; i<d.unitsAttackLeftAtEnd; i++) {
c3.append(getSoldierImage(false));
}
for (i=0; i<unitsAttackTotal-d.unitsAttackLeftAtEnd; i++) {
c3.append(getSoldierImage(true));
}
c3.append(document.createElement("br"));
for (i=0; i<d.unitsDefendLeftAtEnd; i++) {
c3.append(getSoldierImage(false));
}
for (i=0; i<unitsDefendTotal-d.unitsDefendLeftAtEnd; i++) {
c3.append(getSoldierImage(true));
}
// Put it all together
results_row.append(c1, c2, c3);
parentDiv.append(title, results_row);
document.getElementById("div_results").appendChild(parentDiv);
}
function battleUntilEnd(unitsAttackTotal, unitsDefendTotal) {
if (!arePositiveInts(unitsAttackTotal, unitsDefendTotal))
window.alert("Error: Attacking and defending units "
+ "should be whole numbers.");
// Remove any results nodes that are already there (in case
// a battle is run more than once)x
let divResults = document.getElementById("div_results");
while (divResults.firstChild) {
divResults.removeChild(divResults.firstChild);
}
// Create new variables (*not* references to the same object -- these
// are different variables) that track the amount of units left
let unitsAttackLeft = unitsAttackTotal;
let unitsDefendLeft = unitsDefendTotal;
let lossAttack;
let lossDefend;
let roundNumber = 1;
let par;
while (Math.min(unitsAttackLeft, unitsDefendLeft) > 0) {
var roundInfoDict = {};
roundInfoDict.roundNumber = roundNumber;
roundInfoDict.unitsAttackLeftAtStart = unitsAttackLeft;
roundInfoDict.unitsDefendLeftAtStart = unitsDefendLeft;
// Do the actual battle
let unitsAttackRound = Math.min(unitsAttackLeft, 3);
let unitsDefendRound = Math.min(unitsDefendLeft, 2);
[lossAttack, lossDefend, wins] = battleOneRound(unitsAttackRound,
unitsDefendRound,
roundInfoDict);
unitsAttackLeft -= lossAttack;
unitsDefendLeft -= lossDefend;
roundNumber++;
// Add info to the dictionary summarising this round
roundInfoDict.unitsAttackRound = unitsAttackRound;
roundInfoDict.unitsDefendRound = unitsDefendRound;
roundInfoDict.lossAttack = lossAttack;
roundInfoDict.lossDefend = lossDefend;
roundInfoDict.wins = wins;
roundInfoDict.unitsAttackLeftAtEnd = unitsAttackLeft;
roundInfoDict.unitsDefendLeftAtEnd = unitsDefendLeft;
// Render the round info into HTML
renderRoundInfo(roundInfoDict);
console.log(roundInfoDict);
}
renderSummaryInfo(roundInfoDict, unitsAttackTotal, unitsDefendTotal);
return 0;
}
function battleFromBattleButton() {
let unitsAttackTotal = (
Number(document.getElementById("in_attack_units").value)
);
let unitsDefendTotal = (
Number(document.getElementById("in_defend_units").value)
);
let output = battleUntilEnd(unitsAttackTotal, unitsDefendTotal);
}