hnc-eduard/HNS/Excercises/ShipCraft/ShipField.py

58 lines
2.0 KiB
Python
Raw Normal View History

from main import ShootResult
class ShipField:
field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
field_size = 10
field_mode = 0
ship_size = 4
ship_direction = 0
def __init__(self):
pass
def set_ship(self, row, col):
if row < 0 or row > self.field_size:
return
if col < 0 or col > self.field_size:
return
index = row * self.field_size + col
if self.ship_direction == 0:
if self.field_size - row < self.ship_size:
return
for r in range(row, row + self.ship_size):
index = r * self.field_size + col
self.field[index] = "1"
if self.ship_direction == 1:
if self.field_size - col < self.ship_size:
return
for c in range(col, col + self.ship_size):
index = row * self.field_size + c
self.field[index] = "1"
def shoot(self, row, col):
if row < 0 or row > self.field_size - 1:
return ShootResult.UNDEFINED
if col < 0 or col > self.field_size - 1:
return ShootResult.UNDEFINED
index = row * self.field_size + col
if (self.field[index]).strip() == "":
self.field[index] = "0"
return ShootResult.EMPTY
elif (self.field[index]).strip() == "1":
self.field[index] = "\\"
return ShootResult.DAMAGED
else:
return ShootResult.UNDEFINED