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 78bf1a3..46391ef 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/Переделка/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