-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrisk_simulation.py
More file actions
95 lines (76 loc) · 2.87 KB
/
risk_simulation.py
File metadata and controls
95 lines (76 loc) · 2.87 KB
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
from ast import Break
import numpy as np
#for rng, need to generate uniform distribution for range of dice events (1, 6) because each event has an equal probability of occuring.
class Battle():
def __init__(self, attackers, defenders):
self.attackers = attackers - 1 #one attacker must always remain in reserve
self.defenders = defenders
self.starting_attackers = attackers
self.starting_defenders = defenders
self.attackers_lost = 0
self.defenders_lost = 0
self.battle_count = 0
self.winner = "Battle Incomplete"
def outcome(self):
battle_finished = False
while battle_finished == False:
if self.attackers == 0:
battle_finished = True
self.winner = "Defender"
break
if self.defenders == 0:
battle_finished = True
self.winner = "Attacker"
break
if self.attackers >= 3:
a = 3
else:
a = self.attackers
if self.defenders >= 2:
d = 2
else:
d = self.attackers
self.rolls(a, d)
self.battle_count += 1
print(
"""
______________________________
======= Battle Summary =======
~~ Starting Armies ~~
Attackers: {} Defenders: {}
~~ Number of Battles ~~
{}
~~ Armies Lost ~~
Attackers: {} Defenders: {}
~~ Armies Remaining ~~
Attackers: {} Defenders: {}
*** WINNER ***
{}
______________________________
""".format(self.starting_attackers, self.starting_defenders,
self.battle_count, self.attackers_lost, self.defenders_lost,
self.attackers, self.defenders,
self.winner)
)
def rolls(self, a_rolls, d_rolls):
attacks = []
defends = []
for roll in range(a_rolls):
attacks.append(np.random.randint(1,6))
for roll in range(d_rolls):
defends.append(np.random.randint(1,6))
for fight in range(d_rolls):
if attacks.count == 0:
Break
a = max(attacks)
attacks.remove(max(attacks))
d = max(defends)
defends.remove(max(defends))
if d >= a:
self.attackers -= 1
self.attackers_lost += 1
print('Attacker Roll: {} loses to Defender Roll: {}'.format(a, d))
else:
self.defenders -= 1
self.defenders_lost += 1
print('Attacker Roll: {} beats Defender Roll: {}'.format(a, d))