From 23fd823e5060a590b37a9d0b6b0da5d5e02c5e81 Mon Sep 17 00:00:00 2001 From: ehermakov Date: Sun, 11 Feb 2024 18:03:07 +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=BC=D0=B5=D0=B9=D0=BD=20=D1=81=20=D1=83=D1=87=D0=B5?= =?UTF-8?q?=D1=82=D0=BE=D0=BC=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D0=B4=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D0=BE=D0=B3=D0=BE=20=D1=83=D1=80=D0=BE=D0=BA=D0=B0?= =?UTF-8?q?=20=2005022024?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HNS/Excercises/ShipCraft/123.py | 19 --------- HNS/Excercises/ShipCraft/ShipField.py | 57 +++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 19 deletions(-) delete mode 100644 HNS/Excercises/ShipCraft/123.py create mode 100644 HNS/Excercises/ShipCraft/ShipField.py diff --git a/HNS/Excercises/ShipCraft/123.py b/HNS/Excercises/ShipCraft/123.py deleted file mode 100644 index 5094f20..0000000 --- a/HNS/Excercises/ShipCraft/123.py +++ /dev/null @@ -1,19 +0,0 @@ -import tkinter as tk - -root = tk.Tk() - -l1 = tk.Label(root, text='Col:8 Row:1', bg='red') -l1.grid(column=8, row=1) - -l2 = tk.Label(root, text='Col:1 Row:8', bg='red') -l2.grid(column=1, row=8) - -# add empty label in row 0 and column 0 -l0 = tk.Label(root, text=' \n ', bg='green') -l0.grid(column=0, row=0) - -# add empty label in row 9 and column 9 -l9 = tk.Label(root, text=' ', bg='green') -l9.grid(column=9, row=9) - -root.mainloop() \ No newline at end of file diff --git a/HNS/Excercises/ShipCraft/ShipField.py b/HNS/Excercises/ShipCraft/ShipField.py new file mode 100644 index 0000000..e509a47 --- /dev/null +++ b/HNS/Excercises/ShipCraft/ShipField.py @@ -0,0 +1,57 @@ +from main import ShootResult + + +class ShipField: + + field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] + + field_size = 10 + field_mode = 0 + ship_size = 4 + ship_direction = 0 + + def __init__(self): + pass + + def set_ship(self, row, col): + if row < 0 or row > self.field_size: + return + if col < 0 or col > self.field_size: + return + index = row * self.field_size + col + if self.ship_direction == 0: + 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.field_size - col < self.ship_size: + return + for c in range(col, col + self.ship_size): + index = row * self.field_size + c + self.field[index] = "1" + + def shoot(self, row, col): + if row < 0 or row > self.field_size - 1: + return ShootResult.UNDEFINED + if col < 0 or col > self.field_size - 1: + return ShootResult.UNDEFINED + index = row * self.field_size + col + if (self.field[index]).strip() == "": + self.field[index] = "0" + return ShootResult.EMPTY + elif (self.field[index]).strip() == "1": + self.field[index] = "\\" + return ShootResult.DAMAGED + else: + return ShootResult.UNDEFINED