From dc1ba63fefe4ffc0b5a39c22afa359f252ad3bb7 Mon Sep 17 00:00:00 2001 From: danii Date: Mon, 29 Apr 2024 19:33:00 +0200 Subject: [PATCH] Lesson commit --- HNC/Exercises/Ship_Battle/ShipDirection.py | 11 ++++++++++- HNC/Exercises/Ship_Battle/ShipField.py | 13 +++++++++++-- .../__pycache__/ShipDirection.cpython-311.pyc | Bin 535 -> 999 bytes .../__pycache__/ShipField.cpython-311.pyc | Bin 13269 -> 13698 bytes HNC/Exercises/Ship_Battle/main.py | 12 +++++------- HNC/Exercises/Ship_Battle/test.json | 1 - test.json | 1 + 7 files changed, 27 insertions(+), 11 deletions(-) delete mode 100644 HNC/Exercises/Ship_Battle/test.json create mode 100644 test.json 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 d82802b35b45f8a9de82d66a7ef0997b9ce925e7..1dbdd7708495c7cf0923ca70a393dd936a418b04 100644 GIT binary patch literal 999 zcmZuv&r2IY6n>N4jWL==%eE?1@SulaYpusp1gk-ST6#tpn1{wy0LQlO#(o;{JNz!eCeY^Y3d+&Sm-o7{UDV0iskau72@17ul zAJX_MT32b!38e}I2y($gCPEN{RUolrAPPli3c$JeI%39%x&;~Sr!+H&t69I~*o2~i$-z)EY-`D(L@xs1-;B(v|n_JjEu%Lw|GbyPukYL6I!lwM43DmK7_GH zy%&@P7_)@%WMX`fS%>(*a!ZtJ?In;6Pfh(^ClB*WEG#j+(Qv^_TTWKT~_pP5HiPqc{I^?5_3J&peP)y3aG&M zA-xI8;N6BkS0Z!uxC^t$$3_#~;+v^1U3JKHaI0&DKU{ zFVlDG>FHWxx+PM9kxB3b#=hlIjCoQ_ph!xt)Q2&CRkGX;!{Q>A4M>oK;ln{_g^umf zuuvpyM7TZ!8x-GS+`0vD{P7jN0ToCe>1sejI3PPv>w`%=wQyoU4 R^mRB{xsu*r|DoE&{s4^0;V=LI delta 231 zcmaFPKAnYcIWI340}$LhqMz2rFp)2fixI>Hg3nSD>m=DySb`ZeStoARWA@Wzo4kR^ zn0+O~XOQ&dr;OeiMXW#;m|zAnie-UB103Gqmu&F3At>A6b%Rg5!5svPcwy@OG?{O) zq~?|87J+m^%;5pCz$*BFtX~{9x%nxjIjMF<0+YL$Rn3_hB|b1<5))iLf<(VykpdeA E0IZTS#Q*>R diff --git a/HNC/Exercises/Ship_Battle/__pycache__/ShipField.cpython-311.pyc b/HNC/Exercises/Ship_Battle/__pycache__/ShipField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x$doefLh&2Gn^i5AgQy#;c5jga}YA)uB@|~PAL<;nv=f=kn|M4HM GDEb?-_v`-v 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