From 932aa4a8f889442e3a5d4d1a15d5ea8d615c04bf Mon Sep 17 00:00:00 2001 From: ehermakov Date: Sat, 27 Apr 2024 15:19:30 +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=B4=D0=B7=20=D0=BF=D0=BE=20=D0=B8=D1=82=D0=BE=D0=B3?= =?UTF-8?q?=D0=B0=D0=BC=20=D0=B2=D0=B8=D0=B4=D0=B5=D0=BE=2018=20=D0=B2?= =?UTF-8?q?=D1=81=D1=82=D1=80=D0=B5=D1=87=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ShipCraft/Переделка/ShipDirection.py | 14 +++++++--- .../ShipCraft/Переделка/ShipField.py | 24 ++++++++++++------ .../ShipCraft/Переделка/ShipMode.py | 15 ++++++++--- .../__pycache__/ShipDirection.cpython-311.pyc | Bin 618 -> 1020 bytes .../__pycache__/ShipField.cpython-311.pyc | Bin 13176 -> 13924 bytes .../__pycache__/ShipMode.cpython-311.pyc | Bin 575 -> 994 bytes HNS/Excercises/ShipCraft/Переделка/file_path | 1 + HNS/Excercises/ShipCraft/Переделка/main.py | 24 ++++++++++++++++-- HNS/Excercises/ShipCraft/Переделка/test.json | 2 +- 9 files changed, 62 insertions(+), 18 deletions(-) create mode 100644 HNS/Excercises/ShipCraft/Переделка/file_path 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 b6853b9174873630d3dce330abdcb33b64bbfadc..68023d72970bbbf797b637512cdf408b643ddad5 100644 GIT binary patch delta 679 zcmaFG@`s&oIWI340}$vu*GX$-oXD5P#RTF4!DoYsb&~a|Y$+@)49kH0)nHK&5yhUu z8qA={c8fPSBeTFIvnVyWBr`wn7Dt$CP>83qqt7ia5C0&~D1W~YAe%kZ&)d&G+%K6C zZU6%V$P#8C{#*kjrZdzq6zSJ6EC906Ks}I;YI!YV4Pyz+jX>EfIJ=CYh!_p`wQ*#R=3>Yz-tD7#?to zd|+Va)VU!dJKcYh|3wkiD7FDdGnT ztz;+?07>(K2#`m?1PhQ+Yy%`3;P3{&WP`^ILD>ec8+_so?hjZ6KQOVfs(xSq5+9iv z*o0bQ5CUM)B7w;_7-ghHL59NZ_{Cw9o1apelWJEaKADe6)ts9VXa*)R!Q~@J^a~a# GuweiT>6+aD delta 265 zcmeyv{)&ZfIWI340}!~_six&KOyo=BVgzx4;Iq`kI!W~umS6@=)?2*68JPtxnMJ9| zC7JnoMNB~Xmmq~N{{w-aCfnrWOe$)(IKo_mLOh)veQt4i_y>7L`TKfR)GE#gX tUHl+I07(4eu*uC&Da}c>D-xQ#fLYa?nNjcq112%S=0fiX*I*=@H_td5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HChhQZ&oF9%Mm?b6y$E*Wt@ zG`@7i<9N%Py74Zbrn6ZD%ZD%`7at_0hEVTqMz@Sw(L%0EpZC|jFemPW=Y%*O>4)p~sYsWnZ4OTzNbv=6A-b4uMp{I@iCaLShVZ;%*`gU)digdSLQpis#{E z$mvEn4Z3^FZ=+<<_#W1GR(v-R1xNff@nipipKOQpJ7`%d*(W+CXW<92H2KY;)eA$K z5UJ!F@UvY@ehb+8^QqOSY`v`V3i0O&s9w$MmO3q-rlKi1Sub2&7Om>RX5()1@@B2L zMkBm0hEM(uXGCZ^nQhx*H;$g-d$0w|_<)xgmvyMlv9eX(v}&uHC4a`o9J)QOajVD$@SRu$FI31W>tz4=Ywe_mbucPWyCb`@R zqNHW!^zf`0Fm{jK6YtCnh@F{oU{v+ypuwKQ4&lknLr7#Z8B_IQ>InNP^Yw18G?YVy z)Vd@T{~`_w4ZJ%j)CbAPO_Hc=naZE7uT`r0Ez?-w71UuS%({)xPyt|1{F0e{{f-LA P_}{oM_0Rt2RMPwleZ?t; diff --git a/HNS/Excercises/ShipCraft/Переделка/__pycache__/ShipMode.cpython-311.pyc b/HNS/Excercises/ShipCraft/Переделка/__pycache__/ShipMode.cpython-311.pyc index 98430f4fc673d75e23ab45fa65300b59a41f595f..3de8576b0716ec0a019a9b7f3c7963671f2e590c 100644 GIT binary patch delta 648 zcmdnb@`#;pIWI340}yaN*GWrcoXD5P#RTF4!DoYsb>j6=tf_1%EG-PnfFi5G;vgc5 zJ%u%xL6hwkM{q`Ffp30F>MiDg(2!fK!5;qpA-C8={k;AB!~K#O;W`)?K;|+7@#h*K zF`c1?p-8`mVF8eh2@samgCuJiYZyyl&I0nY;OsJnB3@*f8b(RDX~7K0GFc$2!2sxP zMn6rKTdbu81*t_vtU$qA-0|_bskuq1MaA*)MI1m8O{QC%MTzC{!exm$rKz`A!BjEG zUIm4T&u8Y{;!Z2d&y6oGDay=CuM&ZUf*z76Cs1**6_99Pc)%_4fq|J*{f3C_bpJ{I z7e!RBh^ThB-ryIR%W*|Q=K?CCg-M>WaOt5@dJfcG874br1?Mu$Xj571;{A20TK;xc!P_- z!TAP{aD(d&KJf}@kIW2gLM<@}0kCM1z~r@zGE$--L*aJ(;;_lh hPbtkwwJQ>z{FqVIoSPA71}4!_@)0EZ1&b8eFaTwfk1hZJ delta 229 zcmaFFzMq9}IWI340}#}HQArD9n8=sL#R%d8!Dq3Fb>h;&44N#rID#`W3w-laQj3^? z5-&k2{4`l7uVqpZzQr668gh#@*u&pHWF^CAkjBZ&7-#Ahu>x6Of(6JZmH-kBaCn1@ zyTSPe4{w7j2o~{94q%p%;sI&k1rdBe;unWaZhlH>PO4pzz~nY&RdXgrz7GtTL`TU- LkmwgIQeeXXLBuo$ 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