diff --git a/HNS/Excercises/ShipCraft/ShipField.py b/HNS/Excercises/ShipCraft/ShipField.py index 59210fd..adfbfa5 100644 --- a/HNS/Excercises/ShipCraft/ShipField.py +++ b/HNS/Excercises/ShipCraft/ShipField.py @@ -19,6 +19,23 @@ class ShipField: self.ship_size = 4 self.ship_direction = 0 + def action(self, row, col): + 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): + if self.field_mode == 0: + self.clear_marker() + + def clear_marker(self): + for i in range(0, len(self.field)): + if self.field[i] == "p": + self.field[i] = "" + def set_ship(self, row, col): if row < 0 or row > self.field_size: return @@ -53,14 +70,14 @@ class ShipField: else: return ShootResult.UNDEFINED - def check_possible(self, row, col, ship_size, ship_direction): - # Функция должна возвращать True, если можно поставить сюда корабль, - # в противном случае - False + def check_possible(self, row, col, ship_direction): + # Функция должна возвращать True, если можно поставить сюда корабль, + # в противном случае - False if self.ship_direction == 0: - # Здесь мы знаем, что корабль помещается на поле. + # Здесь мы знаем, что корабль помещается на поле. if self.field_size - row >= self.ship_size: - # Теперь нужно проверить, не заблокировано ли какое-то из полей, + # Теперь нужно проверить, не заблокировано ли какое-то из полей, for r in range(row, row + self.ship_size): if not self.check_blocked(self, r, col): return False @@ -76,8 +93,8 @@ class ShipField: return False def check_blocked(self, row, col): - # Функция возвращает True, если все клетки вокруг клетки с координатами 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: @@ -87,10 +104,10 @@ class ShipField: return True def set_ship_size(self, value): - if value.isnumeric(): - number = int(value) - if 1 <= number <= 4: - self.ship_size = number + if value.isnumeric(): + number = int(value) + if 1 <= number <= 4: + self.ship_size = number def set_ship_direction(self, value): if value.isnumeric() == 0 or value.isnumeric() == 1: diff --git a/HNS/Excercises/ShipCraft/__pycache__/ShipField.cpython-311.pyc b/HNS/Excercises/ShipCraft/__pycache__/ShipField.cpython-311.pyc index f5cab3a..f0c1669 100644 Binary files a/HNS/Excercises/ShipCraft/__pycache__/ShipField.cpython-311.pyc and b/HNS/Excercises/ShipCraft/__pycache__/ShipField.cpython-311.pyc differ diff --git a/HNS/Excercises/ShipCraft/__pycache__/main.cpython-311.pyc b/HNS/Excercises/ShipCraft/__pycache__/main.cpython-311.pyc index 7330efb..00f378d 100644 Binary files a/HNS/Excercises/ShipCraft/__pycache__/main.cpython-311.pyc and b/HNS/Excercises/ShipCraft/__pycache__/main.cpython-311.pyc differ diff --git a/HNS/Excercises/ShipCraft/main.py b/HNS/Excercises/ShipCraft/main.py index 2799849..2e73c5a 100644 --- a/HNS/Excercises/ShipCraft/main.py +++ b/HNS/Excercises/ShipCraft/main.py @@ -217,29 +217,22 @@ def button_enter(field, buttons, row, col): other_field = my_field other_buttons = my_buttons - for i in range(0, len(other_field)): - if other_field[i] == "p": - other_field[i] = '' - colorize(other_field, other_buttons) - if field_mode == 0: - for i in range(0, len(field)): - if field[i] == "p": - field[i] = '' - if check_possible(field, row, col,ship_size, ship_direction): - if ship_direction == 0: - for r in range(row, row + ship_size): - field[r * field_size + col] = "p" + if check_possible(field, row, col,ship_size, ship_direction): + if ship_direction == 0: + for r in range(row, row + ship_size): + field[r * field_size + col] = "p" - if ship_direction == 1: - for c in range(col, col + ship_size): - field[row * field_size + c] = "p" + if ship_direction == 1: + for c in range(col, col + ship_size): + field[row * field_size + c] = "p" colorize(field, buttons) + def check_possible(field, row, col, ship_size, ship_direction): # Функция должна возвращать True, если можно поставить сюда корабль, # в противном случае - False