2023-11-12 19:09:32 +01:00
|
|
|
|
from tkinter import *
|
|
|
|
|
from enum import Enum
|
2023-11-19 20:44:56 +01:00
|
|
|
|
import math
|
2023-11-12 19:09:32 +01:00
|
|
|
|
|
|
|
|
|
buttons = []
|
|
|
|
|
|
|
|
|
|
field_size = 10
|
2023-11-27 18:44:02 +01:00
|
|
|
|
ship_size = 4
|
2023-11-27 20:30:13 +01:00
|
|
|
|
ship_direction = 0
|
2023-12-10 15:08:22 +01:00
|
|
|
|
field_mode = 0
|
2023-11-12 19:09:32 +01:00
|
|
|
|
|
2023-11-20 18:13:03 +01:00
|
|
|
|
empty_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
2023-11-12 19:09:32 +01:00
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
|
|
|
|
|
|
|
|
|
|
my_field = list(empty_field)
|
|
|
|
|
enemy_field = list(empty_field)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 1. Определите перечисление (Enum) ShootResult со следующими значениями:
|
|
|
|
|
# EMPTY (мимо), DAMAGED (ранен), KILLED (убит), UNDEFINED (действие не определено)
|
|
|
|
|
|
|
|
|
|
class ShootResult(Enum):
|
|
|
|
|
EMPTY = "EMPTY"
|
|
|
|
|
DAMAGED = "DAMAGED"
|
|
|
|
|
KILLED = "KILLED"
|
|
|
|
|
UNDEFINED = "UNDEFINED"
|
|
|
|
|
|
|
|
|
|
|
2023-12-10 15:08:22 +01:00
|
|
|
|
def set_ship(row, col, ship_size, direction):
|
2023-11-27 18:44:02 +01:00
|
|
|
|
if row < 0 or row > field_size:
|
2023-11-12 19:09:32 +01:00
|
|
|
|
return
|
2023-11-27 18:44:02 +01:00
|
|
|
|
if col < 0 or col > field_size:
|
2023-11-12 19:09:32 +01:00
|
|
|
|
return
|
|
|
|
|
index = row * field_size + col
|
|
|
|
|
if direction == 0:
|
2023-12-10 15:08:22 +01:00
|
|
|
|
if field_size - row < ship_size:
|
2023-11-12 19:09:32 +01:00
|
|
|
|
return
|
2023-12-10 15:08:22 +01:00
|
|
|
|
for r in range(row, row + ship_size):
|
2023-11-12 19:09:32 +01:00
|
|
|
|
index = r * field_size + col
|
2023-11-16 19:41:24 +01:00
|
|
|
|
my_field[index] = "1"
|
2023-11-12 19:09:32 +01:00
|
|
|
|
if direction == 1:
|
2023-12-10 15:08:22 +01:00
|
|
|
|
if field_size - col < ship_size:
|
2023-11-12 19:09:32 +01:00
|
|
|
|
return
|
2023-12-10 15:08:22 +01:00
|
|
|
|
for c in range(col, col + ship_size):
|
2023-11-12 19:09:32 +01:00
|
|
|
|
index = row * field_size + c
|
|
|
|
|
my_field[index] = "1"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def shoot(field, row, col):
|
|
|
|
|
if row < 0 or row > field_size - 1:
|
2023-11-15 20:53:44 +01:00
|
|
|
|
return ShootResult.UNDEFINED
|
2023-11-12 19:09:32 +01:00
|
|
|
|
if col < 0 or col > field_size - 1:
|
2023-11-15 20:53:44 +01:00
|
|
|
|
return ShootResult.UNDEFINED
|
2023-11-12 19:09:32 +01:00
|
|
|
|
index = row * field_size + col
|
2023-11-15 20:53:44 +01:00
|
|
|
|
if (field[index]).strip() == "":
|
2023-11-16 19:41:24 +01:00
|
|
|
|
field[index] = "0"
|
2023-11-12 19:09:32 +01:00
|
|
|
|
return ShootResult.EMPTY
|
2023-11-15 20:53:44 +01:00
|
|
|
|
elif (field[index]).strip() == "1":
|
2023-11-12 19:09:32 +01:00
|
|
|
|
field[index] = "\\"
|
|
|
|
|
return ShootResult.DAMAGED
|
2023-11-15 20:53:44 +01:00
|
|
|
|
else:
|
|
|
|
|
return ShootResult.UNDEFINED
|
2023-11-12 19:09:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def draw_field(window, field):
|
|
|
|
|
for r in range(0, field_size):
|
|
|
|
|
for c in range(0, field_size):
|
|
|
|
|
index = r * field_size + c
|
2023-12-10 15:08:22 +01:00
|
|
|
|
btn = Button(window, text='', width=5, height=2)
|
2023-11-12 19:09:32 +01:00
|
|
|
|
btn.grid(column=c, row=r)
|
2023-12-10 15:08:22 +01:00
|
|
|
|
btn.bind('<Button-1>', lambda e, x=r, y=c: left_button_click(field, x, y))
|
2023-12-10 20:44:14 +01:00
|
|
|
|
btn.bind('<Button-3>', right_button_click)
|
2023-11-19 18:45:31 +01:00
|
|
|
|
btn.bind('<Enter>', lambda e, x=r, y=c: button_enter(field, x, y))
|
2023-11-12 19:09:32 +01:00
|
|
|
|
buttons.append(btn)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def colorize(field, buttons):
|
|
|
|
|
for i in range(len(field)):
|
2023-11-15 20:53:44 +01:00
|
|
|
|
bg = "white"
|
2023-11-16 19:41:24 +01:00
|
|
|
|
if field[i] == "1":
|
2023-11-12 19:09:32 +01:00
|
|
|
|
bg = 'pink'
|
2023-11-16 19:41:24 +01:00
|
|
|
|
if field[i] == "\\":
|
2023-11-12 19:09:32 +01:00
|
|
|
|
bg = 'red'
|
2023-11-16 19:41:24 +01:00
|
|
|
|
if field[i] == "0":
|
2023-11-12 19:09:32 +01:00
|
|
|
|
bg = 'black'
|
2023-12-10 15:08:22 +01:00
|
|
|
|
if field[i] == "p":
|
|
|
|
|
bg = 'blue'
|
2023-11-15 20:53:44 +01:00
|
|
|
|
buttons[i].configure(bg=bg)
|
|
|
|
|
|
|
|
|
|
|
2023-12-10 21:01:46 +01:00
|
|
|
|
def keypress_handler(e):
|
2023-12-11 21:01:55 +01:00
|
|
|
|
global ship_size
|
|
|
|
|
if 1 <= int(e.keysym) <= 4:
|
|
|
|
|
ship_size = int(e.keysym)
|
|
|
|
|
return ship_size
|
2023-12-12 21:31:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def keypress_handler_2(f):
|
|
|
|
|
global field_mode
|
|
|
|
|
if f.keysym == 'm':
|
2023-12-11 21:01:55 +01:00
|
|
|
|
if field_mode == 0:
|
|
|
|
|
field_mode = 1
|
|
|
|
|
else:
|
|
|
|
|
field_mode = 0
|
|
|
|
|
return field_mode
|
2023-12-10 21:01:46 +01:00
|
|
|
|
|
|
|
|
|
|
2023-12-10 15:08:22 +01:00
|
|
|
|
def left_button_click(field, row, col):
|
|
|
|
|
if field_mode == 0:
|
|
|
|
|
if check_possible(field, row, col):
|
2023-12-11 21:01:55 +01:00
|
|
|
|
set_ship(row, col, ship_size, ship_direction)
|
2023-12-10 15:08:22 +01:00
|
|
|
|
elif field_mode == 1:
|
|
|
|
|
shoot(field, row, col)
|
2023-11-15 20:53:44 +01:00
|
|
|
|
colorize(field, buttons)
|
2023-11-12 19:09:32 +01:00
|
|
|
|
|
|
|
|
|
|
2023-12-10 20:44:14 +01:00
|
|
|
|
def right_button_click(d):
|
|
|
|
|
global ship_direction
|
|
|
|
|
if field_mode == 0:
|
|
|
|
|
if ship_direction == 0:
|
|
|
|
|
ship_direction = 1
|
|
|
|
|
else:
|
|
|
|
|
ship_direction = 0
|
2023-12-10 21:01:46 +01:00
|
|
|
|
|
2023-12-10 15:08:22 +01:00
|
|
|
|
|
2023-11-19 18:45:31 +01:00
|
|
|
|
def button_enter(field, row, col):
|
2023-12-10 15:08:22 +01:00
|
|
|
|
if field_mode == 0:
|
|
|
|
|
for i in range(0, len(field)):
|
|
|
|
|
if field[i] == "p":
|
|
|
|
|
field[i] = ''
|
2023-11-19 18:45:31 +01:00
|
|
|
|
|
2023-12-10 15:08:22 +01:00
|
|
|
|
if check_possible(field, row, col):
|
|
|
|
|
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)
|
2023-11-19 18:45:31 +01:00
|
|
|
|
|
2023-12-11 21:01:55 +01:00
|
|
|
|
|
2023-11-19 18:45:31 +01:00
|
|
|
|
def check_possible(field, row, col):
|
|
|
|
|
# Функция должна возвращать True, если можно поставить сюда корабль,
|
|
|
|
|
# в противном случае - False
|
|
|
|
|
|
|
|
|
|
if ship_direction == 0:
|
2023-11-26 19:17:49 +01:00
|
|
|
|
# Здесь мы знаем, что корабль помещается на поле.
|
2023-11-19 18:45:31 +01:00
|
|
|
|
if field_size - row >= ship_size:
|
|
|
|
|
# Теперь нужно проверить, не заблокировано ли какое-то из полей,
|
2023-12-03 21:44:54 +01:00
|
|
|
|
for r in range(row, row + ship_size):
|
|
|
|
|
if not check_blocked(field, r, col):
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
2023-11-19 18:45:31 +01:00
|
|
|
|
if ship_direction == 1:
|
|
|
|
|
if field_size - col >= ship_size:
|
2023-12-03 21:44:54 +01:00
|
|
|
|
for c in range(col, col + ship_size):
|
|
|
|
|
if not check_blocked(field, row, c):
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
2023-11-20 18:13:03 +01:00
|
|
|
|
return False
|
2023-11-19 18:45:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_blocked(field, row, col):
|
|
|
|
|
# Функция возвращает True, если все клетки вокруг клетки с координатами row, col
|
|
|
|
|
# либо находятся за пределами поля, либо в них нет корабля/они пустые
|
2023-11-26 19:17:49 +01:00
|
|
|
|
for r in range(row - 1, row + 2):
|
|
|
|
|
for c in range(col - 1, col + 2):
|
|
|
|
|
if 0 < r < field_size and 0 < c < field_size:
|
2023-12-10 15:08:22 +01:00
|
|
|
|
cell = (field[r * field_size + c]).strip()
|
|
|
|
|
if cell != '' and cell != 'p':
|
2023-11-26 19:17:49 +01:00
|
|
|
|
return False
|
|
|
|
|
return True
|
2023-11-19 18:45:31 +01:00
|
|
|
|
|
|
|
|
|
|
2023-11-12 19:09:32 +01:00
|
|
|
|
window = Tk()
|
|
|
|
|
window.title("Ship Craft!")
|
|
|
|
|
window.geometry('450x410')
|
2023-12-11 21:01:55 +01:00
|
|
|
|
window.bind_all('<KeyPress>', keypress_handler)
|
2023-12-12 21:31:31 +01:00
|
|
|
|
window.bind_all('<KeyPress>', keypress_handler_2)
|
2023-11-26 19:17:49 +01:00
|
|
|
|
set_ship(1, 1, 4, 1)
|
|
|
|
|
set_ship(0, 6, 3, 0)
|
|
|
|
|
set_ship(7, 3, 1, 0)
|
|
|
|
|
draw_field(window, my_field)
|
2023-12-10 15:08:22 +01:00
|
|
|
|
window.mainloop()
|
2023-11-26 19:17:49 +01:00
|
|
|
|
|
|
|
|
|
for r in range(0, field_size):
|
|
|
|
|
blocked_string = ""
|
|
|
|
|
ship_string = ""
|
|
|
|
|
for c in range(0, field_size):
|
|
|
|
|
blocked_string += str(check_blocked(my_field, r, c))[0] + ", "
|
|
|
|
|
ship_string += my_field[r * field_size + c] + ', '
|
|
|
|
|
print(blocked_string[:-2] + ' ' + ship_string[:-2])
|
|
|
|
|
print("********************************************************************")
|
|
|
|
|
|
|
|
|
|
for r in range(0, field_size):
|
|
|
|
|
possible_string = ""
|
|
|
|
|
impossible_string = ""
|
|
|
|
|
for c in range(0, field_size):
|
|
|
|
|
possible_string += str(check_possible(my_field, r, c))[0] + ", "
|
|
|
|
|
impossible_string += my_field[r * field_size + c] + ', '
|
2023-12-10 15:08:22 +01:00
|
|
|
|
print(possible_string[:-2] + ' ' + impossible_string[:-2])
|