def __init__(self, master):
self.ship_sizes = [4, 3, 3, 2, 2, 2, 1, 1, 1, 1]
# Инициализация игровых досок
self.player_board = self.create_board()
self.computer_board = self.create_board()
self.place_ships(self.player_board)
self.place_ships(self.computer_board)
self.total_hits = sum(self.ship_sizes)
# Флаг для отслеживания хода
return [[None for _ in range(self.board_size)] for _ in range(self.board_size)]
def place_ships(self, board):
for size in self.ship_sizes:
direction = random.choice(['horizontal', 'vertical'])
if direction == 'horizontal':
max_col = self.board_size - size
max_row = self.board_size
max_row = self.board_size - size
max_col = self.board_size
row = random.randint(0, max_row - 1)
col = random.randint(0, max_col - 1)
if self.can_place_ship(board, row, col, size, direction):
self.mark_ship(board, row, col, size, direction)
def can_place_ship(self, board, row, col, size, direction):
if direction == 'horizontal':
for c in range(col - 1, col + size + 1):
for r in range(row - 1, row + 2):
if 0 <= r < self.board_size and 0 <= c < self.board_size:
if board[r][c] == 'ship':
for r in range(row - 1, row + size + 1):
for c in range(col - 1, col + 2):
if 0 <= r < self.board_size and 0 <= c < self.board_size:
if board[r][c] == 'ship':
def mark_ship(self, board, row, col, size, direction):
if direction == 'horizontal':
for c in range(col, col + size):
for r in range(row, row + size):
def create_widgets(self):
self.player_frame = tk.Frame(self.master)
self.computer_frame = tk.Frame(self.master)
self.player_frame.pack(side=tk.LEFT, padx=20, pady=20)
self.computer_frame.pack(side=tk.RIGHT, padx=20, pady=20)
tk.Label(self.player_frame, text="Ваше поле").grid(row=0, column=0, columnspan=11)
tk.Label(self.computer_frame, text="Поле компьютера").grid(row=0, column=0, columnspan=11)
self.player_cells = self.create_grid(self.player_frame, self.player_board, False)
self.computer_cells = self.create_grid(self.computer_frame, self.computer_board, True)
def create_grid(self, parent, board, clickable):
# Добавляем буквы над сеткой
for col in range(self.board_size):
lbl = tk.Label(parent, text=chr(65 + col))
lbl.grid(row=1, column=col+1)
for row in range(self.board_size):
lbl = tk.Label(parent, text=str(row))
lbl.grid(row=row+2, column=0)
for row in range(self.board_size):
for col in range(self.board_size):
frame.grid(row=row+2, column=col+1)
if board[row][col] == 'ship' and parent == self.player_frame:
frame.bind('<Button-1>', lambda e, r=row, c=col: self.player_turn(r, c))
def update_cell(self, cells, row, col, status):
cells[row][col].config(bg=color)
def player_turn(self, row, col):
if self.game_over or self.computer_board[row][col] in ['hit', 'miss']:
if self.computer_board[row][col] == 'ship':
self.computer_board[row][col] = 'hit'
self.update_cell(self.computer_cells, row, col, 'hit')
if self.player_hits == self.total_hits:
self.end_game("Поздравляем! Вы выиграли!")
self.computer_board[row][col] = 'miss'
self.update_cell(self.computer_cells, row, col, 'miss')
row = random.randint(0, self.board_size - 1)
col = random.randint(0, self.board_size - 1)
if self.player_board[row][col] not in ['hit', 'miss']:
if self.player_board[row][col] == 'ship':
self.player_board[row][col] = 'hit'
self.update_cell(self.player_cells, row, col, 'hit')
self.end_game("Компьютер выиграл!")
self.player_board[row][col] = 'miss'
self.update_cell(self.player_cells, row, col, 'miss')
def end_game(self, message):
messagebox.showinfo("Игра окончена", message)
if __name__ == "__main__":
game = BattleshipGame(root)