Lesson results commit

This commit is contained in:
Artur Savitskiy 2024-04-29 19:30:01 +02:00
parent 935e6e11ed
commit f6c7ea6e43
2 changed files with 27 additions and 19 deletions

View File

@ -17,7 +17,7 @@ class ShipField:
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
self.ships = [4, 3, 2, 2, 2, 1, 1, 1, 1]
self.ships = [4, 3, 3, 2, 2, 2, 1, 1, 1, 1]
self.field_size = 10
self.field_mode = ShipMode.PUT
self.ship_size = 4
@ -39,15 +39,15 @@ class ShipField:
self.clear_marker()
if self.field_mode == ShipMode.PUT:
if self.check_ship(row, col):
self.get_ship(row, col)
#if self.check_ship(row, col):
# self.get_ship(row, col)
elif self.field_size in self.ships and self.check_possible(row, col):
#elif
if self.ship_size in self.ships and self.check_possible(row, col):
self.set_ship(row, col)
elif self.field_mode == ShipMode.SHOOT:
self.shoot(row, col)
def target(self, row, col):
self.clear_marker()
@ -97,6 +97,9 @@ class ShipField:
index = row * self.field_size + c
self.field[index] = "1"
if self.ship_size in self.ships:
self.ships.remove(self.ship_size)
def shoot(self, row, col):
if row < 0 or row > self.field_size - 1:
return ShootResult.UNDEFINED
@ -112,18 +115,18 @@ class ShipField:
else:
return ShootResult.UNDEFINED
def check_possible(self, row, col, ship_direction):
if ship_direction == ShipDirection.VERTICAL:
def check_possible(self, row, col):
if self.ship_direction == ShipDirection.VERTICAL:
if self.field_size - row >= self.ship_size:
for r in range(row, row + self.ship_size):
if not self.check_blocked(self.field, r, col):
if not self.check_blocked(r, col):
return False
return True
if ship_direction == ShipDirection.HORIZONTAL:
if self.ship_direction == ShipDirection.HORIZONTAL:
if self.field_size - col >= self.ship_size:
for c in range(col, col + self.ship_size):
if not self.check_blocked(self.field, row, c):
if not self.check_blocked(row, c):
return False
return True
@ -138,18 +141,21 @@ class ShipField:
return False
return True
def set_ship_size(self, value):
if value.isnumeric():
nummer = int(value)
if 1 <= nummer <= 4:
self.ship_size = nummer
if value is None:
return
if type(value) is str and value.isnumeric():
value = int(value)
if type(value) is int and 1 <= value <= 4:
self.ship_size = value
def set_ship_direction(self, value):
if value is None:
return
if value != ShipDirection.UNKNOWN:
self.ships_directin = value
self.ship_direction = value
def toggle_ship_direction(self):
if self.ship_direction == ShipDirection.VERTICAL:

View File

@ -16,9 +16,9 @@ def draw_field(window, field, col_offset):
index = r * field.field_size + c
btn = Button(window, text='', width=5, height=2)
btn.grid(column=c + col_offset, row=r)
btn.bind('<Button-1>', lambda e, x=r, y=c: left_button_click(field, buttons, x, y))
btn.bind('<Button-1>', lambda e, x=r, y=c: left_button_click(buttons, x, y))
btn.bind('<Button-3>', right_button_click)
btn.bind('<Enter>', lambda e, x=r, y=c: button_enter(field, buttons, x, y))
btn.bind('<Enter>', lambda e, x=r, y=c: button_enter(buttons, x, y))
buttons.append(btn)
colorize(field, buttons)
return buttons
@ -80,12 +80,14 @@ def button_enter(buttons, row, col):
enemy_field.target(row, col)
colorize(my_field, my_buttons)
colorize(enemy_field, buttons)
colorize(enemy_field, enemy_buttons)
def savebutton_click(event):
with open('test.json', 'w') as f:
json.dump(f, default=ShipField.convert_to_json)
window = Tk()
window.title("Ship Craft!")
window.geometry('940x410')