Lesson results commit
This commit is contained in:
parent
935e6e11ed
commit
f6c7ea6e43
|
@ -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_size = 10
|
||||||
self.field_mode = ShipMode.PUT
|
self.field_mode = ShipMode.PUT
|
||||||
self.ship_size = 4
|
self.ship_size = 4
|
||||||
|
@ -39,15 +39,15 @@ class ShipField:
|
||||||
self.clear_marker()
|
self.clear_marker()
|
||||||
|
|
||||||
if self.field_mode == ShipMode.PUT:
|
if self.field_mode == ShipMode.PUT:
|
||||||
if self.check_ship(row, col):
|
#if self.check_ship(row, col):
|
||||||
self.get_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)
|
self.set_ship(row, col)
|
||||||
elif self.field_mode == ShipMode.SHOOT:
|
elif self.field_mode == ShipMode.SHOOT:
|
||||||
self.shoot(row, col)
|
self.shoot(row, col)
|
||||||
|
|
||||||
|
|
||||||
def target(self, row, col):
|
def target(self, row, col):
|
||||||
self.clear_marker()
|
self.clear_marker()
|
||||||
|
|
||||||
|
@ -97,6 +97,9 @@ class ShipField:
|
||||||
index = row * self.field_size + c
|
index = row * self.field_size + c
|
||||||
self.field[index] = "1"
|
self.field[index] = "1"
|
||||||
|
|
||||||
|
if self.ship_size in self.ships:
|
||||||
|
self.ships.remove(self.ship_size)
|
||||||
|
|
||||||
def shoot(self, row, col):
|
def shoot(self, row, col):
|
||||||
if row < 0 or row > self.field_size - 1:
|
if row < 0 or row > self.field_size - 1:
|
||||||
return ShootResult.UNDEFINED
|
return ShootResult.UNDEFINED
|
||||||
|
@ -112,18 +115,18 @@ class ShipField:
|
||||||
else:
|
else:
|
||||||
return ShootResult.UNDEFINED
|
return ShootResult.UNDEFINED
|
||||||
|
|
||||||
def check_possible(self, row, col, ship_direction):
|
def check_possible(self, row, col):
|
||||||
if ship_direction == ShipDirection.VERTICAL:
|
if self.ship_direction == ShipDirection.VERTICAL:
|
||||||
if self.field_size - row >= self.ship_size:
|
if self.field_size - row >= self.ship_size:
|
||||||
for r in range(row, 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 False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if ship_direction == ShipDirection.HORIZONTAL:
|
if self.ship_direction == ShipDirection.HORIZONTAL:
|
||||||
if self.field_size - col >= self.ship_size:
|
if self.field_size - col >= self.ship_size:
|
||||||
for c in range(col, 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 False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -138,18 +141,21 @@ class ShipField:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def set_ship_size(self, value):
|
def set_ship_size(self, value):
|
||||||
if value.isnumeric():
|
if value is None:
|
||||||
nummer = int(value)
|
return
|
||||||
if 1 <= nummer <= 4:
|
|
||||||
self.ship_size = nummer
|
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):
|
def set_ship_direction(self, value):
|
||||||
if value is None:
|
if value is None:
|
||||||
return
|
return
|
||||||
if value != ShipDirection.UNKNOWN:
|
if value != ShipDirection.UNKNOWN:
|
||||||
self.ships_directin = value
|
self.ship_direction = value
|
||||||
|
|
||||||
def toggle_ship_direction(self):
|
def toggle_ship_direction(self):
|
||||||
if self.ship_direction == ShipDirection.VERTICAL:
|
if self.ship_direction == ShipDirection.VERTICAL:
|
||||||
|
|
|
@ -16,9 +16,9 @@ def draw_field(window, field, col_offset):
|
||||||
index = r * field.field_size + c
|
index = r * field.field_size + c
|
||||||
btn = Button(window, text='', width=5, height=2)
|
btn = Button(window, text='', width=5, height=2)
|
||||||
btn.grid(column=c + col_offset, row=r)
|
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('<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)
|
buttons.append(btn)
|
||||||
colorize(field, buttons)
|
colorize(field, buttons)
|
||||||
return buttons
|
return buttons
|
||||||
|
@ -80,12 +80,14 @@ def button_enter(buttons, row, col):
|
||||||
enemy_field.target(row, col)
|
enemy_field.target(row, col)
|
||||||
|
|
||||||
colorize(my_field, my_buttons)
|
colorize(my_field, my_buttons)
|
||||||
colorize(enemy_field, buttons)
|
colorize(enemy_field, enemy_buttons)
|
||||||
|
|
||||||
|
|
||||||
def savebutton_click(event):
|
def savebutton_click(event):
|
||||||
with open('test.json', 'w') as f:
|
with open('test.json', 'w') as f:
|
||||||
json.dump(f, default=ShipField.convert_to_json)
|
json.dump(f, default=ShipField.convert_to_json)
|
||||||
|
|
||||||
|
|
||||||
window = Tk()
|
window = Tk()
|
||||||
window.title("Ship Craft!")
|
window.title("Ship Craft!")
|
||||||
window.geometry('940x410')
|
window.geometry('940x410')
|
||||||
|
|
Loading…
Reference in New Issue