-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrisk_europe.html
416 lines (339 loc) · 9.19 KB
/
risk_europe.html
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
<!-- SPDX-License-Identifier: 0BSD -->
<!doctype html>
<html><head>
<title>Risk: Europe</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>Risk: Europe<br>
Combat Resolver<br><br>
<script>
// Returns a random number between 1 and 6
function roll()
{
return Math.floor(Math.random() * 6) + 1;
}
// Rolls a die 'count' times and returns the number of dice with value >= 'minimum'
function rolls(count, minimum, player)
{
var hits = 0;
var o = document.getElementById("output");
if (count == 0) {
return 0;
}
if (player !== undefined) {
o.innerHTML += "<br>" + player + " rolls ";
}
for (var i = 0; i < count; i++) {
var r = roll();
if (player !== undefined) {
o.innerHTML += r + " ";
}
if (r >= minimum) {
hits++;
}
}
if (player !== undefined) {
o.innerHTML += " and gets " + hits + " hits\n";
}
return hits;
}
function sum(x, y)
{
return x + y;
}
// Removes 'count' of the lowest-value units from player 'p'
// Returns true if the army is eliminated
function remove(p, count)
{
if (count >= p.reduce(sum)) {
p[0] = 0;
p[1] = 0;
p[2] = 0;
p[3] = 0;
return true;
}
while (count) {
count--;
// Sacrifice Footmen first
if (p[3]) {
p[3]--;
continue;
}
// Then Archers
if (p[1]) {
p[1]--;
continue;
}
// Then Cavalry
if (p[2]) {
p[2]--;
continue;
}
// And finally Siege Weapons
p[0]--;
}
return false;
}
function show_units(a, d)
{
var o = document.getElementById("output");
if (a.reduce(sum) == 0) {
o.innerHTML += "<br>Attacker: ELIMINATED\n";
} else {
o.innerHTML += "<br>Attacker: " + a[0] + " siege, " + a[1] + " archer, " + a[2] + " cavalry, " + a[3] + " footmen\n";
}
if (d.reduce(sum) == 0) {
o.innerHTML += "<br>Defender: ELIMINATED\n";
} else {
o.innerHTML += "<br>Defender: " + d[0] + " siege, " + d[1] + " archer, " + d[2] + " cavalry, " + d[3] + " footmen\n";
}
}
// For Ranks 1-3, eliminates units from both sides
// Returns true is one or both armies are eliminated
function do_hits(a, hits_on_attacker, d, hits_on_defender)
{
var done = false;
if (remove(a, hits_on_attacker)) {
done = true;
}
if (remove(d, hits_on_defender)) {
done = true;
}
return done;
}
// Performs Rank 1 attack
// Returns true is one or both armies are eliminated
function rank1(a, d, show)
{
var hits_on_defender, hits_on_attacker, done;
if (!a[0] && !d[0]) {
return false;
}
if (show) {
var o = document.getElementById("output");
o.innerHTML += "<br><br>Rank 1 -- Siege Weapons\n";
hits_on_defender = rolls(2 * a[0], 3, "Attacker");
hits_on_attacker = rolls(2 * d[0], 3, "Defender");
done = do_hits(a, hits_on_attacker, d, hits_on_defender);
show_units(a, d);
} else {
hits_on_defender = rolls(2 * a[0], 3);
hits_on_attacker = rolls(2 * d[0], 3);
done = do_hits(a, hits_on_attacker, d, hits_on_defender);
}
return done;
}
// Performs Rank 2 attack
// Returns true is one or both armies are eliminated
function rank2(a, d, show)
{
var hits_on_defender, hits_on_attacker, done;
if (!a[1] && !d[1]) {
return false;
}
if (show) {
var o = document.getElementById("output");
o.innerHTML += "<br><br>Rank 2 -- Archers\n";
hits_on_defender = rolls(a[1], 5, "Attacker");
hits_on_attacker = rolls(d[1], 5, "Defender");
done = do_hits(a, hits_on_attacker, d, hits_on_defender);
show_units(a, d);
} else {
hits_on_defender = rolls(a[1], 5);
hits_on_attacker = rolls(d[1], 5);
done = do_hits(a, hits_on_attacker, d, hits_on_defender);
}
return done;
}
// Performs Rank 3 attack
// Returns true is one or both armies are eliminated
function rank3(a, d, show)
{
var hits_on_defender, hits_on_attacker, done;
if (!a[2] && !d[2]) {
return false;
}
if (show) {
var o = document.getElementById("output");
o.innerHTML += "<br><br>Rank 3 -- Cavalry\n";
hits_on_defender = rolls(a[2], 3, "Attacker");
hits_on_attacker = rolls(d[2], 3, "Defender");
done = do_hits(a, hits_on_attacker, d, hits_on_defender);
show_units(a, d);
} else {
hits_on_defender = rolls(a[2], 3);
hits_on_attacker = rolls(d[2], 3);
done = do_hits(a, hits_on_attacker, d, hits_on_defender);
}
return done;
}
// Performs Rank 4 attack
// Returns true is one or both armies are eliminated
function rank4(a, d, show)
{
var a_count = a.reduce(sum);
var d_count = d.reduce(sum);
var a_rolls = [];
var d_rolls = [];
var done;
if (a_count >= 3) {
a_rolls = [roll(), roll(), roll()];
} else if (a_count == 2) {
a_rolls = [roll(), roll()];
} else {
a_rolls = [roll()];
}
if (d_count >= 2) {
d_rolls = [roll(), roll()];
} else {
d_rolls = [roll()];
}
a_rolls.sort(function(a, b) {return b - a});
d_rolls.sort(function(a, b) {return b - a});
if (show) {
var o = document.getElementById("output");
o.innerHTML += "<br><br>Rank 4 - All Units\n";
o.innerHTML += "<br>Attacker rolls: " + a_rolls + "\n"
o.innerHTML += "<br>Defender rolls: " + d_rolls + "\n"
}
if (d_rolls[0] >= a_rolls[0]) {
done = remove(a, 1);
} else {
done = remove(d, 1);
}
if (done) {
if (show) {
show_units(a, d)
}
return true;
}
if (d_rolls[1] >= a_rolls[1]) {
done = remove(a, 1);
} else {
done = remove(d, 1);
}
if (done) {
if (show) {
show_units(a, d)
}
return true;
}
if (show) {
show_units(a, d)
}
return false;
}
function predict()
{
var a0 = [parseInt(document.getElementById('s1').value),
parseInt(document.getElementById('a1').value),
parseInt(document.getElementById('c1').value),
parseInt(document.getElementById('f1').value)];
var d0 = [parseInt(document.getElementById('s2').value),
parseInt(document.getElementById('a2').value),
parseInt(document.getElementById('c2').value),
parseInt(document.getElementById('f2').value)];
var done = false;
var loops = 1000;
var success = 0;
var o = document.getElementById("output");
for (var i = 0; i < loops; i++) {
var a = a0.slice();
var d = d0.slice();
while (true) {
done = rank1(a, d, false);
if (done) {
break;
}
done = rank2(a, d, false);
if (done) {
break;
}
done = rank3(a, d, false);
if (done) {
break;
}
done = rank4(a, d, false);
if (done) {
break;
}
}
if (a.reduce(sum) > 0) {
success++;
}
}
o.innerHTML += "<br>Chance of successful attack: " + Math.round((100.0 * success) / loops) + "%\n";
return false;
}
function resolve()
{
var a = [parseInt(document.getElementById('s1').value),
parseInt(document.getElementById('a1').value),
parseInt(document.getElementById('c1').value),
parseInt(document.getElementById('f1').value)];
var d = [parseInt(document.getElementById('s2').value),
parseInt(document.getElementById('a2').value),
parseInt(document.getElementById('c2').value),
parseInt(document.getElementById('f2').value)];
var done = false;
while (true) {
done = rank1(a, d, true);
if (done) {
break;
}
done = rank2(a, d, true);
if (done) {
break;
}
done = rank3(a, d, true);
if (done) {
break;
}
done = rank4(a, d, true);
if (done) {
break;
}
}
return false;
}
function wipe()
{
var o = document.getElementById("output");
o.innerHTML = "";
}
</script>
<form id="form">
<table>
<th></th>
<th>Attacker</th>
<th>Defender</th>
</tr>
<tr>
<td>Siege Weapons:</td>
<td><input type="text" value="0" id="s1"></td>
<td><input type="text" value="0" id="s2"></td>
</tr>
<tr>
<td>Archers:</td>
<td><input type="text" value="0" id="a1"></td>
<td><input type="text" value="0" id="a2"></td>
</tr>
<tr>
<td>Cavalry:</td>
<td><input type="text" value="0" id="c1"></td>
<td><input type="text" value="0" id="c2"></td>
</tr>
<tr>
<td>Footmen:</td>
<td><input type="text" value="0" id="f1"></td>
<td><input type="text" value="0" id="f2"></td>
</tr>
</table>
<button onclick="return predict();">Predict</button>
<button onclick="return resolve();">Resolve</button>
<button onclick="wipe(); return false;">Clear</button>
<button onclick="wipe(); return true;">Reset</button>
</form>
<div id="output">
</div>
</body></html>