From b0c04bf94ca0b4332f3edc008c06d62b091b6e86 Mon Sep 17 00:00:00 2001 From: ehermakov Date: Sun, 19 Nov 2023 20:45:31 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B6=D0=B0?= =?UTF-8?q?=D1=8E=20=D0=B5=D1=89=D0=B5=20=D0=BD=D0=B5=D0=BC=D0=BD=D0=BE?= =?UTF-8?q?=D0=B3=D0=BE=20=D0=B4=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= =?UTF-8?q?=D0=BD=D0=BD=D0=BE=D0=B5=20=D0=B4=D0=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HNS/Excercises/ShipCraft/.idea/.gitignore | 3 ++ HNS/Excercises/ShipCraft/.idea/ShipCraft.iml | 10 ++++ .../inspectionProfiles/profiles_settings.xml | 6 +++ HNS/Excercises/ShipCraft/.idea/misc.xml | 4 ++ HNS/Excercises/ShipCraft/.idea/modules.xml | 8 ++++ HNS/Excercises/ShipCraft/.idea/vcs.xml | 6 +++ HNS/Excercises/ShipCraft/main.py | 47 +++++++++++++++++++ 7 files changed, 84 insertions(+) create mode 100644 HNS/Excercises/ShipCraft/.idea/.gitignore create mode 100644 HNS/Excercises/ShipCraft/.idea/ShipCraft.iml create mode 100644 HNS/Excercises/ShipCraft/.idea/inspectionProfiles/profiles_settings.xml create mode 100644 HNS/Excercises/ShipCraft/.idea/misc.xml create mode 100644 HNS/Excercises/ShipCraft/.idea/modules.xml create mode 100644 HNS/Excercises/ShipCraft/.idea/vcs.xml diff --git a/HNS/Excercises/ShipCraft/.idea/.gitignore b/HNS/Excercises/ShipCraft/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/HNS/Excercises/ShipCraft/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/HNS/Excercises/ShipCraft/.idea/ShipCraft.iml b/HNS/Excercises/ShipCraft/.idea/ShipCraft.iml new file mode 100644 index 0000000..6209b69 --- /dev/null +++ b/HNS/Excercises/ShipCraft/.idea/ShipCraft.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/HNS/Excercises/ShipCraft/.idea/inspectionProfiles/profiles_settings.xml b/HNS/Excercises/ShipCraft/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/HNS/Excercises/ShipCraft/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/HNS/Excercises/ShipCraft/.idea/misc.xml b/HNS/Excercises/ShipCraft/.idea/misc.xml new file mode 100644 index 0000000..b16dd28 --- /dev/null +++ b/HNS/Excercises/ShipCraft/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/HNS/Excercises/ShipCraft/.idea/modules.xml b/HNS/Excercises/ShipCraft/.idea/modules.xml new file mode 100644 index 0000000..937b603 --- /dev/null +++ b/HNS/Excercises/ShipCraft/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/HNS/Excercises/ShipCraft/.idea/vcs.xml b/HNS/Excercises/ShipCraft/.idea/vcs.xml new file mode 100644 index 0000000..c2365ab --- /dev/null +++ b/HNS/Excercises/ShipCraft/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/HNS/Excercises/ShipCraft/main.py b/HNS/Excercises/ShipCraft/main.py index 74542ae..d51f3b3 100644 --- a/HNS/Excercises/ShipCraft/main.py +++ b/HNS/Excercises/ShipCraft/main.py @@ -5,6 +5,8 @@ from enum import Enum buttons = [] field_size = 10 +ship_size = 4 +ship_direction = 0 empty_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', @@ -77,6 +79,7 @@ def draw_field(window, field): btn = Button(window, text='', bg=bg, width=5, height=2) btn.grid(column=c, row=r) btn.bind('', lambda e, x=r, y=c: button_click(field, x, y)) + btn.bind('', lambda e, x=r, y=c: button_enter(field, x, y)) buttons.append(btn) @@ -97,6 +100,50 @@ def button_click(field, row, col): colorize(field, buttons) +def button_enter(field, row, col): + if ship_direction == 0: + if field_size - row < ship_size: + print("Not OK") + return + if ship_direction == 1: + if field_size - col < ship_size: + print("Not OK") + return + print("OK") + + +def check_possible(field, row, col): + # Функция должна возвращать True, если можно поставить сюда корабль, + # в противном случае - False + + if ship_direction == 0: + if field_size - row >= ship_size: + # Здесь мы знаем, что корабль помещается на поле. + # Теперь нужно проверить, не заблокировано ли какое-то из полей, + # на которые мы хотим поставить корабль. + # Для этого, всего поля, на которые мы его поставили бы исходя из координат row, col + # нужно проверить с помощью вызова метода check_blocked(...) + return True + + if ship_direction == 1: + if field_size - col >= ship_size: + # Здесь мы знаем, что корабль помещается на поле. + # Теперь нужно проверить, не заблокировано ли какое-то из полей, + # на которые мы хотим поставить корабль. + # Для этого, всего поля, на которые мы его поставили бы исходя из координат row, col + # нужно проверить с помощью вызова метода check_blocked(...) + return True + + return False + + +def check_blocked(field, row, col): + # Функция возвращает True, если все клетки вокруг клетки с координатами row, col + # либо находятся за пределами поля, либо в них нет корабля/они пустые + # Во всех других случаях, функция возвращает False + pass + + window = Tk() window.title("Ship Craft!") window.geometry('450x410')