добавлена загрузка вражеског поля, но не работает сохранение в файл

This commit is contained in:
ehermakov 2024-05-12 20:43:35 +03:00
parent af7f915ae2
commit 1b1be42f5b
8 changed files with 102 additions and 30 deletions

View File

@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="b389cdea-22f5-4ba2-8b46-337091984b3c" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/file_path" beforeDir="false" afterPath="$PROJECT_DIR$/file_path" afterDir="false" />
<change beforePath="$PROJECT_DIR$/game1.json" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/game1.txt" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/main.py" beforeDir="false" afterPath="$PROJECT_DIR$/main.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/test.json" beforeDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/../../../.." />
</component>
<component name="ProjectColorInfo"><![CDATA[{
"associatedIndex": 0
}]]></component>
<component name="ProjectId" id="2gNOqqOLo3TzX4nzxb9EIDfRt8A" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"Python.main.executor": "Run",
"RunOnceActivity.ShowReadmeOnStart": "true",
"git-widget-placeholder": "main",
"last_opened_file_path": "C:/Users/Eduardo/Documents/Программирование с Артуром/HNS/hnc-eduard/HNS/Excercises/ShipCraft/Переделка"
}
}]]></component>
<component name="RunManager">
<configuration name="main" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="Переделка" />
<option name="ENV_FILES" value="" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/main.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<recent_temporary>
<list>
<item itemvalue="Python.main" />
</list>
</recent_temporary>
</component>
<component name="SharedIndexes">
<attachedChunks>
<set>
<option value="bundled-python-sdk-babbdf50b680-746f403e7f0c-com.jetbrains.pycharm.community.sharedIndexes.bundled-PC-241.15989.155" />
</set>
</attachedChunks>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="b389cdea-22f5-4ba2-8b46-337091984b3c" name="Changes" comment="" />
<created>1715534761567</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1715534761567</updated>
</task>
<servers />
</component>
</project>

View File

@ -1 +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"}}
{"shipField": {"field": [" ", " ", " ", " ", " ", " ", "1", " ", " ", " ", " ", "1", "1", "1", "1", " ", "1", " ", " ", " ", " ", " ", " ", " ", " ", " ", "1", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "1", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "p", ""], "ships": [3, 2, 2, 2, 1, 1, 1], "field_size": 10, "field_mode": "PUT", "ship_size": 1, "ship_direction": "VERTICAL"}}

View File

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

View File

@ -1,10 +0,0 @@
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

@ -83,22 +83,21 @@ def button_enter(buttons, row, col):
colorize(enemy_field, enemy_buttons)
def savebutton_click():
file_path = filedialog.asksaveasfilename(filetypes=[('JSON files', '*.json')])
def savebutton_click(field):
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)
with open("file_path", "w") as f:
json.dump({"shipField": field}, f, default=ShipField.convert_to_json)
def loadbutton_click():
global my_field
def loadbutton_click(field, buttons):
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'])
my_field.from_json(json.load(lines)['shipField'])
colorize(my_field, my_buttons)
colorize(field, buttons)
def refresh_remaining_ships_label():
@ -131,14 +130,20 @@ enemy_buttons = draw_field(window, enemy_field, 11)
lbl = Label(window, text='', width=5, height=2)
lbl.grid(column=10, row=0)
savebutton = Button(window, text='Save', width=20, height=2, command=savebutton_click)
savebutton = Button(window, text='Save', width=20, height=2, command=lambda: savebutton_click(my_field))
savebutton.grid(column=0, row=11, columnspan=4)
loadbutton = Button(window, text='Load', width=20, height=2, command=loadbutton_click)
loadbutton = Button(window, text='Load', width=20, height=2, command=lambda: loadbutton_click(my_field, my_buttons))
loadbutton.grid(column=5, row=11, columnspan=4)
savebutton_enemy = Button(window, text='Save_enemy', width=20, height=2, command=lambda: savebutton_click(enemy_field))
savebutton_enemy.grid(column=11, row=11, columnspan=4)
loadbutton_enemy = Button(window, text='Load_enemy', width=20, height=2, command=lambda: loadbutton_click(enemy_field, enemy_buttons))
loadbutton_enemy.grid(column=16, row=11, columnspan=4)
remainingShipsText = StringVar()
lbl = Label(window, width=50, height=2, textvariable=remainingShipsText)
lbl.grid(column=11, row=11, columnspan=10)
lbl.grid(column=1, row=13, columnspan=10)
window.mainloop()

View File

@ -1 +0,0 @@
{"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"}}