2023-11-06 15:35:22 +01:00
|
|
|
|
from tkinter import *
|
2023-11-13 10:50:09 +01:00
|
|
|
|
from enum import Enum
|
|
|
|
|
|
|
|
|
|
field_size = 10
|
2023-11-27 19:33:48 +01:00
|
|
|
|
ship_size = 4
|
|
|
|
|
ship_direction = 1
|
2023-11-13 10:50:09 +01:00
|
|
|
|
|
|
|
|
|
buttons = []
|
2023-11-06 15:35:22 +01:00
|
|
|
|
|
|
|
|
|
empty_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
|
|
|
|
|
|
|
|
|
|
my_field = list(empty_field)
|
|
|
|
|
enemy_field = list(empty_field)
|
|
|
|
|
|
|
|
|
|
|
2023-11-13 10:50:09 +01:00
|
|
|
|
class ShootResult(Enum):
|
|
|
|
|
Empty = "EMPTY"
|
|
|
|
|
Damaged = "DAMAGED"
|
|
|
|
|
Killed = "KILLED"
|
|
|
|
|
Undefined = "UNDEFINED"
|
2023-11-06 15:35:22 +01:00
|
|
|
|
|
2023-11-06 19:54:00 +01:00
|
|
|
|
|
2023-11-13 10:50:09 +01:00
|
|
|
|
def set_ship(row, col, size, direction):
|
|
|
|
|
if row < 0 or row > field_size - 1:
|
|
|
|
|
return
|
|
|
|
|
if col < 0 or col > field_size - 1:
|
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:
|
|
|
|
|
if field_size - row < size:
|
|
|
|
|
return
|
|
|
|
|
for r in range(row, row + size):
|
|
|
|
|
index = r * field_size + col
|
2023-11-20 19:05:48 +01:00
|
|
|
|
my_field[index] = "1"
|
2023-11-06 19:54:00 +01:00
|
|
|
|
if direction == 1:
|
|
|
|
|
if field_size - col < size:
|
|
|
|
|
return
|
|
|
|
|
for c in range (col, col + size):
|
|
|
|
|
index = row * field_size + c
|
2023-11-13 10:50:09 +01:00
|
|
|
|
my_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:
|
2023-11-20 19:05:48 +01:00
|
|
|
|
return ShootResult.Undefined
|
|
|
|
|
|
2023-11-13 10:50:09 +01:00
|
|
|
|
if col < 0 or col > field_size - 1:
|
2023-11-20 19:05:48 +01:00
|
|
|
|
return ShootResult.Undefined
|
|
|
|
|
|
|
|
|
|
index = row * field_size + col
|
|
|
|
|
if (field[index]).strip() == "":
|
|
|
|
|
field[index] = "0"
|
2023-11-13 10:50:09 +01:00
|
|
|
|
return ShootResult.Empty
|
|
|
|
|
elif field[index] == "1":
|
|
|
|
|
field[index] = "\\"
|
|
|
|
|
return ShootResult.Damaged
|
|
|
|
|
elif field[index] == "1" or field[index] == "\\" or field[index] == "X":
|
2023-11-20 19:05:48 +01:00
|
|
|
|
return ShootResult.Undefined
|
|
|
|
|
else:
|
|
|
|
|
return ShootResult.Undefined
|
|
|
|
|
|
2023-11-06 15:35:22 +01:00
|
|
|
|
|
|
|
|
|
def draw_field(window, field):
|
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-11-13 10:50:09 +01:00
|
|
|
|
bg = 'white'
|
|
|
|
|
if field[index] == '1':
|
|
|
|
|
bg = 'pink'
|
2023-11-20 19:05:48 +01:00
|
|
|
|
btn = Button(window, text='', bg=bg, width=5, height=2)
|
2023-11-13 10:50:09 +01:00
|
|
|
|
btn.grid(column=c, row=r)
|
2023-11-20 19:05:48 +01:00
|
|
|
|
btn.bind('<Button-1>', lambda e, x=r, y=c: button_click(field, x, y))
|
2023-11-13 10:50:09 +01:00
|
|
|
|
buttons.append(btn)
|
|
|
|
|
|
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"
|
|
|
|
|
if field[i] == '1':
|
2023-11-13 10:50:09 +01:00
|
|
|
|
bg = 'pink'
|
2023-11-20 19:05:48 +01:00
|
|
|
|
if field[i] == '\\':
|
2023-11-13 10:50:09 +01:00
|
|
|
|
bg = 'red'
|
2023-11-20 19:05:48 +01:00
|
|
|
|
if field[i] == '0':
|
2023-11-13 10:50:09 +01:00
|
|
|
|
bg = 'black'
|
2023-11-20 19:05:48 +01:00
|
|
|
|
buttons[i].configure(bg=bg)
|
|
|
|
|
|
|
|
|
|
|
2023-11-20 19:36:57 +01:00
|
|
|
|
def check_possible(field, row, col):
|
|
|
|
|
# Функция должна возвращать True, если можно поставить сюда корабль,
|
|
|
|
|
# в противном случае - False
|
|
|
|
|
|
|
|
|
|
if ship_direction == 0:
|
|
|
|
|
if field_size - row >= ship_size:
|
2023-11-27 19:33:48 +01:00
|
|
|
|
for r in range(row, row + ship_size + 1):
|
|
|
|
|
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-11-27 19:33:48 +01:00
|
|
|
|
for c in range(col, col + ship_size + 1):
|
|
|
|
|
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):
|
|
|
|
|
# Функция возвращает True, если все клетки вокруг клетки с координатами row, col
|
|
|
|
|
# либо находятся за пределами поля, либо в них нет корабля/они пустые
|
|
|
|
|
# Во всех других случаях, функция возвращает False
|
2023-11-27 11:59:47 +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:
|
|
|
|
|
if (field[r * field_size + c]).strip() != '':
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
return True
|
2023-11-20 19:36:57 +01:00
|
|
|
|
|
|
|
|
|
|
2023-11-20 19:05:48 +01:00
|
|
|
|
def button_click(field, row, col):
|
|
|
|
|
shoot(field, row, col)
|
|
|
|
|
colorize(field, buttons)
|
2023-11-20 19:36:57 +01:00
|
|
|
|
|
2023-11-06 15:35:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
window = Tk()
|
|
|
|
|
window.title("Ship Craft!")
|
2023-11-13 10:50:09 +01:00
|
|
|
|
window.geometry('450x410')
|
2023-11-06 15:35:22 +01:00
|
|
|
|
|
2023-11-06 19:54:00 +01:00
|
|
|
|
set_ship(1, 1, 4, 1)
|
2023-11-13 10:50:09 +01:00
|
|
|
|
set_ship(0, 6, 3, 0)
|
|
|
|
|
set_ship(7, 3, 1, 0)
|
2023-11-27 11:59:47 +01:00
|
|
|
|
set_ship(9, 9, 1, 0)
|
|
|
|
|
set_ship(5, 8, 3, 0)
|
2023-11-06 19:54:00 +01:00
|
|
|
|
|
2023-11-06 15:35:22 +01:00
|
|
|
|
draw_field(window, my_field)
|
2023-11-20 19:05:48 +01:00
|
|
|
|
colorize(my_field, buttons)
|
2023-11-06 15:35:22 +01:00
|
|
|
|
|
2023-11-27 18:48:30 +01:00
|
|
|
|
#window.mainloop()
|
2023-11-27 11:59:47 +01:00
|
|
|
|
|
2023-11-27 19:33:48 +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("#########################################################################")
|
2023-11-27 18:48:30 +01:00
|
|
|
|
|
|
|
|
|
|
2023-11-27 19:33:48 +01:00
|
|
|
|
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-11-27 18:48:30 +01:00
|
|
|
|
|
2023-11-27 19:33:48 +01:00
|
|
|
|
print(possible_string[:-2] + ' ' + impossible_string[:-2])
|