hnc-artur/HNS/MB/ShipField.py

362 lines
12 KiB
Python
Raw Permalink Normal View History

2024-04-29 19:12:20 +02:00
import copy
2024-02-19 12:55:57 +01:00
from ShootResult import ShootResult
2024-04-29 19:12:20 +02:00
from ShipMode import ShipMode
2024-05-07 10:24:05 +02:00
from ShipDirection import ShipDirection
2024-02-15 18:27:34 +01:00
class ShipField:
2024-05-27 20:12:58 +02:00
field_size = 10
2024-02-15 18:27:34 +01:00
def __init__(self):
2024-02-19 12:55:57 +01:00
self.field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
2024-04-29 19:12:20 +02:00
self.ships = [4, 3, 3, 2, 2, 2, 1, 1, 1, 1]
2024-06-04 08:43:38 +02:00
self.field_mode = ShipMode.INACTIVE
2024-02-19 12:55:57 +01:00
self.ship_size = 4
2024-04-29 19:12:20 +02:00
self.ship_direction = ShipDirection.VERTICAL
def from_json(self, obj):
self.field = obj['field']
self.ships = obj['ships']
self.field_mode = ShipMode.from_string(obj['field_mode'])
self.ship_size = obj['ship_size']
self.ship_direction = ShipDirection.from_string(obj['ship_direction'])
2024-02-19 12:55:57 +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-07-16 11:40:41 +02:00
def mark_killed(self, row, col):
if row < 0 or row > ShipField.field_size:
return
if col < 0 or col > ShipField.field_size:
return
self.mark_one_killed(row, col)
# check vertical
for r in range(row + 1, ShipField.field_size):
if self.check_damaged(r, col):
self.mark_one_killed(r, col)
else:
break
for r in range(row - 1, -1, -1):
if self.check_damaged(r, col):
self.mark_one_killed(r, col)
else:
break
# check horizontal
for c in range(col + 1, ShipField.field_size):
if self.check_damaged(row, c):
self.mark_one_killed(row, c)
else:
break
for c in range(col - 1, - 1, -1):
if self.check_damaged(row, c):
self.mark_one_killed(row, c)
else:
break
def check_killed(self, row, col):
if row < 0 or row > ShipField.field_size:
return
if col < 0 or col > ShipField.field_size:
return
# check vertical
for r in range(row + 1, ShipField.field_size):
if self.check_ship(r, col):
return False
elif self.check_empty(r, col):
break
for r in range(row - 1, -1, -1):
if self.check_ship(r, col):
return False
elif self.check_empty(r, col):
break
# check horizontal
for c in range(col + 1, ShipField.field_size):
if self.check_ship(row, c):
return False
elif self.check_empty(row, c):
break
for c in range(col - 1, - 1, -1):
if self.check_ship(row, c):
return False
elif self.check_empty(row, c):
break
return True
def mark_one_killed(self, row, col):
self.field[row * ShipField.field_size + col] = 'X'
for r in range(row - 1, row + 2):
if r < 0 or r > ShipField.field_size:
break
for c in range(col - 1, col + 2):
if c < 0 or c > ShipField.field_size:
break
if self.check_empty(r, c):
self.field[r * ShipField.field_size + c] = '0'
2024-02-19 12:55:57 +01:00
def action(self, row, col):
self.clear_marker()
2024-04-29 19:12:20 +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-02-19 12:55:57 +01:00
self.set_ship(row, col)
2024-04-29 19:12:20 +02:00
elif self.field_mode == ShipMode.SHOOT:
2024-06-10 22:20:20 +02:00
shoot_result = self.shoot(row, col)
2024-07-16 11:40:41 +02:00
if shoot_result == ShootResult.KILLED:
self.mark_killed(row, col)
2024-06-10 22:20:20 +02:00
return shoot_result == ShootResult.EMPTY
return False
2024-02-19 12:55:57 +01:00
def target(self, row, col):
self.clear_marker()
2024-04-29 19:12:20 +02:00
if self.field_mode == ShipMode.PUT:
2024-02-19 12:55:57 +01:00
if self.check_possible(row, col):
2024-04-29 19:12:20 +02:00
if self.ship_direction == ShipDirection.VERTICAL:
2024-02-19 12:55:57 +01:00
for r in range(row, row + self.ship_size):
2024-04-29 19:12:20 +02:00
if self.ship_size in self.ships:
2024-05-27 20:12:58 +02:00
self.field[r * ShipField.field_size + col] = "p"
2024-04-29 19:12:20 +02:00
else:
2024-05-27 20:12:58 +02:00
self.field[r * ShipField.field_size + col] = "r"
2024-02-19 12:55:57 +01:00
2024-04-29 19:12:20 +02:00
if self.ship_direction == ShipDirection.HORIZONTAL:
2024-02-19 12:55:57 +01:00
for c in range(col, col + self.ship_size):
2024-04-29 19:12:20 +02:00
if self.ship_size in self.ships:
2024-05-27 20:12:58 +02:00
self.field[row * ShipField.field_size + c] = "p"
2024-04-29 19:12:20 +02:00
else:
2024-05-27 20:12:58 +02:00
self.field[row * ShipField.field_size + c] = "r"
2024-06-04 08:43:38 +02:00
elif self.field_mode == ShipMode.SHOOT:
2024-05-27 20:12:58 +02:00
self.field[row * ShipField.field_size + col] += "+"
2024-02-19 12:55:57 +01:00
def clear_marker(self):
for i in range(0, len(self.field)):
2024-04-29 19:12:20 +02:00
if self.field[i] == "p" or self.field[i] == "r":
2024-02-19 12:55:57 +01:00
self.field[i] = ""
if "+" in self.field[i]:
self.field[i] = self.field[i].replace("+", "")
2024-02-15 18:27:34 +01:00
def set_ship(self, row, col):
2024-05-27 20:12:58 +02:00
if row < 0 or row > ShipField.field_size:
2024-02-15 18:27:34 +01:00
return
2024-05-27 20:12:58 +02:00
if col < 0 or col > ShipField.field_size:
2024-02-15 18:27:34 +01:00
return
2024-05-27 20:12:58 +02:00
index = row * ShipField.field_size + col
2024-04-29 19:12:20 +02:00
if self.ship_direction == ShipDirection.VERTICAL:
2024-05-27 20:12:58 +02:00
if ShipField.field_size - row < self.ship_size:
2024-02-15 18:27:34 +01:00
return
for r in range(row, row + self.ship_size):
2024-05-27 20:12:58 +02:00
index = r * ShipField.field_size + col
2024-02-15 18:27:34 +01:00
self.field[index] = "1"
2024-04-29 19:12:20 +02:00
if self.ship_direction == ShipDirection.HORIZONTAL:
2024-05-27 20:12:58 +02:00
if ShipField.field_size - col < self.ship_size:
2024-02-15 18:27:34 +01:00
return
for c in range(col, col + self.ship_size):
2024-05-27 20:12:58 +02:00
index = row * ShipField.field_size + c
2024-02-15 18:27:34 +01:00
self.field[index] = "1"
2024-04-29 19:12:20 +02:00
if self.ship_size in self.ships:
self.ships.remove(self.ship_size)
2024-05-28 09:12:00 +02:00
if self.ship_size not in self.ships and len(self.ships) > 0:
self.ship_size = max(self.ships) # min(self.ships) self.ships[0]
2024-04-29 19:12:20 +02:00
def get_ship(self, row, col):
2024-05-27 20:12:58 +02:00
if row < 0 or row > ShipField.field_size:
2024-04-29 19:12:20 +02:00
return
2024-05-27 20:12:58 +02:00
if col < 0 or col > ShipField.field_size:
2024-04-29 19:12:20 +02:00
return
2024-05-27 20:12:58 +02:00
self.field[row * ShipField.field_size + col] = ''
2024-04-29 19:12:20 +02:00
ship_size = 1
ship_direction = ShipDirection.UNKNOWN
2024-05-07 10:24:05 +02:00
# check vertical
2024-05-27 20:12:58 +02:00
for r in range(row + 1, ShipField.field_size):
2024-04-29 19:12:20 +02:00
if self.check_ship(r, col):
ship_size += 1
ship_direction = ShipDirection.VERTICAL
2024-05-27 20:12:58 +02:00
self.field[r * ShipField.field_size + col] = ''
2024-04-29 19:12:20 +02:00
else:
break
for r in range(row - 1, -1, -1):
if self.check_ship(r, col):
ship_size += 1
ship_direction = ShipDirection.VERTICAL
2024-05-27 20:12:58 +02:00
self.field[r * ShipField.field_size + col] = ''
2024-04-29 19:12:20 +02:00
else:
break
if ship_direction == ShipDirection.UNKNOWN:
2024-05-07 10:24:05 +02:00
# check horizontal
2024-05-27 20:12:58 +02:00
for c in range(col + 1, ShipField.field_size):
2024-04-29 19:12:20 +02:00
if self.check_ship(row, c):
ship_size += 1
ship_direction = ShipDirection.HORIZONTAL
2024-05-27 20:12:58 +02:00
self.field[row * ShipField.field_size + c] = ''
2024-04-29 19:12:20 +02:00
else:
break
2024-05-07 10:24:05 +02:00
for c in range(col - 1, - 1, -1):
2024-04-29 19:12:20 +02:00
if self.check_ship(row, c):
ship_size += 1
ship_direction = ShipDirection.HORIZONTAL
2024-05-27 20:12:58 +02:00
self.field[row * ShipField.field_size + c] = ''
2024-04-29 19:12:20 +02:00
else:
break
self.set_ship_direction(ship_direction)
2024-05-27 20:12:58 +02:00
self.set_ship_size(ship_size)
2024-04-29 19:12:20 +02:00
2024-05-27 20:12:58 +02:00
self.ships.append(ship_size)
2024-04-29 19:12:20 +02:00
2024-02-15 18:27:34 +01:00
def shoot(self, row, col):
2024-05-27 20:12:58 +02:00
if row < 0 or row > ShipField.field_size - 1:
2024-02-15 18:27:34 +01:00
return ShootResult.UNDEFINED
2024-05-27 20:12:58 +02:00
if col < 0 or col > ShipField.field_size - 1:
2024-02-15 18:27:34 +01:00
return ShootResult.UNDEFINED
2024-05-27 20:12:58 +02:00
index = row * ShipField.field_size + col
2024-02-15 18:27:34 +01:00
if (self.field[index]).strip() == "":
self.field[index] = "0"
return ShootResult.EMPTY
elif (self.field[index]).strip() == "1":
self.field[index] = "\\"
2024-07-16 11:40:41 +02:00
if self.check_killed(row, col):
return ShootResult.KILLED
2024-02-15 18:27:34 +01:00
return ShootResult.DAMAGED
else:
return ShootResult.UNDEFINED
2024-04-29 19:12:20 +02:00
def check_ship(self, row, col):
2024-05-27 20:12:58 +02:00
return self.field[row * ShipField.field_size + col].strip() == '1'
2024-04-29 19:12:20 +02:00
2024-07-16 11:40:41 +02:00
def check_damaged(self, row, col):
return self.field[row * ShipField.field_size + col].strip() == '\\'
def check_empty(self, row, col):
cell = self.field[row * ShipField.field_size + col].strip()
return cell == '0' or cell == ''
2024-06-10 22:20:20 +02:00
def check_end(self):
return '1' not in self.field
2024-02-15 18:27:34 +01:00
def check_possible(self, row, col):
2024-04-29 19:12:20 +02:00
if self.ship_direction == ShipDirection.VERTICAL:
2024-05-27 20:12:58 +02:00
if ShipField.field_size - row >= self.ship_size:
2024-02-15 18:27:34 +01:00
for r in range(row, row + self.ship_size):
2024-02-19 12:55:57 +01:00
if not self.check_blocked(r, col):
2024-02-15 18:27:34 +01:00
return False
return True
2024-04-29 19:12:20 +02:00
if self.ship_direction == ShipDirection.HORIZONTAL:
2024-05-27 20:12:58 +02:00
if ShipField.field_size - col >= self.ship_size:
2024-02-15 18:27:34 +01:00
for c in range(col, col + self.ship_size):
2024-02-19 12:55:57 +01:00
if not self.check_blocked(row, c):
2024-02-15 18:27:34 +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):
2024-05-27 20:12:58 +02:00
if 0 <= r < ShipField.field_size and 0 <= c < ShipField.field_size:
cell = (self.field[r * ShipField.field_size + c]).strip()
2024-02-15 18:27:34 +01:00
if cell != '' and cell != 'p':
return False
return True
def set_ship_size(self, value):
2024-02-19 12:55:57 +01:00
if value is None:
return
2024-02-26 18:44:13 +01:00
if type(value) is str and value.isnumeric():
2024-02-19 12:55:57 +01:00
value = int(value)
if type(value) is int and 1 <= value <= 4:
2024-02-15 18:27:34 +01:00
self.ship_size = value
2024-02-26 18:44:13 +01:00
def set_ship_direction(self, value):
if value is None:
return
2024-04-29 19:12:20 +02:00
if type(value) is not ShipDirection:
return
2024-02-26 18:44:13 +01:00
2024-04-29 19:12:20 +02:00
if value != ShipDirection.UNKNOWN:
2024-02-26 18:44:13 +01:00
self.ship_direction = value
2024-02-19 12:55:57 +01:00
def toggle_ship_direction(self):
2024-05-07 10:24:05 +02:00
if self.field_mode == ShipMode.PUT:
if self.ship_direction == ShipDirection.VERTICAL:
self.ship_direction = ShipDirection.HORIZONTAL
else:
self.ship_direction = ShipDirection.VERTICAL
2024-02-15 18:27:34 +01:00
2024-06-04 08:43:38 +02:00
def set_field_mode(self, value):
if value is None:
return
if type(value) is not ShipMode:
return
self.field_mode = value
2024-02-19 12:55:57 +01:00
def print_field(self):
2024-05-27 20:12:58 +02:00
for r in range(0, ShipField.field_size):
2024-02-19 12:55:57 +01:00
blocked_string = ""
ship_string = ""
2024-05-27 20:12:58 +02:00
for c in range(0, ShipField.field_size):
2024-02-19 12:55:57 +01:00
blocked_string += str(self.check_blocked(r, c))[0] + ", "
2024-05-27 20:12:58 +02:00
ship_string += self.field[r * ShipField.field_size + c] + ', '
2024-05-07 10:24:05 +02:00
print(blocked_string[:-2] + ' ' + ship_string[:-2])
2024-02-26 18:44:13 +01:00
print("********************************************************************")
2024-04-29 19:12:20 +02:00
@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
2024-05-07 10:24:05 +02:00
return result