diff --git a/HNC/Exercises/Ship_Battle/ShipDirection.py b/HNC/Exercises/Ship_Battle/ShipDirection.py index 72f4354..163f2f1 100644 --- a/HNC/Exercises/Ship_Battle/ShipDirection.py +++ b/HNC/Exercises/Ship_Battle/ShipDirection.py @@ -3,4 +3,13 @@ from enum import Enum class ShipDirection(Enum): VERTICAL = "VERTICAL" HORIZONTAL = "HORIZONTAL" - UNKNOWN = "UNKNOWN" \ No newline at end of file + UNKNOWN = "UNKNOWN" + + + @staticmethod + def from_string(raw_value): + if raw_value: + value = raw_value.lower().capitalize() + if value in ShipDirection.__members__: + return ShipDirection[value] + return ShipDirection.UNKNOWN \ No newline at end of file diff --git a/HNC/Exercises/Ship_Battle/ShipField.py b/HNC/Exercises/Ship_Battle/ShipField.py index 50a9e9a..3045881 100644 --- a/HNC/Exercises/Ship_Battle/ShipField.py +++ b/HNC/Exercises/Ship_Battle/ShipField.py @@ -25,6 +25,15 @@ class ShipField: 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 = obj['field_mode'] + self.ship_size = obj['ship_size'] + self.ship_direction = obj['ship_direction'] + + def __getitem__(self, item): if item is None: return None @@ -244,8 +253,8 @@ class ShipField: ship_string += self.field[r * self.field_size + c] + ', ' print(blocked_string[:-2] + ' ' + ship_string[:-2]) print("********************************************************************") - - + + @staticmethod def convert_to_json(obj): if isinstance(obj, ShipField): diff --git a/HNC/Exercises/Ship_Battle/__pycache__/ShipDirection.cpython-311.pyc b/HNC/Exercises/Ship_Battle/__pycache__/ShipDirection.cpython-311.pyc index d82802b..1dbdd77 100644 Binary files a/HNC/Exercises/Ship_Battle/__pycache__/ShipDirection.cpython-311.pyc and b/HNC/Exercises/Ship_Battle/__pycache__/ShipDirection.cpython-311.pyc differ diff --git a/HNC/Exercises/Ship_Battle/__pycache__/ShipField.cpython-311.pyc b/HNC/Exercises/Ship_Battle/__pycache__/ShipField.cpython-311.pyc index c3e554d..857639c 100644 Binary files a/HNC/Exercises/Ship_Battle/__pycache__/ShipField.cpython-311.pyc and b/HNC/Exercises/Ship_Battle/__pycache__/ShipField.cpython-311.pyc differ diff --git a/HNC/Exercises/Ship_Battle/main.py b/HNC/Exercises/Ship_Battle/main.py index cbe06d1..a5c85ac 100644 --- a/HNC/Exercises/Ship_Battle/main.py +++ b/HNC/Exercises/Ship_Battle/main.py @@ -84,14 +84,12 @@ def savebutton_click(event): def loadbutton_click(event): - data_list = [] - with open('test.json') as lines: - data = json.load(lines) - for i in data['shapes']: - sh1 = create_shape(i) - data_list.append(sh1) + global my_field - return data_list + with open('test.json') as lines: + my_field.from_json(json.load(lines)["my_field"]) + + colorize(my_field, my_buttons) window = Tk() diff --git a/HNC/Exercises/Ship_Battle/test.json b/HNC/Exercises/Ship_Battle/test.json deleted file mode 100644 index 3041a88..0000000 --- a/HNC/Exercises/Ship_Battle/test.json +++ /dev/null @@ -1 +0,0 @@ -{"shapes": {"field": ["1", "1", "1", "1", " ", " ", " ", " ", "", "1", "", "", " ", " ", " ", "1", " ", "", "", "1", "", "", "", "", "", "", "", "1", "", "", "1", "", "", "", "", "1", "", "", "", "", "1", "", "", "1", "", "", "", "1", " ", " ", "1", "", "", "1", " ", " ", " ", "", "", "", " ", "", "", "", " ", " ", "", "", "", "", " ", "1", "", " ", " ", " ", " ", " ", "", "", " ", "1", "", " ", " ", " ", " ", " ", " ", " ", " ", "1", "", " ", " ", " ", "r", "", "1", "1"], "ships": [], "field_size": 10, "field_mode": "PUT", "ship_size": 1, "ship_direction": "VERTICAL"}} \ No newline at end of file diff --git a/test.json b/test.json new file mode 100644 index 0000000..085d319 --- /dev/null +++ b/test.json @@ -0,0 +1 @@ +{"my_field": {"field": ["1", "", "", " ", " ", "1", "1", "1", " ", "1", " ", " ", " ", "", " ", " ", "", "", " ", "1", " ", "1", "", "", "", " ", "", "1", "", "", " ", "1", "", "", "", "", "", "", "", "", " ", "1", "", "", "", "1", "", "", "", "", " ", "1", "", "", "", "1", "", "", "", "", "", "", "", "", "", "", "", "", "1", "", "", "", "", "", "", "", "", "", "", "", "1", "", "1", "", "r", " ", " ", " ", " ", " ", "1", " ", "", " ", " ", " ", " ", "1", "1", "1"], "ships": [], "field_size": 10, "field_mode": "PUT", "ship_size": 1, "ship_direction": "VERTICAL"}} \ No newline at end of file