загружаю дз до 34 мин 18 часть

This commit is contained in:
ehermakov 2024-04-22 22:45:02 +03:00
parent d8ce3de627
commit 6322addc99
7 changed files with 38 additions and 8 deletions

View File

@ -25,9 +25,6 @@ def load (file):
# а - массив строк, в которых хранятся числа # а - массив строк, в которых хранятся числа
b = [] b = []
for x in a:
b.append(int(x))
# b - массив чисел из а
f.close() f.close()
return b return a

View File

@ -249,3 +249,9 @@ class ShipField:
print(ship_string[:-2]) print(ship_string[:-2])
# print(blocked_string[:-2] + ' ' + ship_string[:-2]) # print(blocked_string[:-2] + ' ' + ship_string[:-2])
print("********************************************************************") print("********************************************************************")
@staticmethod
def convert_to_json(obj):
if isinstance(obj, ShipField):
result = obj.__dict__
return result

View File

@ -0,0 +1,7 @@
{
"my field": {
"field": [],
"ships": [],
"field_mode": "PUT"
}
}

View File

@ -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

View File

@ -1,7 +1,7 @@
import json
from tkinter import * from tkinter import *
from ShipField import ShipField from ShipField import ShipField
my_field = ShipField() my_field = ShipField()
enemy_field = ShipField() enemy_field = ShipField()
@ -79,9 +79,14 @@ def button_enter(buttons, row, col):
colorize(enemy_field, enemy_buttons) 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 = Tk()
window.title("Ship Craft!") window.title("Ship Craft!")
window.geometry('940x410') window.geometry('940x510')
window.bind_all('<KeyPress>', keypress_handler) window.bind_all('<KeyPress>', keypress_handler)
my_field.toggle_ship_direction() 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 = Label(window, text='', width=5, height=2)
lbl.grid(column=10, row=0) lbl.grid(column=10, row=0)
savebutton = Button(window, text='Save', width=20, height=2)
savebutton.bind("<Button-1>", savebutton_click)
savebutton.grid(column=0, row=11, columnspan=4)
window.mainloop() window.mainloop()

View File

@ -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}}