hnc-artur/HNS/MB/ShipField.py

165 lines
6.2 KiB
Python
Raw Normal View History

2024-02-19 12:55:57 +01:00
from ShootResult import ShootResult
2024-02-15 18:27:34 +01:00
class ShipField:
def __init__(self):
2024-02-19 12:55:57 +01:00
self.field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
self.field_size = 10
self.field_mode = 0
self.ship_size = 4
self.ship_direction = 0
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
def action(self, row, col):
self.clear_marker()
if self.field_mode == 0:
if self.check_possible(row, col):
self.set_ship(row, col)
elif self.field_mode == 1:
self.shoot(row, col)
def target(self, row, col):
self.clear_marker()
if self.field_mode == 0:
if self.check_possible(row, col):
if self.ship_direction == 0:
for r in range(row, row + self.ship_size):
self.field[r * self.field_size + col] = "p"
if self.ship_direction == 1:
for c in range(col, col + self.ship_size):
self.field[row * self.field_size + c] = "p"
else:
self.field[row * self.field_size + col] += "+"
def clear_marker(self):
for i in range(0, len(self.field)):
if self.field[i] == "p":
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):
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
def check_possible(self, row, col):
# Функция должна возвращать True, если можно поставить сюда корабль,
# в противном случае - False
if self.ship_direction == 0:
# Здесь мы знаем, что корабль помещается на поле.
if self.field_size - row >= self.ship_size:
# Теперь нужно проверить, не заблокировано ли какое-то из полей,
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
if self.ship_direction == 1:
if self.field_size - col >= self.ship_size:
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):
# Функция возвращает True, если все клетки вокруг клетки с координатами 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-02-19 12:55:57 +01:00
if value is None:
return
if type(value) is not int and value.isnumeric:
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-19 12:55:57 +01:00
def toggle_ship_direction(self):
if self.ship_direction == 0:
self.ship_direction = 1
else:
self.ship_direction = 0
2024-02-15 18:27:34 +01:00
def toggle_field_mode(self):
if self.field_mode == 0:
self.field_mode = 1
else:
self.field_mode = 0
2024-02-19 12:55:57 +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):
blocked_string += str(self.check_blocked(r, c))[0] + ", "
ship_string += self.field[r * self.field_size + c] + ', '
print(ship_string[:-2])
#print(blocked_string[:-2] + ' ' + ship_string[:-2])
print("********************************************************************")