hnc-daniil/HNC/Exercises/Ship_Battle/ShipField.py

255 lines
8.8 KiB
Python
Raw Normal View History

2024-04-29 19:05:10 +02:00
import copy
2024-03-04 08:46:18 +01:00
from ShootResult import ShootResult
2024-04-16 11:47:52 +02:00
from ShipMode import ShipMode
from ShipDirection import ShipDirection
2024-02-26 18:34:51 +01:00
class ShipField:
def __init__(self):
2024-03-04 08:46:18 +01:00
self.field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
2024-03-13 09:51:59 +01:00
self.ships = [4, 3, 3, 2, 2, 2, 1, 1, 1, 1]
2024-03-04 08:46:18 +01:00
self.field_size = 10
2024-04-16 11:47:52 +02:00
self.field_mode = ShipMode.PUT
2024-03-04 08:46:18 +01:00
self.ship_size = 4
2024-04-16 11:47:52 +02:00
self.ship_direction = ShipDirection.VERTICAL
2024-03-04 08:46:18 +01:00
2024-03-13 09:51:59 +01:00
def __getitem__(self, item):
if item is None:
return None
if type(item) is not int and item.isnumeric():
item = int(item)
if type(item) is int and 0 <= item < len(self.field):
return self.field[item]
return None
2024-03-04 08:46:18 +01:00
def action(self, row, col):
2024-03-13 09:51:59 +01:00
self.clear_marker()
2024-04-16 11:47:52 +02:00
if self.field_mode == ShipMode.PUT:
if self.check_ship(row, col):
self.get_ship(row, col)
elif self.ship_size in self.ships and self.check_possible(row, col):
2024-03-04 08:46:18 +01:00
self.set_ship(row, col)
2024-04-16 11:47:52 +02:00
elif self.field_mode == ShipMode.SHOOT:
2024-03-04 08:46:18 +01:00
self.shoot(row, col)
2024-02-26 18:34:51 +01:00
2024-03-04 08:46:18 +01:00
def target(self, row, col):
2024-03-12 09:51:04 +01:00
self.clear_marker()
2024-04-16 11:47:52 +02:00
if self.field_mode == ShipMode.PUT:
2024-03-04 08:46:18 +01:00
if self.check_possible(row, col):
2024-04-16 11:47:52 +02:00
if self.ship_direction == ShipDirection.VERTICAL:
2024-03-04 08:46:18 +01:00
for r in range(row, row + self.ship_size):
2024-03-13 09:51:59 +01:00
if self.ship_size in self.ships:
self.field[r * self.field_size + col] = "p"
else:
self.field[r * self.field_size + col] = "r"
2024-03-04 08:46:18 +01:00
2024-04-16 11:47:52 +02:00
if self.ship_direction == ShipDirection.HORIZONTAL:
2024-03-04 08:46:18 +01:00
for c in range(col, col + self.ship_size):
2024-03-13 09:51:59 +01:00
if self.ship_size in self.ships:
self.field[row * self.field_size + c] = "p"
else:
self.field[row * self.field_size + c] = "r"
else:
self.field[row * self.field_size + col] = "+"
2024-03-04 08:46:18 +01:00
def clear_marker(self):
for i in range(0, len(self.field)):
2024-03-13 09:51:59 +01:00
if self.field[i] == "p" or self.field[i] == "r":
2024-03-04 08:46:18 +01:00
self.field[i] = ""
2024-03-13 09:51:59 +01:00
if "+" in self.field[i]:
self.field[i] = self.field[i].replace("+", "")
2024-03-04 08:46:18 +01:00
2024-02-26 18:34:51 +01:00
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
2024-04-16 11:47:52 +02:00
if self.ship_direction == ShipDirection.VERTICAL:
2024-02-26 18:34:51 +01:00
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"
2024-04-16 11:47:52 +02:00
if self.ship_direction == ShipDirection.HORIZONTAL:
2024-02-26 18:34:51 +01:00
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"
2024-03-13 09:51:59 +01:00
if self.ship_size in self.ships:
self.ships.remove(self.ship_size)
2024-04-16 11:47:52 +02:00
def get_ship(self, row, col):
if row < 0 or row > self.field_size:
return
if col < 0 or col > self.field_size:
return
self.field[row * self.field_size + col] = ''
ship_size = 1
ship_direction = ShipDirection.UNKNOWN
# check vertical
for r in range(row + 1, self.field_size):
if self.check_ship(r, col):
ship_size += 1
ship_direction = ShipDirection.VERTICAL
self.field[r * self.field_size + col] = ''
else:
break
for r in range(row - 1, -1, -1):
if self.check_ship(r, col):
ship_size += 1
ship_direction = ShipDirection.VERTICAL
self.field[r * self.field_size + col] = ''
else:
break
if ship_direction == ShipDirection.UNKNOWN:
# check horizontal
for c in range(col + 1, self.field_size):
if self.check_ship(row, c):
ship_size += 1
ship_direction = ShipDirection.HORIZONTAL
self.field[row * self.field_size + c] = ''
else:
break
for c in range(col - 1, - 1, -1):
if self.check_ship(row, c):
ship_size += 1
ship_direction = ShipDirection.HORIZONTAL
self.field[row * self.field_size + c] = ''
else:
break
self.set_ship_direction(ship_direction)
self.set_ship_size(ship_size)
self.ships.append(ship_size)
2024-02-26 18:34:51 +01:00
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
2024-04-16 11:47:52 +02:00
def check_ship(self, row, col):
return self.field[row * self.field_size + col].strip() == '1'
2024-02-26 18:34:51 +01:00
2024-03-04 08:46:18 +01:00
def check_possible(self, row, col):
2024-04-16 11:47:52 +02:00
if self.ship_direction == ShipDirection.VERTICAL:
2024-02-26 18:34:51 +01:00
if self.field_size - row >= self.ship_size:
for r in range(row, row + self.ship_size):
2024-03-04 08:46:18 +01:00
if not self.check_blocked(r, col):
2024-02-26 18:34:51 +01:00
return False
return True
2024-04-16 11:47:52 +02:00
if self.ship_direction == ShipDirection.HORIZONTAL:
2024-02-26 18:34:51 +01:00
if self.field_size - col >= self.ship_size:
for c in range(col, col + self.ship_size):
2024-03-04 08:46:18 +01:00
if not self.check_blocked(row, c):
2024-02-26 18:34:51 +01:00
return False
return True
return False
def check_blocked(self, row, col):
for r in range(row - 1, row + 2):
for c in range(col - 1, col + 2):
if 0 <= r < self.field_size and 0 <= c < self.field_size:
cell = (self.field[r * self.field_size + c]).strip()
if cell != '' and cell != 'p':
return False
return True
def set_ship_size(self, value):
2024-03-13 09:51:59 +01:00
if value is None:
return
if type(value) is str and value.isnumeric():
value = int(value)
if type(value) is int and 1 <= value <= 4:
self.ship_size = value
2024-02-26 18:34:51 +01:00
def set_ship_direction(self, value):
2024-03-13 09:51:59 +01:00
if value is None:
return
2024-04-16 11:47:52 +02:00
if type(value) is not ShipDirection:
return
if value != ShipDirection.UNKNOWN:
2024-03-13 09:51:59 +01:00
self.ship_direction = value
2024-03-04 08:46:18 +01:00
def toggle_ship_direction(self):
2024-04-16 11:47:52 +02:00
if self.field_mode == ShipMode.PUT:
if self.ship_direction == ShipDirection.VERTICAL:
self.ship_direction = ShipDirection.HORIZONTAL
2024-03-04 08:46:18 +01:00
else:
2024-04-16 11:47:52 +02:00
self.ship_direction = ShipDirection.VERTICAL
2024-02-26 18:34:51 +01:00
def toggle_field_mode(self):
2024-04-16 11:47:52 +02:00
if self.field_mode == ShipMode.PUT:
self.field_mode = ShipMode.SHOOT
2024-02-26 18:34:51 +01:00
else:
2024-04-16 11:47:52 +02:00
self.field_mode = ShipMode.PUT
2024-02-26 18:34:51 +01:00
def print_field(self):
for r in range(0, self.field_size):
blocked_string = ""
ship_string = ""
for c in range(0, self.field_size):
2024-03-13 09:51:59 +01:00
blocked_string += str(self.check_blocked(r, c))[0] + ", "
2024-02-26 18:34:51 +01:00
ship_string += self.field[r * self.field_size + c] + ', '
print(blocked_string[:-2] + ' ' + ship_string[:-2])
2024-04-29 19:05:10 +02:00
print("********************************************************************")
@staticmethod
def convert_to_json(obj):
if isinstance(obj, ShipField):
result = copy.copy(obj.__dict__)
result['field_mode'] = obj.field_mode.value
result['ship_direction'] = obj.ship_direction.value
return result