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

165 lines
5.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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