diff --git a/HNC/Exercises/Ship_Battle/ShipField.py b/HNC/Exercises/Ship_Battle/ShipField.py index a917394..a87c565 100644 --- a/HNC/Exercises/Ship_Battle/ShipField.py +++ b/HNC/Exercises/Ship_Battle/ShipField.py @@ -15,15 +15,31 @@ class ShipField: ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] + self.ships = [4, 3, 3, 2, 2, 2, 1, 1, 1, 1] 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): + if self.ship_size in self.ships and self.check_possible(row, col): self.set_ship(row, col) elif self.field_mode == 1: @@ -37,16 +53,29 @@ class ShipField: 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_size in self.ships: + self.field[r * self.field_size + col] = "p" + else: + self.field[r * self.field_size + col] = "r" if self.ship_direction == 1: for c in range(col, col + self.ship_size): - self.field[row * self.field_size + c] = "p" + 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] = "+" def clear_marker(self): for i in range(0, len(self.field)): - if self.field[i] == "p": + if self.field[i] == "p" or self.field[i] == "r": self.field[i] = "" + + if "+" in self.field[i]: + self.field[i] = self.field[i].replace("+", "") def set_ship(self, row, col): if row < 0 or row > self.field_size: @@ -67,6 +96,9 @@ class ShipField: index = row * self.field_size + c self.field[index] = "1" + if self.ship_size in self.ships: + self.ships.remove(self.ship_size) + def shoot(self, row, col): if row < 0 or row > self.field_size - 1: return ShootResult.UNDEFINED @@ -109,12 +141,24 @@ class ShipField: return True def set_ship_size(self, value): - if 1 <= value <= 4: - self.ship_size = value + 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 def set_ship_direction(self, value): - if 0 <= value <= 1: - self.ship_direction = value + if value is None: + return + + if type(value) is str and value.isnumeric(): + value = int(value) + + if type(value) is int and 0 <= value <= 1: + self.ship_direction = value def toggle_ship_direction(self): if self.field_mode == 0: @@ -134,10 +178,7 @@ class ShipField: blocked_string = "" ship_string = "" for c in range(0, self.field_size): - blocked_string += str(self.check_blocked(self, r, c))[0] + ", " + blocked_string += str(self.check_blocked(r, c))[0] + ", " ship_string += self.field[r * self.field_size + c] + ', ' print(blocked_string[:-2] + ' ' + ship_string[:-2]) - print("********************************************************************") - - - \ No newline at end of file + print("********************************************************************") \ No newline at end of file diff --git a/HNC/Exercises/Ship_Battle/ShipField_test.py b/HNC/Exercises/Ship_Battle/ShipField_test.py index 95b0cf4..bc5ceb7 100644 --- a/HNC/Exercises/Ship_Battle/ShipField_test.py +++ b/HNC/Exercises/Ship_Battle/ShipField_test.py @@ -54,7 +54,11 @@ class TestShipField(TestCase): self.assertEqual(ship_field.field_mode, 0) # Проверяем, что field_mode принял желаемое значение def test_action(self): - self.fail() + ship_field = ShipField() + self.assertEqual(ship_field.action, 0) + + ship_field.action() + self.assertEqual(ship_field.action, 1) def test_target(self): self.fail() @@ -114,10 +118,10 @@ class TestShipField(TestCase): new_field_string = str.join(' ', ship_field.field) self.assertEqual(new_field_string, old_field_string) - def test_set_ship(self): + def test_set_ship_size4_vertical_direction(self): ship_field = ShipField() ship_field.set_ship_size(4) - ship_field.set_ship_direction(0) + ship_field.set_ship_direction(0) #vertikal ship_field.set_ship(6, 3) @@ -126,6 +130,29 @@ class TestShipField(TestCase): self.assertEqual(ship_field.field[83].strip(), '1') self.assertEqual(ship_field.field[93].strip(), '1') + def test_set_ship_size4_horizontal_direction(self): + ship_field = ShipField() + ship_field.set_ship_size(4) + ship_field.set_ship_direction(1) + + ship_field.set_ship(6, 3) + + self.assertEqual(ship_field.field[63].strip(), '1') + self.assertEqual(ship_field.field[64].strip(), '1') + self.assertEqual(ship_field.field[65].strip(), '1') + self.assertEqual(ship_field.field[66].strip(), '1') + + def test_set_ship_size4_vertical_direction_outofrange(self): + ship_field = ShipField() + ship_field.set_ship_size(4) + ship_field.set_ship_direction(0) + old_field_string = str.join(' ', ship_field.field) + + ship_field.set_ship(7, 3) + + new_field_string = str.join(' ', ship_field.field) + self.assertEqual(new_field_string, old_field_string) + def test_check_possible(self): self.fail() @@ -133,8 +160,22 @@ class TestShipField(TestCase): self.fail() def test_set_ship_direction(self): - self.fail() + ship_field = ShipField() + ship_field.set_ship_direction(1) + self.assertEqual(ship_field.ship_direction, 1) + + ship_field.set_ship_direction(0) + self.assertEqual(ship_field.ship_direction, 0) + def test_toggle_ship_direction(self): ship_field = ShipField() + ship_field.toggle_ship_direction() + + self.assertEqual(ship_field.ship_direction, 1) + + ship_field.toggle_ship_direction() + + self.assertEqual(ship_field.ship_direction, 0) + diff --git a/HNC/Exercises/Ship_Battle/__pycache__/ShipField.cpython-311.pyc b/HNC/Exercises/Ship_Battle/__pycache__/ShipField.cpython-311.pyc index aaba9de..b442d41 100644 Binary files a/HNC/Exercises/Ship_Battle/__pycache__/ShipField.cpython-311.pyc and b/HNC/Exercises/Ship_Battle/__pycache__/ShipField.cpython-311.pyc differ diff --git a/HNC/Exercises/Ship_Battle/__pycache__/ShipField_test.cpython-311-pytest-8.1.1.pyc b/HNC/Exercises/Ship_Battle/__pycache__/ShipField_test.cpython-311-pytest-8.1.1.pyc index 5c6578b..f41d633 100644 Binary files a/HNC/Exercises/Ship_Battle/__pycache__/ShipField_test.cpython-311-pytest-8.1.1.pyc and b/HNC/Exercises/Ship_Battle/__pycache__/ShipField_test.cpython-311-pytest-8.1.1.pyc differ diff --git a/HNC/Exercises/Ship_Battle/main.py b/HNC/Exercises/Ship_Battle/main.py index 63a69b4..ad1e62c 100644 --- a/HNC/Exercises/Ship_Battle/main.py +++ b/HNC/Exercises/Ship_Battle/main.py @@ -11,7 +11,6 @@ def draw_field(window, field, col_offset): buttons = [] for r in range(0, field.field_size): for c in range(0, field.field_size): - index = r * field.field_size + c btn = Button(window, text='', width=5, height=2) btn.grid(column=c + col_offset, row=r) btn.bind('', lambda e, x=r, y=c: left_button_click(buttons, x, y)) @@ -28,11 +27,15 @@ def colorize(field, buttons): if field.field[i] == "1": bg = 'pink' if field.field[i] == "\\": - bg = 'red' + bg = 'grey' if field.field[i] == "0": bg = 'black' if field.field[i] == "p": bg = 'blue' + if field.field[i] == "r": + bg = 'red' + if "+" in field.field[i]: + bg = 'orange' buttons[i].configure(bg=bg) diff --git a/HNC/Exercises/Ship_Battle/test.py b/HNC/Exercises/Ship_Battle/test.py deleted file mode 100644 index 71554bf..0000000 --- a/HNC/Exercises/Ship_Battle/test.py +++ /dev/null @@ -1,10 +0,0 @@ -from ShipField import ShipField - -my_field = ShipField() - -my_field.set_ship_size(1) - -if my_field.ship_size == 1: - print("OK") -else: - print("ERROR") \ No newline at end of file