-
Notifications
You must be signed in to change notification settings - Fork 0
/
ships.py
58 lines (41 loc) · 1.41 KB
/
ships.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
# -*- coding: utf-8 -*-
class Ship(object):
def __init__(self, shipName):
self.name = shipName
self.health=100
if(shipName == 'Carrier'):
self.size=5
elif(shipName == 'Battleship'):
self.size=4
elif(shipName == 'Cruiser'):
self.size=3
elif(shipName == 'Submarine'):
self.size=3
elif(shipName == 'Destroyer'):
self.size=2
def getHealth(self):
return self.health
def getSize(self):
return self.size
def getName(self):
return self.name
def hit(self):
self.health*=1-(1/self.size)
def getLocation(self):
''' Gets the ships location on the game board '''
return self.location
def setLocation(self,x,y,direction):
''' Sets the ships location on the game board '''
self.location = [x,y,direction]
class Armada(object):
def __init__(self):
self.armada = [Ship('Carrier'),Ship('Battleship'),Ship('Cruiser'),Ship('Submarine'),Ship('Destroyer')]
def printShips(self):
for ship in self.armada:
print(f"Ship: {ship.getName()}")
def addShip(self, shipType):
self.armada.append(Ship(shipType))
def __len__(self):
return len(self.armada)
def __getitem__(self, index):
return self.armada[index]