From 6322addc9977d5540095baa9bd175fe5bc1703ab Mon Sep 17 00:00:00 2001 From: ehermakov Date: Mon, 22 Apr 2024 22:45:02 +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=B4=D0=BE=2034=20=D0=BC=D0=B8=D0=BD?= =?UTF-8?q?=2018=20=D1=87=D0=B0=D1=81=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HNS/Excercises/ShipCraft/Переделка/Files.py | 7 ++----- .../ShipCraft/Переделка/ShipField.py | 6 ++++++ .../__pycache__/ShipField.cpython-311.pyc | Bin 12867 -> 13176 bytes HNS/Excercises/ShipCraft/Переделка/game1.json | 7 +++++++ HNS/Excercises/ShipCraft/Переделка/game1.txt | 10 ++++++++++ HNS/Excercises/ShipCraft/Переделка/main.py | 15 ++++++++++++--- HNS/Excercises/ShipCraft/Переделка/test.json | 1 + 7 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 HNS/Excercises/ShipCraft/Переделка/game1.json create mode 100644 HNS/Excercises/ShipCraft/Переделка/game1.txt create mode 100644 HNS/Excercises/ShipCraft/Переделка/test.json diff --git a/HNS/Excercises/ShipCraft/Переделка/Files.py b/HNS/Excercises/ShipCraft/Переделка/Files.py index 6a5769e..5a541a6 100644 --- a/HNS/Excercises/ShipCraft/Переделка/Files.py +++ b/HNS/Excercises/ShipCraft/Переделка/Files.py @@ -11,7 +11,7 @@ def list_levels(): return result -def load (file): +def load(file): # 1 загрузить весь файл в строку f = open(file, "r") s = f.read() @@ -25,9 +25,6 @@ def load (file): # а - массив строк, в которых хранятся числа b = [] - for x in a: - b.append(int(x)) - # b - массив чисел из а f.close() - return b + return a diff --git a/HNS/Excercises/ShipCraft/Переделка/ShipField.py b/HNS/Excercises/ShipCraft/Переделка/ShipField.py index 28e34ef..2505b6c 100644 --- a/HNS/Excercises/ShipCraft/Переделка/ShipField.py +++ b/HNS/Excercises/ShipCraft/Переделка/ShipField.py @@ -249,3 +249,9 @@ class ShipField: print(ship_string[:-2]) # print(blocked_string[:-2] + ' ' + ship_string[:-2]) print("********************************************************************") + + @staticmethod + def convert_to_json(obj): + if isinstance(obj, ShipField): + result = obj.__dict__ + return result diff --git a/HNS/Excercises/ShipCraft/Переделка/__pycache__/ShipField.cpython-311.pyc b/HNS/Excercises/ShipCraft/Переделка/__pycache__/ShipField.cpython-311.pyc index 78bf1a311d1cce65fa55d57592a205e37162a37f..46391efbdd330830b9742fa4145dc11f11577198 100644 GIT binary patch delta 472 zcmX?{@*|CJIWI340}y=Lu9n84Hj!@u7ZZpJ1fN?b?#Pu%6;2UsVOR#_uLg^Ph$xX1 zpEmi77E1Za0J_JA6N|0tLXLMbbd!eww0_uj-cw+~NT` zuOu@$H?<@qKcy&rvYUY%quSA2?jQy4@}IgKy5(cBQucO5`z!`i%u>zR8r0X x8VEKO?EYUIHo5sJr8%i~MY%u;P{0+JPu^wdU|-G52s8^j`4KGs1&1uybO1&adJg~q delta 197 zcmey7b~uG^IWI340}%AOsiqaEPUKs_#R%d8!RNe*J961l1cMngg*H1fF4trB(-fZk z!k}bwzM(WD@8)hpRz_|HF4i!{4`6cg79%C)RG`KpX%Ha^Bz|$&BCKZgvfotqXcXZ05P&KJpcdz diff --git a/HNS/Excercises/ShipCraft/Переделка/game1.json b/HNS/Excercises/ShipCraft/Переделка/game1.json new file mode 100644 index 0000000..9aaf6a2 --- /dev/null +++ b/HNS/Excercises/ShipCraft/Переделка/game1.json @@ -0,0 +1,7 @@ +{ + "my field": { + "field": [], + "ships": [], + "field_mode": "PUT" + } +} \ No newline at end of file diff --git a/HNS/Excercises/ShipCraft/Переделка/game1.txt b/HNS/Excercises/ShipCraft/Переделка/game1.txt new file mode 100644 index 0000000..8ebffb6 --- /dev/null +++ b/HNS/Excercises/ShipCraft/Переделка/game1.txt @@ -0,0 +1,10 @@ +0, 0, 1, 1, 1, 0, 0, 0, 0, 0, +0, 0, 0, 0, 0, 0, 0, 0, 0, \\, +0, 0, 0, 0, 0, 1, 0, 0, 0, \\, +0, 0, 0, 0, 0, 1, 0, 0, 0, \\, +0, 0, 0, 0, 0, 1, 0, 0, 0, 0, +0, 0, 0, 0, 0, 1, 0, 0, 0, 0, +0, 0, 0, 0, 0, 0, 0, 0, 1, 0, +1, 0, 0, 0, 0, 0, 0, 0, 0, 0, +0, 0, 0, 1, 0, 0, 0, 0, 0, 0, +0, 0, 0, 0, 0, 0, 0, 0, 1, 0 \ No newline at end of file diff --git a/HNS/Excercises/ShipCraft/Переделка/main.py b/HNS/Excercises/ShipCraft/Переделка/main.py index 944c595..cfa5695 100644 --- a/HNS/Excercises/ShipCraft/Переделка/main.py +++ b/HNS/Excercises/ShipCraft/Переделка/main.py @@ -1,7 +1,7 @@ +import json from tkinter import * from ShipField import ShipField - my_field = ShipField() enemy_field = ShipField() @@ -79,9 +79,14 @@ def button_enter(buttons, row, col): colorize(enemy_field, enemy_buttons) +def savebutton_click(event): + with open("test.json", 'w') as f: + json.dump({'shapes': my_field}, f, default=ShipField.convert_to_json) + + window = Tk() window.title("Ship Craft!") -window.geometry('940x410') +window.geometry('940x510') window.bind_all('', keypress_handler) my_field.toggle_ship_direction() @@ -99,4 +104,8 @@ enemy_buttons = draw_field(window, enemy_field, 11) lbl = Label(window, text='', width=5, height=2) lbl.grid(column=10, row=0) -window.mainloop() \ No newline at end of file +savebutton = Button(window, text='Save', width=20, height=2) +savebutton.bind("", savebutton_click) +savebutton.grid(column=0, row=11, columnspan=4) + +window.mainloop() diff --git a/HNS/Excercises/ShipCraft/Переделка/test.json b/HNS/Excercises/ShipCraft/Переделка/test.json new file mode 100644 index 0000000..2571408 --- /dev/null +++ b/HNS/Excercises/ShipCraft/Переделка/test.json @@ -0,0 +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