-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSHIP.py
65 lines (54 loc) · 2.31 KB
/
SHIP.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
# Ship class
from random import randint
import BOARD
class Ship(object):
""" The Ship object, stored in the shipsMap of the board"""
def __init__(self, symbol, length):
""" symbol - a single char. length - int """
self.length = length #The original length. Doesn't change
self.remaining = length #Decreases when ship is hit
self.symbol = symbol # the character representation the ship will take on the board
def set_randomly(self, board):
""" Randomly places the ship on a open part of the map
Inputs: self and the board to place the ship on
Outputs: None """
# 1 represents Horozontal and 2 represents Vertical
ship_ori = randint(1,2)
if ship_ori == 1: # Horizontal
while True:
ship_row = randint(0, board.get_height() - 1)
ship_col = randint(0, board.get_length() - self.length)
# check if location is free
for i in range(0, self.length):
if board.shipsMap[ship_row][ship_col + i] != None:
break #something there, go through while-loop again
else:
break #once done for-loop exit while-loop
# place the ship
for i in range(0, self.length):
board.shipsMap[ship_row][ship_col + i] = self
elif ship_ori == 2: # Vertical
while True:
ship_row = randint(0, board.get_height() - self.length)
ship_col = randint(0, board.get_length() - 1)
# check if the location is free
for i in range(0, self.length):
if board.shipsMap[ship_row + i][ship_col] != None:
break
else:
break
# place the ship
for i in range(0,self.length):
board.shipsMap[ship_row + i][ship_col] = self
def hit(self):
""" Ship has been hit.
Inputs: None
Outputs: True if sunk otherwise False"""
self.remaining -= 1
if self.remaining == 0:
return True
return False
def reset_remaining(self):
""" Puts the ship back to full health
Inputs: None. Outputs: None"""
self.remaining = self.length