-
Notifications
You must be signed in to change notification settings - Fork 2
/
example-simulator.php
119 lines (104 loc) · 3.11 KB
/
example-simulator.php
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
<?php
error_reporting(E_ALL);
require_once 'BattleEngine.php';
require_once 'OG.php';
use BattleEngine\BattleEngine;
use BattleEngine\Combatant;
use OG\OG;
$numSimulations = 100;
$attackers = [
new Combatant(
10,
10,
10,
[
OG::BATTLESHIP => 25000,
],
),
new Combatant(
10,
10,
10,
[
OG::LIGHT_FIGHTER => 25000,
OG::HEAVY_FIGHTER => 25000,
OG::CRUISER => 25000,
],
),
];
$defenders = [
new Combatant(
10,
10,
10,
[
OG::DEATH_STAR => 250,
],
),
];
function printCombatant(Combatant $combatant)
{
$weapons = $combatant->getWeaponsTechnology() * 10;
$shield = $combatant->getShieldingTechnology() * 10;
$armor = $combatant->getArmorTechnology() * 10;
printf(
"\tWeapons %d%% Shield %d%% Armor %d%%\n",
$weapons,
$shield,
$armor
);
$unitGroups = $combatant->getUnitGroups();
foreach ($unitGroups as $kind => $count) {
printf("\t%s %d\n", OG::$names[$kind], $count);
}
}
function printCombatants(string $who, array $combatants)
{
foreach ($combatants as $i => $combatant) {
printf("%s #%d\n", $who, $i);
printCombatant($combatant);
printf("\n");
}
}
function mean(array $values): float
{
return array_sum($values) / count($values);
}
function stdev(array $values): float
{
$mean = array_sum($values) / count($values);
$var = 0.0;
foreach ($values as $val) {
$var += pow($val - $mean, 2);
}
return sqrt($var / (count($values) - 1));
}
function printCombatantResult(Combatant $combatant, array $stats)
{
$unitGroups = $combatant->getUnitGroups();
foreach ($unitGroups as $kind => $count) {
printf("\t%s:\n", OG::$names[$kind]);
$numRemainingUnits = array_map(fn($s) => $s[$kind]->getNumRemainingUnits(), $stats);
printf("\t\tMean: %f\n", mean($numRemainingUnits));
printf("\t\tStdev: %f\n", stdev($numRemainingUnits));
printf("\t\tMin: %d\n", min($numRemainingUnits));
printf("\t\tMax: %d\n", max($numRemainingUnits));
}
}
function printCombatantResults(string $who, array $combatants, array $simulations, callable $getOutcomes)
{
foreach ($combatants as $i => $combatant) {
printf("%s #%d\n", $who, $i);
$stats = array_map(fn($s) => $getOutcomes($s)[$i]->getRoundStats($s->getNumRounds() - 1), $simulations);
printCombatantResult($combatant, $stats);
printf("\n");
}
}
printCombatants('Attacker', $attackers);
printCombatants('Defender', $defenders);
$battleEngine = new BattleEngine('./build/BattleEngine', OG::$unitsAttributes);
$simulations = $battleEngine->simulate($attackers, $defenders, 0, $numSimulations);
$numRounds = mean(array_map(fn($s): int => $s->getNumRounds(), $simulations));
printf("Num rounds: %f\n\n", $numRounds);
printCombatantResults('Attacker', $attackers, $simulations, fn($s) => $s->getAttackersOutcomes());
printCombatantResults('Defender', $defenders, $simulations, fn($s) => $s->getDefendersOutcomes());