diff --git a/HNS/Excercises/ShipCraft/Переделка/ShipDirection.py b/HNS/Excercises/ShipCraft/Переделка/ShipDirection.py index cf85028..c794076 100644 --- a/HNS/Excercises/ShipCraft/Переделка/ShipDirection.py +++ b/HNS/Excercises/ShipCraft/Переделка/ShipDirection.py @@ -2,6 +2,14 @@ from enum import Enum class ShipDirection(Enum): - VERTICAL = 0 - HORIZONTAL = 1 - UNKNOWN = -1 + VERTICAL = 'VERTICAL' + HORIZONTAL = 'HORIZONTAL' + UNKNOWN = 'UNKNOWN' + + @staticmethod + def from_string(raw_value): + if raw_value: + value = raw_value.upper() + if value in ShipDirection.__members__: + return ShipDirection[value] + return ShipDirection.UNKNOWN diff --git a/HNS/Excercises/ShipCraft/Переделка/ShipField.py b/HNS/Excercises/ShipCraft/Переделка/ShipField.py index 2505b6c..459ba91 100644 --- a/HNS/Excercises/ShipCraft/Переделка/ShipField.py +++ b/HNS/Excercises/ShipCraft/Переделка/ShipField.py @@ -1,3 +1,5 @@ +import copy + from ShootResult import ShootResult from ShipDirection import ShipDirection from ShipMode import ShipMode @@ -23,6 +25,14 @@ class ShipField: self.ship_size = 4 self.ship_direction = ShipDirection.VERTICAL + def from_json(self, obj): + self.field = obj['field'] + self.ships = obj['ships'] + self.field_size = obj['field_size'] + self.field_mode = ShipMode.from_string(obj['field_mode']) + self.ship_size = obj['ship_size'] + self.ship_direction = ShipDirection.from_string(obj['ship_direction']) + def __getitem__(self, item): if item is None: return None @@ -130,7 +140,7 @@ class ShipField: if ship_direction == ShipDirection.UNKNOWN: - # проверим горизонталь + # проверим горизонталь for c in range(col + 1, self.field_size): if self.check_ship(row, c): ship_size += 1 @@ -165,14 +175,11 @@ class ShipField: else: return ShootResult.UNDEFINED - - def check_ship(self, row, col): - #функция должна возвражать тру, если в заданной клетке есть корабль - # в противном случае фолс + # функция должна возвражать тру, если в заданной клетке есть корабль + # в противном случае фолс return self.field[row * self.field_size + col].strip() == "1" - def check_possible(self, row, col): # Функция должна возвращать True, если можно поставить сюда корабль, # в противном случае - False @@ -225,7 +232,6 @@ class ShipField: if value != ShipDirection.UNKNOWN: self.ship_direction = value - def toggle_ship_direction(self): if self.ship_direction == ShipDirection.VERTICAL: self.ship_direction = ShipDirection.HORIZONTAL @@ -253,5 +259,7 @@ class ShipField: @staticmethod def convert_to_json(obj): if isinstance(obj, ShipField): - result = obj.__dict__ + result = copy.deepcopy(obj.__dict__) + result['field_mode'] = obj.field_mode.value + result['ship_direction'] = obj.ship_direction.value return result diff --git a/HNS/Excercises/ShipCraft/Переделка/ShipMode.py b/HNS/Excercises/ShipCraft/Переделка/ShipMode.py index 9516bfc..f9e97b8 100644 --- a/HNS/Excercises/ShipCraft/Переделка/ShipMode.py +++ b/HNS/Excercises/ShipCraft/Переделка/ShipMode.py @@ -2,9 +2,16 @@ from enum import Enum class ShipMode(Enum): - PUT = 0 - SHOOT = 1 - - + PUT = 'PUT' + SHOOT = 'SHOOT' + UNKNOWN = 'UNKNOWN' + + @staticmethod + def from_string(raw_value): + if raw_value: + value = raw_value.upper() + if value in ShipMode.__members__: + return ShipMode[value] + return ShipMode.UNKNOWN diff --git a/HNS/Excercises/ShipCraft/Переделка/__pycache__/ShipDirection.cpython-311.pyc b/HNS/Excercises/ShipCraft/Переделка/__pycache__/ShipDirection.cpython-311.pyc index b6853b9..68023d7 100644 Binary files a/HNS/Excercises/ShipCraft/Переделка/__pycache__/ShipDirection.cpython-311.pyc and b/HNS/Excercises/ShipCraft/Переделка/__pycache__/ShipDirection.cpython-311.pyc differ diff --git a/HNS/Excercises/ShipCraft/Переделка/__pycache__/ShipField.cpython-311.pyc b/HNS/Excercises/ShipCraft/Переделка/__pycache__/ShipField.cpython-311.pyc index 46391ef..4b61648 100644 Binary files a/HNS/Excercises/ShipCraft/Переделка/__pycache__/ShipField.cpython-311.pyc and b/HNS/Excercises/ShipCraft/Переделка/__pycache__/ShipField.cpython-311.pyc differ diff --git a/HNS/Excercises/ShipCraft/Переделка/__pycache__/ShipMode.cpython-311.pyc b/HNS/Excercises/ShipCraft/Переделка/__pycache__/ShipMode.cpython-311.pyc index 98430f4..3de8576 100644 Binary files a/HNS/Excercises/ShipCraft/Переделка/__pycache__/ShipMode.cpython-311.pyc and b/HNS/Excercises/ShipCraft/Переделка/__pycache__/ShipMode.cpython-311.pyc differ diff --git a/HNS/Excercises/ShipCraft/Переделка/file_path b/HNS/Excercises/ShipCraft/Переделка/file_path new file mode 100644 index 0000000..4289e18 --- /dev/null +++ b/HNS/Excercises/ShipCraft/Переделка/file_path @@ -0,0 +1 @@ +{"my_field": {"field": [" ", " ", " ", " ", " ", " ", "1", " ", " ", " ", " ", "1", "1", "1", "1", " ", "1", " ", " ", " ", " ", " ", " ", " ", " ", " ", "1", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "", " ", " ", " ", " ", " ", " ", " ", " ", " ", "1", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "", " ", " ", " ", "1", " ", "", " ", " ", "", " ", " ", " ", " ", " ", " ", " ", " ", " ", "", " ", " ", "p", "", "", " ", " ", "", "", " ", " "], "ships": [3, 2, 2, 2, 1, 1], "field_size": 10, "field_mode": "PUT", "ship_size": 1, "ship_direction": "VERTICAL"}} \ No newline at end of file diff --git a/HNS/Excercises/ShipCraft/Переделка/main.py b/HNS/Excercises/ShipCraft/Переделка/main.py index cfa5695..4fb54d4 100644 --- a/HNS/Excercises/ShipCraft/Переделка/main.py +++ b/HNS/Excercises/ShipCraft/Переделка/main.py @@ -1,5 +1,8 @@ import json +import os from tkinter import * +from tkinter import filedialog + from ShipField import ShipField my_field = ShipField() @@ -80,8 +83,21 @@ def button_enter(buttons, row, col): def savebutton_click(event): - with open("test.json", 'w') as f: - json.dump({'shapes': my_field}, f, default=ShipField.convert_to_json) + file_path = filedialog.asksaveasfilename(filetypes=[('JSON files', '*.json')]) + if file_path: + with open("file_path", 'w') as f: + json.dump({'my_field': my_field}, f, default=ShipField.convert_to_json) + + +def loadbutton_click(event): + global my_field + + file_path = filedialog.askopenfilename(filetypes=[('JSON files', '*.json')]) + if os.path.isfile(file_path): + with open("file_path") as lines: + my_field.from_json(json.load(lines)['my_field']) + + colorize(my_field, my_buttons) window = Tk() @@ -108,4 +124,8 @@ savebutton = Button(window, text='Save', width=20, height=2) savebutton.bind("", savebutton_click) savebutton.grid(column=0, row=11, columnspan=4) +loadbutton = Button(window, text='Load', width=20, height=2) +loadbutton.bind("", loadbutton_click) +loadbutton.grid(column=5, row=11, columnspan=4) + window.mainloop() diff --git a/HNS/Excercises/ShipCraft/Переделка/test.json b/HNS/Excercises/ShipCraft/Переделка/test.json index 2571408..c748bd8 100644 --- a/HNS/Excercises/ShipCraft/Переделка/test.json +++ b/HNS/Excercises/ShipCraft/Переделка/test.json @@ -1 +1 @@ -{"shapes": {"field": [" ", " ", " ", " ", " ", " ", "1", " ", " ", " ", " ", "1", "1", "1", "1", " ", "1", " ", " ", " ", " ", " ", " ", " ", " ", " ", "1", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "", " ", " ", " ", "1", " ", " ", " ", "", "", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "p", "", " ", " ", " ", " ", " ", " "], "ships": [3, 2, 2, 2, 1, 1, 1], "field_size": 10, "field_mode": null, "ship_size": 1, "ship_direction": null}} \ No newline at end of file +{"my_field": {"field": [" ", " ", " ", " ", " ", " ", "1", " ", "1", " ", " ", "1", "1", "1", "1", " ", "1", " ", "1", " ", " ", " ", " ", " ", " ", " ", "1", " ", "1", " ", " ", "", "", "", "", " ", " ", " ", " ", " ", "", "1", "", " ", "1", " ", "", "1", " ", " ", "", "1", "", " ", " ", " ", " ", " ", " ", " ", "", "", " ", " ", " ", " ", " ", "", " ", " ", " ", "", " ", "1", " ", "1", " ", "1", " ", " ", " ", "1", " ", " ", " ", "1", " ", " ", " ", " ", " ", "1", " ", " ", " ", "", "", "", " ", " "], "ships": [], "field_size": 10, "field_mode": "PUT", "ship_size": 2, "ship_direction": "VERTICAL"}} \ No newline at end of file