-
Notifications
You must be signed in to change notification settings - Fork 0
/
squadrons.py
144 lines (117 loc) · 4.26 KB
/
squadrons.py
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#! /usr/bin/python3
"""Provides classes and methods to simulate squadron battles in Star Wars Armada.
Each squadron will have hull and be able to roll for attack.
"""
from random import choice
# Die faces if undefined are blank
class Face:
def __init__(self, color, hits=0, crits=0, accuracies=0):
self.color = color
self.hits = hits
self.crits = crits
self.accuracies = accuracies
def __repr__(self):
return f'color={self.color}, hits={self.hits}, crits={self.crits}, accuracies={self.accuracies}'
red = (Face("red", hits=1),
Face("red", hits=1),
Face("red", crits=1),
Face("red", crits=1),
Face("red", accuracies=1),
Face("red", hits=2),
Face("red", hits=1),
Face("red", crits=1),
Face("red"),
Face("red"))
blue = (Face("blue", hits=1),
Face("blue", hits=1),
Face("blue", hits=1),
Face("blue", hits=1),
Face("blue", crits=1),
Face("blue", crits=1),
Face("blue", accuracies=1),
Face("blue", accuracies=1))
black = (Face("black", hits=1),
Face("black", hits=1),
Face("black", hits=1),
Face("black", hits=1),
Face("black", hits=1, crits=1),
Face("black", hits=1, crits=1),
Face("black"),
Face("black"))
faces = {"red": red,
"blue": blue,
"black": black}
# After creating a Die with d1 = Die("red", red), d1.roll will return a Face
class Die:
"""A single attack die"""
def __init__(self, color, die_faces):
self.color = color
self.faces = die_faces
@property
def roll(self):
return choice(self.faces)
class Squadron:
"""The parent class for all squadrons."""
def __init__(self, model, hull, dice_colors):
self.kind = model
self.hull = hull
self.dice = []
for die_color in dice_colors:
self.dice.append(Die(die_color, faces[die_color]))
@property
def roll(self, dice=None):
# Roll all the die and return the result
attack = []
# Can pass in dice to roll. Default is to roll all dice.
if dice is None:
dice = self.dice
for die in dice:
result = die.roll
attack.append(result)
return attack
def defend(self, attack):
# Given at attack from an enemy wing, the squadron defends itself, or takes damage.
pass
class TieFighter(Squadron):
"""Tie fighter squadron, implementing Swarm. For now, until I can figure out how to
have the definition of the 'worst' die vary by the opponent, I will just re-roll the
first result that did not result in a hit."""
def __init__(self):
super(TieFighter, self).__init__(model="TIE Fighter Squadron", hull=3, dice_colors=["blue", "blue", "blue"])
def roll(self, dice=None, wing=None):
# Use the parent method to start.
attack = super().roll
if len(wing) > 1:
# The enemy is engaged by two fighters, so we can use swarm.
for i, result in enumerate(attack):
if result.hits is 0:
# re-roll this, then stop
new_color = result.color
del attack[i]
new_die = Die(new_color, faces[new_color])
attack.insert(i, new_die.roll)
break
return attack
class Xwing(Squadron):
"""X-wings have escort. I will need to figure how to model that."""
def __init__(self):
super(Xwing, self).__init__(model="X-wing Squadron ", hull=5, dice_colors=["blue", "blue", "blue", "blue"])
def roll(self, dice=None, wing=None):
attack = super().roll
return attack
class Wing:
"""A wing is composed of one or more squadrons."""
def __init__(self, squadrons):
self.squadrons = squadrons
def attack(enemy_wing):
"""Given an enemy wing, selects a squadron to attack in the enemy wing."""
targets = sorted(enemy_wing, key=lambda x: x.hull, reverse=True)
if __name__ == "__main__":
t1 = TieFighter()
t2 = TieFighter()
imperial_wing = [t1, t2]
print(t1.roll(wing=imperial_wing))
print(t2.roll(wing=imperial_wing))
x1 = Xwing()
rebel_wing = [x1]
print(x1.roll(wing=rebel_wing))