Merge remote-tracking branch 'origin/main'

This commit is contained in:
Artur Savitskiy 2024-03-11 19:05:03 +01:00
commit ce3f7e4ea5
4 changed files with 246 additions and 287 deletions

View File

@ -1,26 +1,53 @@
from main import ShootResult
from ShootResult 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 action(self, row, col):
if self.field_mode == 0:
if self.check_possible(row, col):
self.set_ship(row, col)
elif self.field_mode == 1:
self.shoot(row, col)
def target(self, row, col):
if self.field_mode == 0:
self.clear_marker()
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_direction == 1:
for c in range(col, col + self.ship_size):
self.field[row * self.field_size + c] = "p"
def clear_marker(self):
for i in range(0, len(self.field)):
if self.field[i] == "p":
self.field[i] = ""
def set_ship(self, row, col):
if row < 0 or row > self.field_size:
return
@ -55,19 +82,18 @@ class ShipField:
else:
return ShootResult.UNDEFINED
def check_possible(self, row, col, ship_size, ship_direction):
def check_possible(self, row, col):
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):
if not self.check_blocked(r, col):
return False
return True
if ship_direction == 1:
if self.ship_direction == 1:
if self.field_size - col >= self.ship_size:
for c in range(col, col + self.ship_size):
if not self.check_blocked(self, row, c):
if not self.check_blocked(row, c):
return False
return True
@ -83,14 +109,23 @@ 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 type(value) is str and value.isnumeric():
value = int(value)
if 1 <= value <= 4:
self.ship_size = value
def set_ship_direction(self, value):
if value.isnumeric() == 0 or value.isnumeric() == 1:
self.ship_direction = value
if type(value) is str and value.isnumeric():
value = int(value)
if 0 <= value <= 1:
self.ship_direction = value
def toggle_ship_direction(self):
if self.field_mode == 0:
if self.ship_direction == 0:
self.ship_direction = 1
else:
self.ship_direction = 0
def toggle_field_mode(self):
if self.field_mode == 0:

View File

@ -0,0 +1,8 @@
from enum import Enum
class ShootResult(Enum):
EMPTY = "EMPTY"
DAMAGED = "DAMAGED"
KILLED = "KILLED"
UNDEFINED = "UNDEFINED"

View File

@ -1,311 +1,96 @@
from tkinter import *
from enum import Enum
from ShipField import ShipField
my_buttons = []
enemy_buttons = []
my_field = ShipField()
enemy_field = ShipField()
field_size = 10
active_field = 0
ship_size_left = 4
ship_direction_left = 0
field_mode_left = 0
ship_size_right = 4
ship_direction_right = 0
field_mode_right = 0
empty_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
my_field = list(empty_field)
enemy_field = list(empty_field)
# 1. Определите перечисление (Enum) ShootResult со следующими значениями:
# EMPTY (мимо), DAMAGED (ранен), KILLED (убит), UNDEFINED (действие не определено)
class ShootResult(Enum):
EMPTY = "EMPTY"
DAMAGED = "DAMAGED"
KILLED = "KILLED"
UNDEFINED = "UNDEFINED"
def set_ship(field, row, col, ship_size, direction):
if row < 0 or row > field_size:
return
if col < 0 or col > field_size:
return
index = row * field_size + col
if direction == 0:
if field_size - row < ship_size:
return
for r in range(row, row + ship_size):
index = r * field_size + col
field[index] = "1"
if direction == 1:
if field_size - col < ship_size:
return
for c in range(col, col + ship_size):
index = row * field_size + c
field[index] = "1"
def shoot(field, row, col):
if row < 0 or row > field_size - 1:
return ShootResult.UNDEFINED
if col < 0 or col > field_size - 1:
return ShootResult.UNDEFINED
index = row * field_size + col
if (field[index]).strip() == "":
field[index] = "0"
return ShootResult.EMPTY
elif (field[index]).strip() == "1":
field[index] = "\\"
return ShootResult.DAMAGED
else:
return ShootResult.UNDEFINED
active_field = my_field
def draw_field(window, field, col_offset):
buttons = []
for r in range(0, field_size):
for c in range(0, field_size):
index = r * field_size + c
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('<Button-1>', lambda e, x=r, y=c: left_button_click(field, buttons, x, y))
btn.bind('<Button-1>', lambda e, x=r, y=c: left_button_click(buttons, x, y))
btn.bind('<Button-3>', right_button_click)
btn.bind('<Enter>', lambda e, x=r, y=c: button_enter(field, buttons, x, y))
btn.bind('<Enter>', lambda e, x=r, y=c: button_enter(buttons, x, y))
buttons.append(btn)
colorize(field, buttons)
return buttons
def colorize(field, buttons):
for i in range(len(field)):
for i in range(len(field.field)):
bg = "white"
if field[i] == "1":
if field.field[i] == "1":
bg = 'pink'
if field[i] == "\\":
if field.field[i] == "\\":
bg = 'red'
if field[i] == "0":
if field.field[i] == "0":
bg = 'black'
if field[i] == "p":
if field.field[i] == "p":
bg = 'blue'
buttons[i].configure(bg=bg)
def keypress_handler(e):
global ship_size_left
global field_mode_left
global ship_size_right
global field_mode_right
global active_field
if e.keysym.isnumeric():
number = int(e.keysym)
if 1 <= number <= 4:
if active_field == 0:
ship_size_left = number
else:
ship_size_right = number
active_field.set_ship_size(e.keysym)
else:
if e.keysym == 'm':
if active_field == 0:
field_mode = field_mode_left
else:
field_mode = field_mode_right
if field_mode == 0:
field_mode = 1
else:
field_mode = 0
if active_field == 0:
field_mode_left = field_mode
else:
field_mode_right = field_mode
active_field.toggle_field_mode()
def left_button_click(field, buttons, row, col):
global ship_size_left
global ship_direction_left
def left_button_click(buttons, row, col):
global active_field
global ship_size_right
global ship_direction_right
if active_field == 0:
field_mode = field_mode_left
ship_size = ship_size_left
ship_direction = ship_direction_left
else:
field_mode = field_mode_right
ship_size = ship_size_right
ship_direction = ship_direction_right
if field_mode == 0:
if check_possible(field, row, col, ship_size, ship_direction):
set_ship(field, row, col, ship_size, ship_direction)
elif field_mode == 1:
shoot(field, row, col)
colorize(field, buttons)
active_field.action(row, col)
colorize(active_field, buttons)
def right_button_click(d):
global ship_direction_left
global ship_direction_right
global field_mode_left
global field_mode_right
if active_field == 0:
field_mode = field_mode_left
ship_direction = ship_direction_left
else:
field_mode = field_mode_right
ship_direction = ship_direction_right
if field_mode == 0:
if ship_direction == 0:
ship_direction = 1
else:
ship_direction = 0
if active_field == 0:
ship_direction_left = ship_direction
else:
ship_direction_right = ship_direction
def button_enter(field, buttons, row, col):
global active_field
global ship_direction_left
global ship_direction_right
active_field.toggle_ship_direction()
global ship_size_left
global ship_size_right
global field_mode_left
global field_mode_right
def button_enter(buttons, row, col):
global active_field
if buttons == my_buttons:
active_field = 0
active_field = my_field
enemy_field.clear_marker()
my_field.target(row, col)
elif buttons == enemy_buttons:
active_field = 1
if active_field == 0:
field_mode = field_mode_left
ship_direction = ship_direction_left
ship_size = ship_size_left
other_field = enemy_field
other_buttons = enemy_buttons
else:
field_mode = field_mode_right
ship_direction = ship_direction_right
ship_size = ship_size_right
other_field = my_field
other_buttons = my_buttons
for i in range(0, len(other_field)):
if other_field[i] == "p":
other_field[i] = ''
colorize(other_field, other_buttons)
if field_mode == 0:
for i in range(0, len(field)):
if field[i] == "p":
field[i] = ''
if check_possible(field, row, col,ship_size, ship_direction):
if ship_direction == 0:
for r in range(row, row + ship_size):
field[r * field_size + col] = "p"
if ship_direction == 1:
for c in range(col, col + ship_size):
field[row * field_size + c] = "p"
colorize(field, buttons)
def check_possible(field, row, col, ship_size, ship_direction):
# Функция должна возвращать True, если можно поставить сюда корабль,
# в противном случае - False
if ship_direction == 0:
# Здесь мы знаем, что корабль помещается на поле.
if field_size - row >= ship_size:
# Теперь нужно проверить, не заблокировано ли какое-то из полей,
for r in range(row, row + ship_size):
if not check_blocked(field, r, col):
return False
return True
if ship_direction == 1:
if field_size - col >= ship_size:
for c in range(col, col + ship_size):
if not check_blocked(field, row, c):
return False
return True
return False
def check_blocked(field, row, col):
# Функция возвращает True, если все клетки вокруг клетки с координатами row, col
# либо находятся за пределами поля, либо в них нет корабля/они пустые
for r in range(row - 1, row + 2):
for c in range(col - 1, col + 2):
if 0 <= r < field_size and 0 <= c < field_size:
cell = (field[r * field_size + c]).strip()
if cell != '' and cell != 'p':
return False
return True
active_field = enemy_field
my_field.clear_marker()
enemy_field.target(row, col)
colorize(my_field, my_buttons)
colorize(enemy_field, enemy_buttons)
window = Tk()
window.title("Ship Craft!")
window.geometry('940x410')
window.bind_all('<KeyPress>', keypress_handler)
set_ship(my_field, 1, 1, 4, 1)
set_ship(my_field,0, 6, 3, 0)
set_ship(my_field, 7, 3, 1, 0)
my_field.toggle_ship_direction()
my_field.set_ship_size(4)
my_field.set_ship(1, 1)
my_field.toggle_ship_direction()
my_field.set_ship_size(3)
my_field.set_ship(0, 6)
my_field.set_ship_size(1)
my_field.set_ship(7, 3)
my_buttons = draw_field(window, my_field, 0)
enemy_buttons = draw_field(window, enemy_field, 11)
print(len(my_buttons))
print(len(enemy_buttons))
lbl = Label(window, text='', width=5, height=2)
lbl.grid(column=10, row=0)
window.mainloop()
# for r in range(0, field_size):
# blocked_string = ""
# ship_string = ""
# for c in range(0, field_size):
# blocked_string += str(check_blocked(my_field, r, c))[0] + ", "
# ship_string += my_field[r * field_size + c] + ', '
# print(blocked_string[:-2] + ' ' + ship_string[:-2])
# print("********************************************************************")
# for r in range(0, field_size):
# possible_string = ""
# impossible_string = ""
# for c in range(0, field_size):
# possible_string += str(check_possible(my_field, r, c))[0] + ", "
# impossible_string += my_field[r * field_size + c] + ', '
# print(possible_string[:-2] + ' ' + impossible_string[:-2])
window.mainloop()

View File

@ -0,0 +1,131 @@
from unittest import TestCase
from ShipField import ShipField
from ShootResult import ShootResult
class TestShipField(TestCase):
def test_set_ship_size(self):
ship_field = ShipField() # Заводим объект типа ShipField
ship_field.set_ship_size(1)
self.assertEqual(ship_field.ship_size, 1)
ship_field.set_ship_size(2)
self.assertEqual(ship_field.ship_size, 2)
ship_field.set_ship_size(3)
self.assertEqual(ship_field.ship_size, 3)
ship_field.set_ship_size(4)
self.assertEqual(ship_field.ship_size, 4)
def test_set_ship_size_outofrange(self):
ship_field = ShipField() # Заводим объект типа ShipField
ship_field.set_ship_size(1)
ship_field.set_ship_size(-1)
self.assertEqual(ship_field.ship_size, 1)
ship_field.set_ship_size(0)
self.assertEqual(ship_field.ship_size, 1)
ship_field.set_ship_size(6)
self.assertEqual(ship_field.ship_size, 1)
ship_field.set_ship_size(100)
self.assertEqual(ship_field.ship_size, 1)
def test_set_ship_size_wrongtype(self):
ship_field = ShipField() # Заводим объект типа ShipField
ship_field.set_ship_size(1)
ship_field.set_ship_size([])
self.assertEqual(ship_field.ship_size, 1)
ship_field.set_ship_size('')
self.assertEqual(ship_field.ship_size, 1)
ship_field.set_ship_size(None)
self.assertEqual(ship_field.ship_size, 1)
ship_field.set_ship_size(False)
self.assertEqual(ship_field.ship_size, 1)
def test_toggle_field_mode(self):
ship_field = ShipField() # Заводим объект типа ShipField
self.assertEqual(ship_field.field_mode, 0) # Проверяем, что изначальное значение field_mode равно 0
ship_field.toggle_field_mode() # Вызываем метод, который тестируем
self.assertEqual(ship_field.field_mode, 1) # Проверяем, что field_mode принял желаемое значение
ship_field.toggle_field_mode() # Вызываем метод, который тестируем
self.assertEqual(ship_field.field_mode, 0) # Проверяем, что field_mode принял желаемое значение
def test_action(self):
self.fail()
def test_target(self):
self.fail()
def test_clear_marker(self):
ship_field = ShipField()
ship_field.field[0] = 'p'
ship_field.field[ship_field.field_size - 1] = 'p'
ship_field.clear_marker()
self.assertNotIn('p', ship_field.field)
def test_shoot_empty(self):
ship_field = ShipField()
self.assertEqual(ship_field.field[0].strip(), '')
result = ship_field.shoot(0, 0)
self.assertEqual(ship_field.field[0].strip(), '0')
self.assertEqual(result, ShootResult.EMPTY)
def test_shoot_ship(self):
ship_field = ShipField()
ship_field.field[0] = '1'
result = ship_field.shoot(0, 0)
self.assertEqual(ship_field.field[0].strip(), '\\')
self.assertEqual(result, ShootResult.DAMAGED)
def test_shoot_unknown(self):
ship_field = ShipField()
ship_field.field[0] = 'x'
result = ship_field.shoot(0, 0)
self.assertEqual(ship_field.field[0].strip(), 'x')
self.assertEqual(result, ShootResult.UNDEFINED)
def test_shoot_outofrange(self):
ship_field = ShipField()
old_field_string = str.join(' ', ship_field.field)
result = ship_field.shoot(-1, -1)
self.assertEqual(result, ShootResult.UNDEFINED)
result = ship_field.shoot(-1, 0)
self.assertEqual(result, ShootResult.UNDEFINED)
result = ship_field.shoot(1000, 1000)
self.assertEqual(result, ShootResult.UNDEFINED)
result = ship_field.shoot(0, 1000)
self.assertEqual(result, ShootResult.UNDEFINED)
new_field_string = str.join(' ', ship_field.field)
self.assertEqual(new_field_string, old_field_string)
def test_set_ship(self):
self.fail()
def test_check_possible(self):
self.fail()
def test_check_blocked(self):
self.fail()
def test_set_ship_direction(self):
self.fail()
def test_toggle_ship_direction(self):
self.fail()