-
Notifications
You must be signed in to change notification settings - Fork 0
/
riskSimulationSingleBattleFlex.m
executable file
·92 lines (71 loc) · 3 KB
/
riskSimulationSingleBattleFlex.m
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
% Main Function associated with risk simulator GUI
% flexibility in changing number of dice that is rolled
function probabilities = riskSimulationSingleBattleFlex...
(num_attack, num_defense, num_trials, diceMax, attackDiceLim, ...
defenseDiceLim, numConsideredLim)
% Attack and defense always roll as many dice as they can, but bounded by
% the number of dice that are specified for both sides.
% For the attack, the greatest number of dice it can roll is one less than
% the total number of remaining troops. The greatest number of dice the
% defense can roll, however, is the number of defending troops.
% We are always considering the greatest number of troops possible,
% bounded by the specified number considered parameter. The number of
% troops is the smallest of either the parameter itself, one less than the
% number of attack dice, or the number of defense dice.
ATTACK = num_attack; % number of attacking troops
DEFENSE = num_defense; % number of defending troops
probabilities = zeros(num_attack, 1);
for k = 1 : num_trials
num_attack = ATTACK;
num_defense = DEFENSE;
while num_attack > 1 && num_defense > 0
% figuring out the number of dice
if attackDiceLim == 0
num_diceAttack = num_attack - 1;
elseif num_attack - 1 > attackDiceLim
num_diceAttack = attackDiceLim;
else
num_diceAttack = num_attack - 1;
end
if defenseDiceLim == 0
num_diceDefense = num_defense;
elseif num_defense > defenseDiceLim
num_diceDefense = defenseDiceLim;
else
num_diceDefense = num_defense;
end
% figuring out number of considered:
if num_diceAttack >= numConsideredLim && num_diceDefense >= numConsideredLim
num_considered = numConsideredLim;
elseif num_diceAttack <= num_diceDefense
num_considered = num_diceAttack;
else
num_considered = num_diceDefense;
end
rollsA = [];
% roll and sort dice
for j = 1 : num_diceAttack
rollsA(j) = floor(rand()*diceMax) + 1;
end
rollsA = sort(rollsA, 'descend');
rollsD = [];
for j = 1 : num_diceDefense
rollsD(j) = floor(rand()*diceMax) + 1;
end
rollsD = sort(rollsD, 'descend');
% decide number of lost troops
countA = 0; % this counts how many dice the attack loses
countD = 0; % this counts how many dice the defense loses
for j = 1 : num_considered
if rollsD(j) >= rollsA(j)
countA = countA + 1;
else
countD = countD + 1;
end
end
num_attack = num_attack - countA;
num_defense = num_defense - countD;
end
probabilities(num_attack) = probabilities(num_attack) + 1;
end
probabilities = probabilities / num_trials * 100;