import random import copy class MakeArmy def __init__ self team self i

  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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import random
import copy
class MakeArmy:
def __init__(self, team):
self.inf = 0; self.tank = 0; self.fight = 0; self.bomb = 0
self.trans = 0; self.sub = 0; self.carr = 0; self.batt = 0
self.battb = 0; self.dbatt = 0; self.aa_gun = 0; army_ready = 0
while army_ready != 1:
print "\nEnter 0 when all %s units have been entered\n" % team
print 'Current %s force:' % team
print self.inf, 'Infantry'
print self.tank, 'Tank(s)'
print self.fight, 'Fighter(s)'
print self.bomb, 'Bomber(s)'
print self.trans, 'Transport(s)'
print self.sub, 'Submarine(s)'
print self.carr, 'Carrier(s)'
print self.batt / 2, 'Battleship(s)'
if team == 'defending':
print self.aa_gun, 'AA gun(s)'
else:
print self.battb, 'Bombarding battleships'
print '\n1. Infantry\n2. Tank\n3. Fighter\n4. Bomber\n5. Transport\
\n6. Submarine\n7. Carrier\n8. Battleship'
if team == 'defending':
print '9. AA Gun'
else:
print '9. Bombarding battleship'
try:
choice = input('\nEnter the number to add a type of unit')
if choice == 0:
army_ready = 1
elif choice == 1:
self.inf = input('How many?')
elif choice == 2:
self.tank = input('How many?')
elif choice == 3:
self.fight = input('How many?')
elif choice == 4:
self.bomb = input('How many?')
elif choice == 5:
self.trans = input('How many?')
elif choice == 6:
self.sub = input('How many?')
elif choice == 7:
self.carr = input('How many?')
elif choice == 8:
self.batt = input('How many?') * 2
elif (choice == 9) and (team == 'attacking'):
self.battb = input('How many?')
elif (choice == 9) and (team == 'defending'):
self.aa_gun = 1
except:
print 'Invalid input'
self.units = {'inf':{'amount':self.inf, 'skill':(1, 2)},
'tank':{'amount':self.tank, 'skill':(3, 2)},
'fight':{'amount':self.fight, 'skill':(3, 4)},
'bomb':{'amount':self.bomb, 'skill':(4, 1)},
'sub':{'amount':self.sub, 'skill':(2, 2)},
'carr':{'amount':self.carr, 'skill':(1, 3)},
'batt':{'amount':self.batt, 'skill':(4, 4)},
'dbatt':{'amount':self.dbatt, 'skill':(4, 4)}}
def choose_mission(self, team):
self.protected = 0
print 'Choose a mission for the %s team:\n\n1. Regular\
\n2. Protect [x unit type] at all costs' % team
if team == 'attacking':
print '3. Occupy enemy territory at all costs'
self.mission = input('\nType the number for a mission: ')
self.death_order = (self.batt, 'batt'), (self.inf, 'inf'),\
(self.tank, 'tank'), (self.trans, 'trans'), (self.sub, 'sub'),\
(self.fight, 'fight'), (self.bomb, 'bomb'),\
(self.carr, 'carr'), (self.dbatt, 'dbatt')
if self.mission == 2:
print '\n1. Battleship\n2. Infantry\n3. Tank\n4. Transport\
\n5. Submarine\n6. Fighter\n7. Bomber\n8. Carrier\
\n9. Damaged Battleship'
self.protected = input('\nWhat unit type do you want to protect? ')
self.death_order.append((self.death_order.pop(self.protected - 1)))
def roll(self, stance):
self.hits = 0; self.sub_insta_kills = 0
self.d_sub_hits = 0; batt_present = 0
if self.batt > 0:
self.batt /= 2
#^each 2 hit battleship only takes one shot each
batt_present = 1
for i in self.units:
if self.units[i]['amount'] > 0:
skill = self.units[i]['skill'][stance]
for j in xrange(self.units[i]['amount']):
if random.randint(1, 6) <= skill:
if (i == 'sub') and (stance == 0):
self.sub_insta_kills += 1
elif (i == 'sub') and (stance == 1):
self.d_sub_hits += 1
else:
self.hits += 1
if batt_present == 1:
self.batt *= 2 #restore original battleship value
def remove_casualties(self, opposing_hits, d_sub_hits):
passer = 0; go = 1
while go = 1:
if (i[0] > 0) and ((opposing_hits > 0) or (d_sub_hits > 0)):
if ((opposing_hits >= self.death_order[ + self.tank) and \
(self.mission == 3)) and ((i[1] == 'inf') or\
(i[1] == 'tank')):
if (self.inf == 0) and (self.tank == 1):
self.death_order.append\
(self.death_order.pop(self.tank))
break
elif (self.inf == 1) and (self.tank == 0):
self.death_order.append\
(self.death_order.pop(self.inf))
break
else:
i[0] -= 1
opposing_hits -= 1
self.units[i][0] =
elif i[1] == 'batt':
while (opposing_hits > 0) and (i[0] > 0):
i[0] -= 2 #battleship - 2
opposing_hits -= 1
units[9][0] += 1 #dbatt + 1
while (d_sub_hits > 0) and (i[0] > 0):
i[0] -= 2
d_sub_hits -= 1
units[9][0] += 1
elif (i[1] == 'fight') or (i[1] == 'bomb'):
passer += 1
while (opposing_hits > 0) and (i[0] > 0):
i[0] -= 1
opposing_hits -= 1
elif i[3] == 'dummy':
d_sub_hits = 0
else:
i[0] -= 1
opposing_hits -= 1
if passer >= 3:
d_sub_hits = 0
counter = 0
for i in self.death_order:
counter += 1
if counter == 0: go = 0
attacker = MakeArmy('attacking')
attacker.choose_mission('attacking')
defender = MakeArmy('defending')
defender.choose_mission('defending')
attacker.roll(0)
defender.roll(1)
print attacker.hits
print defender.hits
raw_input('exit')