diff --git a/HNS/Excercises/ShipCraft/ShipField.py b/HNS/Excercises/ShipCraft/ShipField.py index 56472b3..59210fd 100644 --- a/HNS/Excercises/ShipCraft/ShipField.py +++ b/HNS/Excercises/ShipCraft/ShipField.py @@ -2,24 +2,22 @@ from main import ShootResult class ShipField: - field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] - - field_size = 10 - field_mode = 0 - ship_size = 4 - ship_direction = 0 - def __init__(self): - pass + self.field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] + + self.field_size = 10 + self.field_mode = 0 + self.ship_size = 4 + self.ship_direction = 0 def set_ship(self, row, col): if row < 0 or row > self.field_size: @@ -56,13 +54,13 @@ class ShipField: return ShootResult.UNDEFINED def check_possible(self, row, col, ship_size, ship_direction): - # Функция должна возвращать True, если можно поставить сюда корабль, - # в противном случае - False + # Функция должна возвращать 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 @@ -78,8 +76,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: @@ -89,10 +87,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: @@ -113,6 +111,3 @@ class ShipField: ship_string += self.field[r * self.field_size + c] + ', ' print(blocked_string[:-2] + ' ' + ship_string[:-2]) print("********************************************************************") - - - \ No newline at end of file diff --git a/HNS/Excercises/ShipCraft/Test ShipCraft.py b/HNS/Excercises/ShipCraft/Test ShipCraft.py index 25b1d31..9e0e1b3 100644 --- a/HNS/Excercises/ShipCraft/Test ShipCraft.py +++ b/HNS/Excercises/ShipCraft/Test ShipCraft.py @@ -1,8 +1,48 @@ -from main import ShootResult from ShipField import ShipField -ship_field = ShipField() -ship_field.set_ship_size(3) -ship_field.set_ship_direction(0) -ship_field.set_ship(1, 1) -ship_field.print_field() \ No newline at end of file + +def verify_value(actual, expected): + if actual == expected: + print("OK") + else: + print("ERROR") + + +my_field = ShipField() + +print("set_ship_size()") +my_field.set_ship_size(1) +verify_value(my_field.set_ship_size, 1) + +my_field.set_ship_size(0) +my_field.set_ship_size(6) +verify_value(my_field.set_ship_size, 1) + +my_field.set_ship_size([]) +my_field.set_ship_size("") +my_field.set_ship_size(None) +my_field.set_ship_size(True) +my_field.set_ship_size(False) +verify_value(my_field.set_ship_size, 1) + +my_field.set_ship_size("2") +verify_value(my_field.set_ship_size, 2) +print() + +print('toggle_field_mode()') +verify_value(my_field.field_mode, 0) + +my_field.toggle_field_mode() +verify_value(my_field.field_mode, 1) + +my_field.toggle_field_mode() +verify_value(my_field.field_mode, 0) +print() + +print("ship_set()") +verify_value(my_field.field[0], " ") + +my_field.set_ship_size(1) +my_field.ship_direction(0) +my_field.set_ship(0, 0) + diff --git a/HNS/Excercises/ShipCraft/__pycache__/ShipField.cpython-311.pyc b/HNS/Excercises/ShipCraft/__pycache__/ShipField.cpython-311.pyc new file mode 100644 index 0000000..f5cab3a Binary files /dev/null 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 0e342ec..7330efb 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 80d4047..2799849 100644 --- a/HNS/Excercises/ShipCraft/main.py +++ b/HNS/Excercises/ShipCraft/main.py @@ -290,7 +290,7 @@ print(len(enemy_buttons)) lbl = Label(window, text='', width=5, height=2) lbl.grid(column=10, row=0) -window.mainloop() +#window.mainloop() for r in range(0, field_size):