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
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
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:
|
|
|
|
return
|
|
|
|
if col < 0 or col > field_size - 1:
|
|
|
|
return
|
|
|
|
index = row * field_size *col
|
|
|
|
if field[index] == "":
|
|
|
|
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.Damaged
|
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):
|
|
|
|
bg = 'white'
|
|
|
|
index = r * field_size +c
|
|
|
|
if field[index] == '1':
|
|
|
|
bg = 'pink'
|
|
|
|
btn = Button(window, text="", bg=bg, width=5, height=2)
|
|
|
|
btn.grid(column=c, row=r)
|
|
|
|
buttons.append(btn)
|
|
|
|
|
|
|
|
def colorize(field, buttons):
|
|
|
|
bg = "grey"
|
|
|
|
for i in range(len(field)):
|
|
|
|
if i == '1':
|
|
|
|
bg = 'pink'
|
|
|
|
elif i == '\\':
|
|
|
|
bg = 'red'
|
|
|
|
elif i == '0':
|
|
|
|
bg = 'black'
|
|
|
|
buttons.configure(bg=bg)
|
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-06 19:54:00 +01:00
|
|
|
|
2023-11-06 15:35:22 +01:00
|
|
|
draw_field(window, my_field)
|
|
|
|
|
2023-11-13 10:50:09 +01:00
|
|
|
window.mainloop()
|