загружаю код по итогам 17 занятия
This commit is contained in:
parent
2ff4eceea1
commit
1ba926be25
|
@ -15,6 +15,7 @@ class ShipField:
|
||||||
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||||||
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
|
||||||
|
|
||||||
|
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 = 0
|
||||||
self.ship_size = 4
|
self.ship_size = 4
|
||||||
|
@ -36,7 +37,7 @@ class ShipField:
|
||||||
self.clear_marker()
|
self.clear_marker()
|
||||||
|
|
||||||
if self.field_mode == 0:
|
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)
|
self.set_ship(row, col)
|
||||||
|
|
||||||
elif self.field_mode == 1:
|
elif self.field_mode == 1:
|
||||||
|
@ -49,17 +50,23 @@ class ShipField:
|
||||||
if self.check_possible(row, col):
|
if self.check_possible(row, col):
|
||||||
if self.ship_direction == 0:
|
if self.ship_direction == 0:
|
||||||
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:
|
||||||
self.field[r * self.field_size + col] = "p"
|
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 == 1:
|
||||||
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:
|
||||||
self.field[row * self.field_size + c] = "p"
|
self.field[row * self.field_size + c] = "p"
|
||||||
|
else:
|
||||||
|
self.field[r * self.field_size + col] = "r"
|
||||||
else:
|
else:
|
||||||
self.field[row * self.field_size + col] += "+"
|
self.field[row * self.field_size + col] += "+"
|
||||||
|
|
||||||
def clear_marker(self):
|
def clear_marker(self):
|
||||||
for i in range(0, len(self.field)):
|
for i in range(0, len(self.field)):
|
||||||
if self.field[i] == "p" or self.field[i] == "+":
|
if self.field[i] == "p" or self.field[i] == "r":
|
||||||
self.field[i] = ""
|
self.field[i] = ""
|
||||||
|
|
||||||
if "+" in self.field[i]:
|
if "+" in self.field[i]:
|
||||||
|
@ -84,6 +91,10 @@ class ShipField:
|
||||||
index = row * self.field_size + c
|
index = row * self.field_size + c
|
||||||
self.field[index] = "1"
|
self.field[index] = "1"
|
||||||
|
|
||||||
|
if self.ship_size in self.ships:
|
||||||
|
self.ships.remove(self.ship_size)
|
||||||
|
self.print_field()
|
||||||
|
|
||||||
def shoot(self, row, col):
|
def shoot(self, row, col):
|
||||||
if row < 0 or row > self.field_size - 1:
|
if row < 0 or row > self.field_size - 1:
|
||||||
return ShootResult.UNDEFINED
|
return ShootResult.UNDEFINED
|
||||||
|
@ -164,6 +175,7 @@ class ShipField:
|
||||||
self.field_mode = 0
|
self.field_mode = 0
|
||||||
|
|
||||||
def print_field(self):
|
def print_field(self):
|
||||||
|
print(self.ships)
|
||||||
for r in range(0, self.field_size):
|
for r in range(0, self.field_size):
|
||||||
blocked_string = ""
|
blocked_string = ""
|
||||||
ship_string = ""
|
ship_string = ""
|
||||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -35,6 +35,8 @@ def colorize(field, buttons):
|
||||||
bg = 'blue'
|
bg = 'blue'
|
||||||
if "+" in field[i]:
|
if "+" in field[i]:
|
||||||
bg = 'orange'
|
bg = 'orange'
|
||||||
|
if "r" in field[i]:
|
||||||
|
bg = 'red'
|
||||||
buttons[i].configure(bg=bg)
|
buttons[i].configure(bg=bg)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -114,9 +114,6 @@ class TestShipField(TestCase):
|
||||||
new_field_string = str.join(' ', ship_field.field)
|
new_field_string = str.join(' ', ship_field.field)
|
||||||
self.assertEqual(new_field_string, old_field_string)
|
self.assertEqual(new_field_string, old_field_string)
|
||||||
|
|
||||||
def test_set_ship(self):
|
|
||||||
self.fail()
|
|
||||||
|
|
||||||
def test_check_possible(self):
|
def test_check_possible(self):
|
||||||
self.fail()
|
self.fail()
|
||||||
|
|
||||||
|
@ -150,8 +147,37 @@ class TestShipField(TestCase):
|
||||||
|
|
||||||
def test_toggle_ship_direction(self):
|
def test_toggle_ship_direction(self):
|
||||||
# arrangement установка
|
# arrangement установка
|
||||||
# action действие
|
|
||||||
#assertion проверка
|
|
||||||
ship_field = ShipField()
|
ship_field = ShipField()
|
||||||
|
ship_field.set_ship_direction(0)
|
||||||
|
# action действие
|
||||||
ship_field.toggle_ship_direction()
|
ship_field.toggle_ship_direction()
|
||||||
|
# assertion проверка
|
||||||
self.assertEqual(ship_field.ship_direction, 1)
|
self.assertEqual(ship_field.ship_direction, 1)
|
||||||
|
|
||||||
|
ship_field.toggle_ship_direction()
|
||||||
|
self.assertEqual(ship_field.ship_direction, 0)
|
||||||
|
|
||||||
|
def test_set_ship(self):
|
||||||
|
# arrangement установка
|
||||||
|
ship_field = ShipField()
|
||||||
|
ship_field.set_ship_size(4)
|
||||||
|
ship_field.set_ship_direction(1)
|
||||||
|
# action действие
|
||||||
|
ship_field.set_ship(5, 3)
|
||||||
|
# assertion проверка
|
||||||
|
self.assertEqual(ship_field.field[53].strip(), '1')
|
||||||
|
self.assertEqual(ship_field.field[54].strip(), '1')
|
||||||
|
self.assertEqual(ship_field.field[55].strip(), '1')
|
||||||
|
self.assertEqual(ship_field.field[56].strip(), '1')
|
||||||
|
|
||||||
|
def test_set_ship_size4_vertical_outofrange(self):
|
||||||
|
# arrangement установка
|
||||||
|
ship_field = ShipField()
|
||||||
|
ship_field.set_ship_size(4)
|
||||||
|
ship_field.set_ship_direction(0)
|
||||||
|
old_field_string = str.join(" ", ship_field.field)
|
||||||
|
# action действие
|
||||||
|
ship_field.set_ship(7, 3)
|
||||||
|
# assertion проверка
|
||||||
|
new_field_string = str.join(" ", ship_field.field)
|
||||||
|
self.assertEqual(new_field_string, old_field_string)
|
Loading…
Reference in New Issue