-
Notifications
You must be signed in to change notification settings - Fork 2
/
Risk_Battle.m
79 lines (55 loc) · 1.7 KB
/
Risk_Battle.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
function [AttackArmy, DefendArmy] = Risk_Battle(AttackArmy, DefendArmy)
% Ties defenders win
%AttackArmy = 10;
%DefendArmy = 10;
% How many to attack with
countervariable = 0;
if AttackArmy > 3
AttackDice = 3;
elseif AttackArmy > 2
AttackDice = 2;
elseif AttackArmy > 1
AttackDice = 1;
end
if DefendArmy >= 2
DefendDice = 2;
elseif DefendArmy == 1
DefendDice = 1;
end
for DefendRoll = 1:DefendDice
DefendRolls(1, DefendRoll) = randi(6);
end
for AttackRoll = 1:AttackDice
AttackRolls(1, AttackRoll) = randi(6);
end
if AttackDice > 1
for temp = 1:DefendDice
if AttackArmy > 0
[ValueA, LocationA] = max(AttackRolls);
[ValueD, LocationD] = max(DefendRolls);
if ValueA > ValueD
DefendRolls(1,LocationD) = 0;
AttackRolls(1,LocationA) = 0;
DefendArmy = DefendArmy - 1;
else
DefendRolls(1,LocationD) = 0;
AttackRolls(1,LocationA) = 0;
AttackArmy = AttackArmy - 1;
end
end
end
else
if AttackArmy > 0
[ValueA, LocationA] = max(AttackRolls);
[ValueD, LocationD] = max(DefendRolls);
if ValueA > ValueD
DefendRolls(1,LocationD) = 0;
AttackRolls(1,LocationA) = 0;
DefendArmy = DefendArmy - 1;
else
DefendRolls(1,LocationD) = 0;
AttackRolls(1,LocationA) = 0;
AttackArmy = AttackArmy - 1;
end
end
end