загружаю еще немного доработанное дз
This commit is contained in:
parent
785392ed92
commit
b0c04bf94c
|
@ -0,0 +1,3 @@
|
||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="PYTHON_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="jdk" jdkName="Python 3.11 (ShipCraft)" jdkType="Python SDK" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
|
@ -0,0 +1,6 @@
|
||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<settings>
|
||||||
|
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||||
|
<version value="1.0" />
|
||||||
|
</settings>
|
||||||
|
</component>
|
|
@ -0,0 +1,4 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (ShipCraft)" project-jdk-type="Python SDK" />
|
||||||
|
</project>
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/ShipCraft.iml" filepath="$PROJECT_DIR$/.idea/ShipCraft.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -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>
|
|
@ -5,6 +5,8 @@ from enum import Enum
|
||||||
buttons = []
|
buttons = []
|
||||||
|
|
||||||
field_size = 10
|
field_size = 10
|
||||||
|
ship_size = 4
|
||||||
|
ship_direction = 0
|
||||||
|
|
||||||
empty_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
empty_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||||||
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||||||
|
@ -77,6 +79,7 @@ def draw_field(window, field):
|
||||||
btn = Button(window, text='', bg=bg, width=5, height=2)
|
btn = Button(window, text='', bg=bg, width=5, height=2)
|
||||||
btn.grid(column=c, row=r)
|
btn.grid(column=c, row=r)
|
||||||
btn.bind('<Button-1>', lambda e, x=r, y=c: button_click(field, x, y))
|
btn.bind('<Button-1>', lambda e, x=r, y=c: button_click(field, x, y))
|
||||||
|
btn.bind('<Enter>', lambda e, x=r, y=c: button_enter(field, x, y))
|
||||||
buttons.append(btn)
|
buttons.append(btn)
|
||||||
|
|
||||||
|
|
||||||
|
@ -97,6 +100,50 @@ def button_click(field, row, col):
|
||||||
colorize(field, buttons)
|
colorize(field, buttons)
|
||||||
|
|
||||||
|
|
||||||
|
def button_enter(field, row, col):
|
||||||
|
if ship_direction == 0:
|
||||||
|
if field_size - row < ship_size:
|
||||||
|
print("Not OK")
|
||||||
|
return
|
||||||
|
if ship_direction == 1:
|
||||||
|
if field_size - col < ship_size:
|
||||||
|
print("Not OK")
|
||||||
|
return
|
||||||
|
print("OK")
|
||||||
|
|
||||||
|
|
||||||
|
def check_possible(field, row, col):
|
||||||
|
# Функция должна возвращать True, если можно поставить сюда корабль,
|
||||||
|
# в противном случае - False
|
||||||
|
|
||||||
|
if ship_direction == 0:
|
||||||
|
if field_size - row >= ship_size:
|
||||||
|
# Здесь мы знаем, что корабль помещается на поле.
|
||||||
|
# Теперь нужно проверить, не заблокировано ли какое-то из полей,
|
||||||
|
# на которые мы хотим поставить корабль.
|
||||||
|
# Для этого, всего поля, на которые мы его поставили бы исходя из координат row, col
|
||||||
|
# нужно проверить с помощью вызова метода check_blocked(...)
|
||||||
|
return True
|
||||||
|
|
||||||
|
if ship_direction == 1:
|
||||||
|
if field_size - col >= ship_size:
|
||||||
|
# Здесь мы знаем, что корабль помещается на поле.
|
||||||
|
# Теперь нужно проверить, не заблокировано ли какое-то из полей,
|
||||||
|
# на которые мы хотим поставить корабль.
|
||||||
|
# Для этого, всего поля, на которые мы его поставили бы исходя из координат row, col
|
||||||
|
# нужно проверить с помощью вызова метода check_blocked(...)
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def check_blocked(field, row, col):
|
||||||
|
# Функция возвращает True, если все клетки вокруг клетки с координатами row, col
|
||||||
|
# либо находятся за пределами поля, либо в них нет корабля/они пустые
|
||||||
|
# Во всех других случаях, функция возвращает False
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
window = Tk()
|
window = Tk()
|
||||||
window.title("Ship Craft!")
|
window.title("Ship Craft!")
|
||||||
window.geometry('450x410')
|
window.geometry('450x410')
|
||||||
|
|
Loading…
Reference in New Issue