загружаю код с добавлением расчета остатка кораблей и новыми ограничителями поля

This commit is contained in:
ehermakov 2024-05-19 18:51:21 +03:00
parent 1b1be42f5b
commit 925230782f
8 changed files with 126 additions and 52 deletions

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.11" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11" project-jdk-type="Python SDK" />
</project>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../../../.." vcs="Git" />
</component>
</project>

View File

@ -5,11 +5,9 @@
</component>
<component name="ChangeListManager">
<list default="true" id="b389cdea-22f5-4ba2-8b46-337091984b3c" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<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" />
@ -19,23 +17,49 @@
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/../../../.." />
</component>
<component name="ProjectColorInfo"><![CDATA[{
"associatedIndex": 0
}]]></component>
<component name="HighlightingSettingsPerFile">
<setting file="file://$PROJECT_DIR$/main.py" root0="SKIP_INSPECTION" />
</component>
<component name="ProjectColorInfo">{
&quot;associatedIndex&quot;: 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 name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;Python.ShipField.executor&quot;: &quot;Run&quot;,
&quot;Python.main.executor&quot;: &quot;Run&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;git-widget-placeholder&quot;: &quot;main&quot;,
&quot;last_opened_file_path&quot;: &quot;C:/Users/Eduardo/Documents/Программирование с Артуром/HNS/hnc-eduard/HNS/Excercises/ShipCraft/Переделка&quot;
}
}]]></component>
<component name="RunManager">
}</component>
<component name="RunManager" selected="Python.main">
<configuration name="ShipField" 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="$USER_HOME$/AppData/Local/Temp/Rar$DRa3732.15361/ShipBattle" />
<option name="IS_MODULE_SDK" value="false" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$USER_HOME$/AppData/Local/Temp/Rar$DRa3732.15361/ShipBattle/ShipField.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>
<configuration name="main" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="Переделка" />
<option name="ENV_FILES" value="" />
@ -61,6 +85,7 @@
<recent_temporary>
<list>
<item itemvalue="Python.main" />
<item itemvalue="Python.ShipField" />
</list>
</recent_temporary>
</component>
@ -82,4 +107,15 @@
</task>
<servers />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" suspend="THREAD" type="python-line">
<url>file://$PROJECT_DIR$/main.py</url>
<line>147</line>
<option name="timeStamp" value="1" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
</component>
</project>

View File

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

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

View File

@ -1,5 +1,6 @@
import json
import os
import time
from tkinter import *
from tkinter import filedialog
@ -9,14 +10,14 @@ my_field = ShipField()
enemy_field = ShipField()
active_field = my_field
active_text = {}
def draw_field(window, field, col_offset):
def draw_field(window, field, col_offset=0, row_offset=0):
buttons = []
for r in range(0, field.field_size):
for c in range(0, field.field_size):
btn = Button(window, text='', width=5, height=2)
btn.grid(column=c + col_offset, row=r)
btn.grid(column=c + col_offset, row=r + row_offset)
btn.bind('<Button-1>', lambda e, x=r, y=c: left_button_click(buttons, x, y))
btn.bind('<Button-3>', right_button_click)
btn.bind('<Enter>', lambda e, x=r, y=c: button_enter(buttons, x, y))
@ -54,29 +55,31 @@ def keypress_handler(e):
def left_button_click(buttons, row, col):
global active_field
global active_text
active_field.action(row, col)
refresh_remaining_ships_label()
refresh_remaining_ships_label(active_field, active_text)
colorize(active_field, buttons)
def right_button_click(unused):
global active_field
active_field.toggle_ship_direction()
def button_enter(buttons, row, col):
global active_field
global active_text
if buttons == my_buttons:
active_field = my_field
active_text = my_remainingShipsText
enemy_field.clear_marker()
my_field.target(row, col)
elif buttons == enemy_buttons:
active_field = enemy_field
my_field.clear_marker()
active_text = enemy_remainingShipsText
enemy_field.clear_marker()
enemy_field.target(row, col)
colorize(my_field, my_buttons)
@ -86,7 +89,7 @@ def button_enter(buttons, row, col):
def savebutton_click(field):
file_path = filedialog.asksaveasfilename(filetypes=[("JSON files", "*.json")])
if file_path:
with open("file_path", "w") as f:
with open(file_path, "w") as f:
json.dump({"shipField": field}, f, default=ShipField.convert_to_json)
@ -94,56 +97,70 @@ 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)['shipField'])
with open(file_path) as lines:
field.from_json(json.load(lines)['shipField'])
colorize(field, buttons)
def refresh_remaining_ships_label():
def refresh_remaining_ships_label(field, stringvar):
text = ''
for i in range(1,5):
count = my_field.ships.count(i)
count = field.ships.count(i)
if count > 0:
text += f'{"[]" * i}: {count}, '
remainingShipsText.set(text[:-2])
stringvar.set(text[:-2])
window = Tk()
window.title("Ship Craft!")
window.geometry('940x510')
window.geometry('1020x540')
window.bind_all('<KeyPress>', keypress_handler)
my_field.toggle_ship_direction()
my_field.set_ship_size(4)
my_field.set_ship(1, 1)
my_field.toggle_ship_direction()
my_field.set_ship_size(3)
my_field.set_ship(0, 6)
my_field.set_ship_size(1)
my_field.set_ship(7, 3)
my_remainingShipsText = StringVar()
enemy_remainingShipsText = StringVar()
my_buttons = draw_field(window, my_field, 0)
enemy_buttons = draw_field(window, enemy_field, 11)
start_column_my_field = 1
start_row_my_field = 1
lbl = Label(window, text='', width=5, height=2)
lbl.grid(column=10, row=0)
start_column_enemy_field = start_column_my_field + my_field.field_size + 1
start_row_enemy_field = start_row_my_field
col_vertical_separator = start_column_my_field + my_field.field_size
row_horizontal_separator = start_row_my_field + my_field.field_size
load_button_row = start_row_my_field + my_field.field_size + 1
my_buttons = draw_field(window, my_field, start_column_my_field, start_row_my_field)
enemy_buttons = draw_field(window, enemy_field, start_column_enemy_field, start_row_enemy_field)
if start_column_my_field > 0:
lbl_left_vertical = Label(window, text='', width=5, height=2)
lbl_left_vertical.grid(column=start_column_my_field - 1, row=start_row_my_field)
lbl_center_vertical = Label(window, text='', width=5, height=2)
lbl_center_vertical.grid(column=col_vertical_separator, row=start_row_my_field)
if start_row_my_field > 0:
lbl_upper_horizontal = Label(window, text='', width=5, height=2)
lbl_upper_horizontal.grid(column=start_column_my_field, row=start_row_my_field - 1)
lbl_lower_horizontal = Label(window, text='', width=50, height=2, textvariable=my_remainingShipsText)
lbl_lower_horizontal.grid(column=start_column_my_field, row=row_horizontal_separator, columnspan=10)
lbl_lower_enemy_horizontal = Label(window, text='', width=50, height=2, textvariable=enemy_remainingShipsText)
lbl_lower_enemy_horizontal.grid(column=start_column_enemy_field, row=row_horizontal_separator, columnspan=10)
savebutton = Button(window, text='Save', width=20, height=2, command=lambda: savebutton_click(my_field))
savebutton.grid(column=0, row=11, columnspan=4)
savebutton.grid(column=start_column_my_field, row=load_button_row, columnspan=4)
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)
loadbutton.grid(column=start_column_my_field + 6, row=load_button_row, 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)
savebutton_enemy = Button(window, text='Save', width=20, height=2, command=lambda: savebutton_click(enemy_field))
savebutton_enemy.grid(column=start_column_enemy_field, row=load_button_row, 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=1, row=13, columnspan=10)
loadbutton_enemy = Button(window, text='Load', width=20, height=2, command=lambda: loadbutton_click(enemy_field, enemy_buttons))
loadbutton_enemy.grid(column=start_column_enemy_field + 6, row=load_button_row, columnspan=4)
window.mainloop()