Add some features

This commit is contained in:
Artur Savitskiy 2023-11-27 19:16:08 +01:00
parent 45ec86040b
commit 151a461de0
4 changed files with 195 additions and 39 deletions

7
HNS/MB/FieldMode.py Normal file
View File

@ -0,0 +1,7 @@
from enum import Enum
class FieldMode(Enum):
Placing = 0
Friendly = 1
Enemy = 2

8
HNS/MB/ShootResult.py Normal file
View File

@ -0,0 +1,8 @@
from enum import Enum
class ShootResult(Enum):
Empty = 0
Damaged = 1
Killed = 2
Undefined = -1

View File

@ -1,7 +1,11 @@
from tkinter import *
from ShootResult import ShootResult
from FieldMode import FieldMode
field_size = 10
mode = 0
mode = FieldMode.Placing
ship_size = 4
ship_direction = 0
empty_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
@ -21,7 +25,100 @@ enemy_field = list(empty_field)
buttons = []
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
value = (field[index]).strip()
if value == '':
field[index] = '0'
return ShootResult.Empty
elif value == '1':
field[index] = '\\'
return ShootResult.Damaged
return ShootResult.Undefined
def set_ship(field, row, col, size, direction):
# 0. Проверка стартовой точки (находится ли в поле)
if row < 0 or row > field_size - 1:
return
if col < 0 or col > field_size - 1:
return
# 1. Нахождение стартовой точки
index = row * field_size + col
# 2. Определяем направление и в зависимости от него
# 2.1 Если вниз - то сравниваем размер поля (field_size=10) - row и size (field_size - row > size или row + size < field_size)
if direction == 0:
if field_size - row < size:
# 3. Если не помещается, то корабль не ставим
return
# 4. А если помещается - ставим корабль, т.е. (row,col) до (row+size,col) заполняем единичками
for r in range(row, row + size):
index = r * field_size + col
field[index] = '1'
# 2.2 Если вправо - то сравниваем field_size - col и size (field_size - col > size или col + size < field_size)
if direction == 1:
if field_size - col < size:
# 3. Если не помещается, то корабль не ставим
return
# 4. А если помещается - ставим корабль, т.е. (row,col) до (row,col+size) заполняем единичками
for c in range(col, col + size):
index = row * field_size + c
field[index] = '1'
def draw_field(window, field):
for r in range(0, field_size):
for c in range(0, field_size):
btn = Button(window, text="", bg='white', width=5, height=2)
btn.grid(column=c, row=r)
btn.bind('<Enter>', lambda e, row=r, col=c: button_hover(my_field, row, col))
btn.bind('<Button-1>', lambda e, x=r, y=c: button_action(my_field, x, y))
btn.bind('<Button-3>', rotate_ship)
buttons.append(btn)
def rotate_ship(e):
global ship_direction
if ship_direction == 0:
ship_direction = 1
else:
ship_direction = 0
def button_hover(field, row, col):
clear_placing(field)
place_ship(field, row, col, ship_size, ship_direction)
colorize(field)
def clear_placing(field):
for r in range(0, field_size):
for c in range(0, field_size):
index = r*field_size+c
if field[index] == 'p':
field[index] = ''
def place_ship(field, row, col, size, direction):
blocked = False
if row < 0 or row > field_size - 1:
return
@ -29,54 +126,98 @@ def set_ship(field, row, col, size, direction):
return
if direction == 0:
if row + size > field_size:
if field_size - row < size:
return
for r in range(row, row + size):
index = r * field_size + col
field[index] = '1'
if check_blocked(field, r, col):
blocked = True
break
if not blocked:
for r in range(row, row + size):
field[r * field_size + col] = 'p'
if direction == 1:
if col + size > field_size - 1:
if field_size - col < size:
return
for c in range(col, col + size):
index = row * field_size + c
field[index] = '1'
if check_blocked(field, row, c):
blocked = True
break
if not blocked:
for c in range(col, col + size):
field[row * field_size + c] = 'p'
def draw_field(window, field):
for row in range(0, field_size):
for col in range(0, field_size):
index = row * field_size + col
bg = 'lightgray'
def check_blocked(field, row, col):
for r in range(row-1, row+2):
for c in range(col-1, col+2):
if r < 0 or r > field_size - 1:
continue
if mode == 1:
btn = Button(window, text="", bg=bg, width=5, height=2, command=lambda i=index: colorize(i, field))
else:
if field[index] == '1':
bg = 'pink'
btn = Button(window, text="", bg=bg, width=5, height=2)
if c < 0 or c > field_size - 1:
continue
btn.grid(column=col, row=row)
buttons.append(btn)
if (field[r*field_size + c]).strip() != '':
return True
return False
def colorize(index, field):
bg = 'black'
if field[index] == '1':
bg = 'red'
buttons[index].configure(bg=bg)
def colorize(field):
for r in range(0, field_size):
for c in range(0, field_size):
index = r*field_size+c
btn = buttons[index]
bg = 'white'
if field[index] == '1':
bg = 'pink'
elif field[index] == '\\':
bg = 'red'
elif field[index] == '0':
bg = 'black'
elif field[index] == 'p':
bg = 'blue'
btn.configure(bg=bg)
def button_action(field, row, col):
if mode == FieldMode.Enemy:
shoot(field, row, col)
colorize(field)
elif mode == FieldMode.Friendly:
colorize(field)
elif mode == FieldMode.Placing:
set_ship(field, row, col, ship_size, ship_direction)
def keypress_handler(e):
global ship_size
if e.keysym in '1234':
ship_size = int(e.keysym)
window = Tk()
window.title("Ship Craft!")
window.geometry('450x410')
window.bind_all('<KeyPress>', keypress_handler)
set_ship(my_field, 1, 1, 4, 1)
set_ship(my_field, 0, 6, 3, 0)
set_ship(my_field, 9, 9, 1, 0)
#set_ship(my_field, 1, 1, 4, 1)
#set_ship(my_field, 0, 6, 3, 0)
#set_ship(my_field, 9, 9, 1, 0)
#set_ship(my_field, 0, 0, 1, 0)
#set_ship(my_field, 9, 0, 1, 0)
#set_ship(my_field, 9, 2, 4, 1)
draw_field(window, my_field)
colorize(my_field)
window.mainloop()

View File

@ -43,23 +43,28 @@ def create_shape(json):
def generate_shape():
max_length = 100
types = list(ShapeType)
rnd = random.randint(1, len(types) - 1)
shape_type = types[rnd]
colors = list(ShapeColor)
type_index = random.randint(1, len(types) - 1)
shape_type = types[type_index]
if shape_type == ShapeType.Circle:
obj = Circle(rnd)
obj = Circle(random.randint(1, 100))
elif shape_type == ShapeType.Square:
obj = Square(rnd)
obj = Square(random.randint(1, 100))
elif shape_type == ShapeType.Rectangle:
obj = Rectangle(rnd, rnd)
obj = Rectangle(random.randint(1, 100), random.randint(1, 100))
else:
raise TypeError(f'Происходит что-то непонятное')
obj.color = ShapeColor.Unknown
color_index = random.randint(0, len(colors) - 1)
obj.color = colors[color_index]
return obj
@ -91,8 +96,3 @@ shape_list = filter_shapes(shape_list, 20)
shape_list.append(generate_shape())
python_to_json(shape_list, 'shapes.json')