hnc-daniil/HNC/Exercises/Ship_Battle/main.py

312 lines
9.4 KiB
Python
Raw Normal View History

2023-11-06 15:35:22 +01:00
from tkinter import *
2023-11-13 10:50:09 +01:00
from enum import Enum
2024-02-26 18:34:51 +01:00
my_buttons = []
enemy_buttons = []
2023-11-13 10:50:09 +01:00
field_size = 10
2024-02-26 18:34:51 +01:00
active_field = 0
ship_size_left = 4
ship_direction_left = 0
field_mode_left = 0
2023-11-13 10:50:09 +01:00
2024-02-26 18:34:51 +01:00
ship_size_right = 4
ship_direction_right = 0
field_mode_right = 0
2023-11-06 15:35:22 +01:00
empty_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
my_field = list(empty_field)
enemy_field = list(empty_field)
2024-02-26 18:34:51 +01:00
# 1. Определите перечисление (Enum) ShootResult со следующими значениями:
# EMPTY (мимо), DAMAGED (ранен), KILLED (убит), UNDEFINED (действие не определено)
2023-11-13 10:50:09 +01:00
class ShootResult(Enum):
2024-02-26 18:34:51 +01:00
EMPTY = "EMPTY"
DAMAGED = "DAMAGED"
KILLED = "KILLED"
UNDEFINED = "UNDEFINED"
2023-11-06 15:35:22 +01:00
2023-11-06 19:54:00 +01:00
2024-02-26 18:34:51 +01:00
def set_ship(field, row, col, ship_size, direction):
if row < 0 or row > field_size:
return
if col < 0 or col > field_size:
2023-11-06 19:54:00 +01:00
return
2023-11-20 19:05:48 +01:00
index = row * field_size + col
2023-11-06 19:54:00 +01:00
if direction == 0:
2024-02-26 18:34:51 +01:00
if field_size - row < ship_size:
2023-11-06 19:54:00 +01:00
return
2024-02-26 18:34:51 +01:00
for r in range(row, row + ship_size):
2023-11-06 19:54:00 +01:00
index = r * field_size + col
2024-02-26 18:34:51 +01:00
field[index] = "1"
2023-11-06 19:54:00 +01:00
if direction == 1:
2024-02-26 18:34:51 +01:00
if field_size - col < ship_size:
2023-11-06 19:54:00 +01:00
return
2024-02-26 18:34:51 +01:00
for c in range(col, col + ship_size):
2023-11-06 19:54:00 +01:00
index = row * field_size + c
2024-02-26 18:34:51 +01:00
field[index] = "1"
2023-11-06 15:35:22 +01:00
2023-11-13 10:50:09 +01:00
def shoot(field, row, col):
if row < 0 or row > field_size - 1:
2024-02-26 18:34:51 +01:00
return ShootResult.UNDEFINED
2023-11-13 10:50:09 +01:00
if col < 0 or col > field_size - 1:
2024-02-26 18:34:51 +01:00
return ShootResult.UNDEFINED
2023-11-20 19:05:48 +01:00
index = row * field_size + col
if (field[index]).strip() == "":
field[index] = "0"
2024-02-26 18:34:51 +01:00
return ShootResult.EMPTY
elif (field[index]).strip() == "1":
2023-11-13 10:50:09 +01:00
field[index] = "\\"
2024-02-26 18:34:51 +01:00
return ShootResult.DAMAGED
2023-11-20 19:05:48 +01:00
else:
2024-02-26 18:34:51 +01:00
return ShootResult.UNDEFINED
2023-11-06 15:35:22 +01:00
2024-02-26 18:34:51 +01:00
def draw_field(window, field, col_offset):
buttons = []
2023-11-13 10:50:09 +01:00
for r in range(0, field_size):
for c in range(0, field_size):
2023-11-20 19:05:48 +01:00
index = r * field_size + c
2023-12-11 17:16:12 +01:00
btn = Button(window, text='', width=5, height=2)
2024-02-26 18:34:51 +01:00
btn.grid(column=c + col_offset, row=r)
btn.bind('<Button-1>', lambda e, x=r, y=c: left_button_click(field, buttons, x, y))
btn.bind('<Button-3>', right_button_click)
btn.bind('<Enter>', lambda e, x=r, y=c: button_enter(field, buttons, x, y))
2023-11-13 10:50:09 +01:00
buttons.append(btn)
2024-02-26 18:34:51 +01:00
colorize(field, buttons)
return buttons
2023-11-13 10:50:09 +01:00
2023-11-20 19:36:57 +01:00
2023-11-13 10:50:09 +01:00
def colorize(field, buttons):
for i in range(len(field)):
2023-11-20 19:05:48 +01:00
bg = "white"
2024-02-26 18:34:51 +01:00
if field[i] == "1":
2023-11-13 10:50:09 +01:00
bg = 'pink'
2024-02-26 18:34:51 +01:00
if field[i] == "\\":
2023-11-13 10:50:09 +01:00
bg = 'red'
2024-02-26 18:34:51 +01:00
if field[i] == "0":
2023-11-13 10:50:09 +01:00
bg = 'black'
2024-02-26 18:34:51 +01:00
if field[i] == "p":
2023-12-11 17:16:12 +01:00
bg = 'blue'
2023-11-20 19:05:48 +01:00
buttons[i].configure(bg=bg)
2024-02-26 18:34:51 +01:00
def keypress_handler(e):
global ship_size_left
global field_mode_left
global ship_size_right
global field_mode_right
if e.keysym.isnumeric():
number = int(e.keysym)
if 1 <= number <= 4:
if active_field == 0:
ship_size_left = number
else:
ship_size_right = number
else:
if e.keysym == 'm':
if active_field == 0:
field_mode = field_mode_left
else:
field_mode = field_mode_right
if field_mode == 0:
field_mode = 1
else:
field_mode = 0
if active_field == 0:
field_mode_left = field_mode
else:
field_mode_right = field_mode
def left_button_click(field, buttons, row, col):
global ship_size_left
global ship_direction_left
global ship_size_right
global ship_direction_right
if active_field == 0:
field_mode = field_mode_left
ship_size = ship_size_left
ship_direction = ship_direction_left
else:
field_mode = field_mode_right
ship_size = ship_size_right
ship_direction = ship_direction_right
if field_mode == 0:
if check_possible(field, row, col, ship_size, ship_direction):
set_ship(field, row, col, ship_size, ship_direction)
elif field_mode == 1:
shoot(field, row, col)
colorize(field, buttons)
def right_button_click(d):
global ship_direction_left
global ship_direction_right
global field_mode_left
global field_mode_right
if active_field == 0:
field_mode = field_mode_left
ship_direction = ship_direction_left
else:
field_mode = field_mode_right
ship_direction = ship_direction_right
if field_mode == 0:
if ship_direction == 0:
ship_direction = 1
else:
ship_direction = 0
if active_field == 0:
ship_direction_left = ship_direction
else:
ship_direction_right = ship_direction
def button_enter(field, buttons, row, col):
global active_field
global ship_direction_left
global ship_direction_right
global ship_size_left
global ship_size_right
global field_mode_left
global field_mode_right
if buttons == my_buttons:
active_field = 0
elif buttons == enemy_buttons:
active_field = 1
if active_field == 0:
field_mode = field_mode_left
ship_direction = ship_direction_left
ship_size = ship_size_left
other_field = enemy_field
other_buttons = enemy_buttons
else:
field_mode = field_mode_right
ship_direction = ship_direction_right
ship_size = ship_size_right
other_field = my_field
other_buttons = my_buttons
for i in range(0, len(other_field)):
if other_field[i] == "p":
other_field[i] = ''
colorize(other_field, other_buttons)
if field_mode == 0:
for i in range(0, len(field)):
if field[i] == "p":
field[i] = ''
if check_possible(field, row, col,ship_size, ship_direction):
if ship_direction == 0:
for r in range(row, row + ship_size):
field[r * field_size + col] = "p"
if ship_direction == 1:
for c in range(col, col + ship_size):
field[row * field_size + c] = "p"
colorize(field, buttons)
def check_possible(field, row, col, ship_size, ship_direction):
# Функция должна возвращать True, если можно поставить сюда корабль,
# в противном случае - False
2023-11-20 19:36:57 +01:00
if ship_direction == 0:
2024-02-26 18:34:51 +01:00
# Здесь мы знаем, что корабль помещается на поле.
2023-11-20 19:36:57 +01:00
if field_size - row >= ship_size:
2024-02-26 18:34:51 +01:00
# Теперь нужно проверить, не заблокировано ли какое-то из полей,
2023-12-11 17:16:12 +01:00
for r in range(row, row + ship_size):
2023-11-27 19:33:48 +01:00
if not check_blocked(field, r, col):
return False
return True
2023-11-20 19:36:57 +01:00
if ship_direction == 1:
if field_size - col >= ship_size:
2023-12-11 17:16:12 +01:00
for c in range(col, col + ship_size):
2023-11-27 19:33:48 +01:00
if not check_blocked(field, row, c):
return False
return True
2023-11-20 19:36:57 +01:00
return False
def check_blocked(field, row, col):
2024-02-26 18:34:51 +01:00
# Функция возвращает True, если все клетки вокруг клетки с координатами row, col
# либо находятся за пределами поля, либо в них нет корабля/они пустые
2023-11-27 11:59:47 +01:00
for r in range(row - 1, row + 2):
for c in range(col - 1, col + 2):
2024-02-26 18:34:51 +01:00
if 0 <= r < field_size and 0 <= c < field_size:
2023-12-11 17:16:12 +01:00
cell = (field[r * field_size + c]).strip()
2024-02-26 18:34:51 +01:00
if cell != '' and cell != 'p':
2023-11-27 11:59:47 +01:00
return False
return True
2023-11-20 19:36:57 +01:00
2023-11-06 15:35:22 +01:00
window = Tk()
window.title("Ship Craft!")
2024-02-26 18:34:51 +01:00
window.geometry('940x410')
2023-12-11 20:01:56 +01:00
window.bind_all('<KeyPress>', keypress_handler)
2024-02-26 18:34:51 +01:00
set_ship(my_field, 1, 1, 4, 1)
set_ship(my_field,0, 6, 3, 0)
set_ship(my_field, 7, 3, 1, 0)
my_buttons = draw_field(window, my_field, 0)
enemy_buttons = draw_field(window, enemy_field, 11)
print(len(my_buttons))
print(len(enemy_buttons))
2023-11-06 15:35:22 +01:00
2024-02-26 18:34:51 +01:00
lbl = Label(window, text='', width=5, height=2)
lbl.grid(column=10, row=0)
2023-11-06 15:35:22 +01:00
2023-12-11 17:16:12 +01:00
window.mainloop()
2023-11-27 11:59:47 +01:00
2024-02-26 18:34:51 +01:00
2023-12-11 17:16:12 +01:00
# for r in range(0, field_size):
2024-02-26 18:34:51 +01:00
# blocked_string = ""
# ship_string = ""
2023-12-11 17:16:12 +01:00
# for c in range(0, field_size):
2024-02-26 18:34:51 +01:00
# blocked_string += str(check_blocked(my_field, r, c))[0] + ", "
2023-12-11 17:16:12 +01:00
# ship_string += my_field[r * field_size + c] + ', '
2024-02-26 18:34:51 +01:00
# print(blocked_string[:-2] + ' ' + ship_string[:-2])
# print("********************************************************************")
2023-11-27 18:48:30 +01:00
2023-12-11 17:16:12 +01:00
# for r in range(0, field_size):
2024-02-26 18:34:51 +01:00
# possible_string = ""
# impossible_string = ""
2023-12-11 17:16:12 +01:00
# for c in range(0, field_size):
2024-02-26 18:34:51 +01:00
# possible_string += str(check_possible(my_field, r, c))[0] + ", "
2023-12-11 17:16:12 +01:00
# impossible_string += my_field[r * field_size + c] + ', '
2024-02-26 18:34:51 +01:00
# print(possible_string[:-2] + ' ' + impossible_string[:-2])