-
Notifications
You must be signed in to change notification settings - Fork 0
/
riskSimulationDifferentBattles2.m
executable file
·119 lines (89 loc) · 4.04 KB
/
riskSimulationDifferentBattles2.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
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
% simulates risk over multiple trials AND over multiple combinations of
% troops, in the same program
% main function associated with riskWinOrLose
function [probabilities winOrLose] = riskSimulationDifferentBattles2...
(num_attack, num_defense, num_trials, diceMax, attackDiceI, attackDiceF, ...
defenseDiceI, defenseDiceF, 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.
probabilities = cell(attackDiceF,defenseDiceF);
winOrLose = zeros(attackDiceF,defenseDiceF);
ATTACK = num_attack; % number of attacking troops
DEFENSE = num_defense; % number of defending troops
for x = attackDiceI : attackDiceF
for y = defenseDiceI : defenseDiceF
attackDiceLim = x;
defenseDiceLim = y;
probabilities{x,y} = zeros(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
% disp('Hi');
% disp(x);
% disp(y);
% disp(num_attack);
% disp('Goodbye');
probabilities{x,y}(num_attack) = probabilities{x,y}(num_attack) + 1;
if num_attack ~= 1
winOrLose(x,y) = winOrLose(x,y) + 1;
end
end
probabilities{x,y} = probabilities{x,y} / num_trials * 100;
end
end
winOrLose = winOrLose / num_trials;