загружаю код с полным дз 2

This commit is contained in:
ehermakov 2024-03-23 17:53:11 +03:00
parent c4cc9b0245
commit 4d85cdfac2
6 changed files with 28 additions and 24 deletions

View File

@ -1,5 +1,5 @@
from ShootResult import ShootResult
from ShipModeDirection import ShipMode, ShipDirection
from ShipModeDirection import ShipDirection, ShipMode
class ShipField:
@ -18,9 +18,9 @@ class ShipField:
self.ships = [4, 3, 3, 2, 2, 2, 1, 1, 1, 1]
self.field_size = 10
self.field_mode = 0
self.field_mode = ShipMode.PUT.value
self.ship_size = 4
self.ship_direction = 0
self.ship_direction = ShipDirection.VERTICAL.value
def __getitem__(self, item):
if item is None:
@ -37,26 +37,26 @@ class ShipField:
def action(self, row, col):
self.clear_marker()
if self.field_mode == 0:
if self.field_mode == ShipMode.PUT.value:
if self.ship_size in self.ships and self.check_possible(row, col):
self.set_ship(row, col)
elif self.field_mode == 1:
elif self.field_mode == ShipMode.SHOOT.value:
self.shoot(row, col)
def target(self, row, col):
self.clear_marker()
if self.field_mode == 0:
if self.field_mode == ShipMode.PUT.value:
if self.check_possible(row, col):
if self.ship_direction == 0:
if self.ship_direction == ShipDirection.VERTICAL.value:
for r in range(row, row + self.ship_size):
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:
if self.ship_direction == ShipDirection.HORIZONTAL.value:
for c in range(col, col + self.ship_size):
if self.ship_size in self.ships:
self.field[row * self.field_size + c] = "p"
@ -79,13 +79,13 @@ class ShipField:
if col < 0 or col > self.field_size:
return
index = row * self.field_size + col
if self.ship_direction == 0:
if self.ship_direction == ShipDirection.VERTICAL.value:
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.ship_direction == ShipDirection.HORIZONTAL.value:
if self.field_size - col < self.ship_size:
return
for c in range(col, col + self.ship_size):
@ -114,7 +114,7 @@ class ShipField:
def check_possible(self, row, col):
# Функция должна возвращать True, если можно поставить сюда корабль,
# в противном случае - False
if self.ship_direction == 0:
if self.ship_direction == ShipDirection.VERTICAL.value:
# Здесь мы знаем, что корабль помещается на поле.
if self.field_size - row >= self.ship_size:
# Теперь нужно проверить, не заблокировано ли какое-то из полей,
@ -123,7 +123,7 @@ class ShipField:
return False
return True
if self.ship_direction == 1:
if self.ship_direction == ShipDirection.HORIZONTAL.value:
if self.field_size - col >= self.ship_size:
for c in range(col, col + self.ship_size):
if not self.check_blocked(row, c):
@ -160,20 +160,20 @@ class ShipField:
if type(value) is str and value.isnumeric():
value = int(value)
if type(value) is int and 0 <= value <= 1:
if type(value) is int and ShipDirection.VERTICAL.value <= value <= ShipDirection.HORIZONTAL.value:
self.ship_direction = value
def toggle_ship_direction(self):
if self.ship_direction == 0:
self.ship_direction = 1
if self.ship_direction == ShipDirection.VERTICAL.value:
self.ship_direction = ShipDirection.HORIZONTAL.value
else:
self.ship_direction = 0
self.ship_direction = ShipDirection.VERTICAL.value
def toggle_field_mode(self):
if self.field_mode == 0:
self.field_mode = 1
if self.field_mode == ShipMode.PUT.value:
self.field_mode = ShipMode.SHOOT.value
else:
self.field_mode = 0
self.field_mode = ShipMode.PUT.value
def print_field(self):
print(self.ships)

View File

@ -2,10 +2,13 @@ from enum import Enum
class ShipMode(Enum):
PUT = "PUT"
SHOOT = "SHOOT"
PUT = 0
SHOOT = 1
class ShipDirection(Enum):
VERTICAL = "VERTICAL"
HORIZONTAL = "HORIZONTAL"
VERTICAL = 0
HORIZONTAL = 1
print(ShipDirection.VERTICAL.value)

View File

@ -1,6 +1,7 @@
from unittest import TestCase
from ShipField import ShipField
from ShootResult import ShootResult
from ShipModeDirection import ShipDirection, ShipMode
class TestShipField(TestCase):
@ -118,7 +119,7 @@ class TestShipField(TestCase):
# arrangement установка
ship_field = ShipField()
ship_field.set_ship_size(4)
ship_field.set_ship_direction(1)
ship_field.set_ship_direction(ShipDirection.HORIZONTAL.value)
# action действие
ship_field.set_ship(5, 3)
# assertion проверка занятых