New Version

This commit is contained in:
danii 2023-11-20 19:05:48 +01:00
parent 3e7ee1386a
commit d42048e178
1 changed files with 27 additions and 15 deletions

View File

@ -32,13 +32,13 @@ def set_ship(row, col, size, direction):
return return
if col < 0 or col > field_size - 1: if col < 0 or col > field_size - 1:
return return
index = row * field_size *col index = row * field_size + col
if direction == 0: if direction == 0:
if field_size - row < size: if field_size - row < size:
return return
for r in range(row, row + size): for r in range(row, row + size):
index = r * field_size + col index = r * field_size + col
my_field[index] = '1' my_field[index] = "1"
if direction == 1: if direction == 1:
if field_size - col < size: if field_size - col < size:
return return
@ -49,41 +49,52 @@ def set_ship(row, col, size, direction):
def shoot(field, row, col): def shoot(field, row, col):
if row < 0 or row > field_size - 1: if row < 0 or row > field_size - 1:
return return ShootResult.Undefined
if col < 0 or col > field_size - 1: if col < 0 or col > field_size - 1:
return return ShootResult.Undefined
index = row * field_size *col
if field[index] == "": index = row * field_size + col
field[index] = 0 if (field[index]).strip() == "":
field[index] = "0"
return ShootResult.Empty return ShootResult.Empty
elif field[index] == "1": elif field[index] == "1":
field[index] = "\\" field[index] = "\\"
return ShootResult.Damaged return ShootResult.Damaged
elif field[index] == "1" or field[index] == "\\" or field[index] == "X": elif field[index] == "1" or field[index] == "\\" or field[index] == "X":
return ShootResult.Damaged return ShootResult.Undefined
else:
return ShootResult.Undefined
def draw_field(window, field): def draw_field(window, field):
for r in range(0, field_size): for r in range(0, field_size):
for c in range(0, field_size): for c in range(0, field_size):
index = r * field_size + c
bg = 'white' bg = 'white'
index = r * field_size +c
if field[index] == '1': if field[index] == '1':
bg = 'pink' bg = 'pink'
btn = Button(window, text="", bg=bg, width=5, height=2) btn = Button(window, text='', bg=bg, width=5, height=2)
btn.grid(column=c, row=r) btn.grid(column=c, row=r)
btn.bind('<Button-1>', lambda e, x=r, y=c: button_click(field, x, y))
buttons.append(btn) buttons.append(btn)
def colorize(field, buttons): def colorize(field, buttons):
bg = "grey"
for i in range(len(field)): for i in range(len(field)):
if i == '1': bg = "white"
if field[i] == '1':
bg = 'pink' bg = 'pink'
elif i == '\\': if field[i] == '\\':
bg = 'red' bg = 'red'
elif i == '0': if field[i] == '0':
bg = 'black' bg = 'black'
buttons.configure(bg=bg) buttons[i].configure(bg=bg)
def button_click(field, row, col):
shoot(field, row, col)
colorize(field, buttons)
window = Tk() window = Tk()
@ -95,5 +106,6 @@ set_ship(0, 6, 3, 0)
set_ship(7, 3, 1, 0) set_ship(7, 3, 1, 0)
draw_field(window, my_field) draw_field(window, my_field)
colorize(my_field, buttons)
window.mainloop() window.mainloop()