diff --git a/HNS/Excercises/ShipCraft/main.py b/HNS/Excercises/ShipCraft/main.py index c74ff48..42ad99a 100644 --- a/HNS/Excercises/ShipCraft/main.py +++ b/HNS/Excercises/ShipCraft/main.py @@ -7,6 +7,7 @@ buttons = [] field_size = 10 ship_size = 4 ship_direction = 0 +field_mode = 0 empty_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', @@ -33,22 +34,22 @@ class ShootResult(Enum): UNDEFINED = "UNDEFINED" -def set_ship(row, col, size, direction): +def set_ship(row, col, ship_size, direction): if row < 0 or row > field_size: return if col < 0 or col > field_size: return index = row * field_size + col if direction == 0: - if field_size - row < size: + if field_size - row < ship_size: return - for r in range(row, row + size): + for r in range(row, row + ship_size): index = r * field_size + col my_field[index] = "1" if direction == 1: - if field_size - col < size: + if field_size - col < ship_size: return - for c in range(col, col + size): + for c in range(col, col + ship_size): index = row * field_size + c my_field[index] = "1" @@ -73,12 +74,10 @@ def draw_field(window, field): for r in range(0, field_size): for c in range(0, field_size): index = r * field_size + c - bg = 'white' - if field[index] == "1": - bg = 'pink' - btn = Button(window, text='', bg=bg, width=5, height=2) + btn = Button(window, text='', width=5, height=2) btn.grid(column=c, row=r) - btn.bind('', lambda e, x=r, y=c: button_click(field, x, y)) + btn.bind('', lambda e, x=r, y=c: left_button_click(field, x, y)) + btn.bind('', lambda e, x=r, y=c: right_button_click(field, x, y)) btn.bind('', lambda e, x=r, y=c: button_enter(field, x, y)) buttons.append(btn) @@ -92,26 +91,41 @@ def colorize(field, buttons): bg = 'red' if field[i] == "0": bg = 'black' + if field[i] == "p": + bg = 'blue' buttons[i].configure(bg=bg) -def button_click(field, row, col): - shoot(field, row, col) +def left_button_click(field, row, col): + if field_mode == 0: + if check_possible(field, row, col): + set_ship(row, col,ship_size, ship_direction) + elif field_mode == 1: + shoot(field, row, col) colorize(field, buttons) -def button_enter(field, row, col): - if ship_direction == 0: - if field_size - row < ship_size: - print("Not OK") - return - if ship_direction == 1: - if field_size - col < ship_size: - print("Not OK") - return - print("OK") +def right_button_click(field, row, col): + pass +def button_enter(field, row, col): + if field_mode == 0: + for i in range(0, len(field)): + if field[i] == "p": + field[i] = '' + + if check_possible(field, row, col): + if ship_direction == 0: + for r in range(row, row + ship_size): + field[r * field_size + col] = "p" + + if ship_direction == 1: + for c in range(col, col + ship_size): + field[row * field_size + c] = "p" + + colorize(field, buttons) + def check_possible(field, row, col): # Функция должна возвращать True, если можно поставить сюда корабль, # в противном случае - False @@ -125,8 +139,6 @@ def check_possible(field, row, col): return False return True - return False - if ship_direction == 1: if field_size - col >= ship_size: @@ -144,7 +156,8 @@ def check_blocked(field, row, col): for r in range(row - 1, row + 2): for c in range(col - 1, col + 2): if 0 < r < field_size and 0 < c < field_size: - if (field[r * field_size + c]).strip() != '': + cell = (field[r * field_size + c]).strip() + if cell != '' and cell != 'p': return False return True @@ -156,7 +169,7 @@ set_ship(1, 1, 4, 1) set_ship(0, 6, 3, 0) set_ship(7, 3, 1, 0) draw_field(window, my_field) -# window.mainloop() +window.mainloop() for r in range(0, field_size): blocked_string = "" @@ -167,11 +180,10 @@ for r in range(0, field_size): print(blocked_string[:-2] + ' ' + ship_string[:-2]) print("********************************************************************") - for r in range(0, field_size): possible_string = "" impossible_string = "" for c in range(0, field_size): possible_string += str(check_possible(my_field, r, c))[0] + ", " impossible_string += my_field[r * field_size + c] + ', ' - print(possible_string[:-2] + ' ' + impossible_string[:-2]) \ No newline at end of file + print(possible_string[:-2] + ' ' + impossible_string[:-2])