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