Exemple #1
0
    def __init__(self):
        self.edge = 30
        self.cell_size = 63
        self.field = [[None for _ in range(8)] for _ in range(8)]
        for i in range(8):
            self.field[i][1] = Pawn('w', (i, 1))
            self.field[i][6] = Pawn('b', (i, 6))

        self.field[0][7] = Rook('b', (0, 7))
        self.field[7][7] = Rook('b', (7, 7))
        self.field[1][7] = Knight('b', (1, 7))
        self.field[6][7] = Knight('b', (6, 7))
        self.field[2][7] = Bishop('b', (2, 7))
        self.field[5][7] = Bishop('b', (5, 7))
        self.field[4][7] = King('b', (4, 7))
        self.field[3][7] = Queen('b', (3, 7))

        self.field[0][0] = Rook('w', (0, 0))
        self.field[7][0] = Rook('w', (7, 0))
        self.field[1][0] = Knight('w', (1, 0))
        self.field[6][0] = Knight('w', (6, 0))
        self.field[2][0] = Bishop('w', (2, 0))
        self.field[5][0] = Bishop('w', (5, 0))
        self.field[4][0] = King('w', (4, 0))
        self.field[3][0] = Queen('w', (3, 0))

        self.field_copy = self.field.copy()
        self.curr_player = 'w'
        self.move_history = []
Exemple #2
0
    def set_figures(self):
        for x in range(8):
            self.figures.append(
                Pawn(x, 1, Color.BLACK, self.width // 8, self.height // 8))
            self.figures.append(
                Pawn(x, 6, Color.WHITE, self.width // 8, self.height // 8))

        self.figures.append(
            Bishop(2, 0, Color.BLACK, self.width // 8, self.height // 8))
        self.figures.append(
            Bishop(5, 0, Color.BLACK, self.width // 8, self.height // 8))
        self.figures.append(
            Bishop(2, 7, Color.WHITE, self.width // 8, self.height // 8))
        self.figures.append(
            Bishop(5, 7, Color.WHITE, self.width // 8, self.height // 8))

        self.figures.append(
            Knight(1, 0, Color.BLACK, self.width // 8, self.height // 8))
        self.figures.append(
            Knight(6, 0, Color.BLACK, self.width // 8, self.height // 8))
        self.figures.append(
            Knight(1, 7, Color.WHITE, self.width // 8, self.height // 8))
        self.figures.append(
            Knight(6, 7, Color.WHITE, self.width // 8, self.height // 8))

        self.figures.append(
            Rook(0, 0, Color.BLACK, self.width // 8, self.height // 8))
        self.figures.append(
            Rook(7, 0, Color.BLACK, self.width // 8, self.height // 8))
        self.figures.append(
            Rook(0, 7, Color.WHITE, self.width // 8, self.height // 8))
        self.figures.append(
            Rook(7, 7, Color.WHITE, self.width // 8, self.height // 8))

        self.figures.append(
            Queen(3, 0, Color.BLACK, self.width // 8, self.height // 8))
        self.figures.append(
            Queen(3, 7, Color.WHITE, self.width // 8, self.height // 8))

        self.figures.append(
            King(4, 0, Color.BLACK, self.width // 8, self.height // 8))
        self.figures.append(
            King(4, 7, Color.WHITE, self.width // 8, self.height // 8))
Exemple #3
0
 def chess_start(self):
     for figures_color in self.starting_positions:
         for coords in self.starting_positions[figures_color]:
             constructor_args = [coords[0], coords[1], 'wheat' if coords[1] == 0 or coords[1] == 1 else 'snow4']
             if coords[1] == 1 or coords[1] == 6:
                 self.all_heroes.append(Pawn(constructor_args))
             else:
                 if coords[0] == 0 or coords[0] == 7:
                     self.all_heroes.append(Rook(constructor_args))
                 elif coords[0] == 1 or coords[0] == 6:
                     self.all_heroes.append(Knight(constructor_args))
                 elif coords[0] == 2 or coords[0] == 5:
                     self.all_heroes.append(
                         Bishop(constructor_args))
                 elif coords[0] == 3:
                     self.all_heroes.append(Queen(constructor_args))
                 else:
                     self.all_heroes.append(King(constructor_args))
Exemple #4
0
 def move_if_possible(self, prey_x, prey_y, hero_to_move):
     if (self.last_move and hero_to_move.color == hero_to_move.white_color)\
             or (not self.last_move and hero_to_move.color == hero_to_move.black_color)\
             or ([prey_x, prey_y] == [hero_to_move.x, hero_to_move.y]):
         return
     prey_coords = [prey_x, prey_y]
     occupation_number = 1 if hero_to_move.color == hero_to_move.white_color else -1
     old_x = hero_to_move.x
     old_y = hero_to_move.y
     move_performed = hero_to_move.move(prey_coords, self.all_heroes)
     if move_performed:
         self.is_occupied[old_y][old_x] = 0
         self.is_occupied[hero_to_move.y][hero_to_move.x] = occupation_number
         if hero_to_move.__class__.__name__ == 'Pawn'\
                 and hero_to_move.y == (0 if hero_to_move.color == hero_to_move.black_color else self.board_side - 1):
             constructor_args = [hero_to_move.x, hero_to_move.y, hero_to_move.color]
             self.all_heroes.remove(hero_to_move)
             self.all_heroes.append(Queen(constructor_args))
     self.last_move = not self.last_move
Exemple #5
0
    def get_click(self):
        x, y = pygame.mouse.get_pos()
        # Обработка клика мыши
        # Получение клетки из координат
        cell = self.get_cell((x, y))
        # Если не пауза
        if not self.pause:
            if cell:
                # Фигура на клетке. Нужна для смены фокуса на другую союзную фигуру без повторного клика по текущей.
                cell_figure = self.board.figure_at(cell)
                # Если фигура не выбрана:
                if not self.target:
                    # Фокус на данную клетку
                    if self.is_curr_player_figure_at(cell):
                        self.figure_target(cell)
                # Если фигура уже выбрана:
                else:
                    # Убирает фокус при повторном нажатии
                    if cell == self.target.pos:
                        self.figure_untarget()
                    # Меняет фокус на другую союзную фигуру
                    elif cell_figure and cell_figure.color == self.board.curr_player:
                        self.figure_target(cell)
                    # Если клетка находится в доступных шагах:
                    elif cell in self.target_poss_moves:
                        # Является ли фигура королём
                        if self.target.name == 'King' and cell in self.board.castle_moves_for_player(
                        ):
                            # Castle that king
                            self.add_move(self.target.pos, cell)
                            self.board.castle_king(self.target, cell)
                        else:
                            # Движение фигуры
                            self.add_move(self.target.pos, cell)
                            self.move_figure(self.target, cell)
                            # Повышение пешки
                            if self.target.name == 'Pawn' and (cell[1] == 0 or
                                                               cell[1] == 7):
                                self.board.field[cell[0]][cell[1]] = None
                                self.board.field[cell[0]][cell[1]] = Queen(
                                    self.board.curr_player, cell)

                        # Снятие фокуса
                        self.figure_untarget()
                        # Смена игрока
                        self.change_player()
                        # Проверка на шах и мат, и обновление списка со всеми возможными ходами
                        self.all_poss_moves = self.get_all_poss_moves()
                        checkmate = True
                        for figures_pos in self.all_poss_moves:
                            if len(self.all_poss_moves[figures_pos]) != 0:
                                checkmate = False
                        # Если шах и мат:
                        if checkmate:
                            # Определение победителя
                            self.winner = 'Белые' if self.board.curr_player == 'b' else 'Чёрные'
                            self.pause = True
                            self.draw_board()
                            self.draw_win_menu()
        # Иначе переключение на победное/главное меню
        else:
            # Если есть победитель, то переключение на победное меню
            if self.winner:
                if 365 <= y <= 440:
                    # Если "да", то происходит сброс и возврат в гланое меню
                    if 255 <= x <= 345:
                        self.reset()
                    # Если "нет", то выход из игры
                    elif 455 <= x <= 545:
                        quit()
                    else:
                        pass
            # Иначе главное меню
            else:
                # Если "Новая игра"
                if 225 <= x <= 625 and 210 <= y <= 295:
                    self.new_game = True
                    self.pause = False
                # Если "Выход"
                elif 305 <= x <= 555 and 310 <= y <= 395:
                    quit()
Exemple #6
0
 def test_check_valid_position_rook_move_up(self):
     queen = Queen(4, 4, Color.BLACK)
     self.assertTrue(queen.check_valid_position(4, 0, []))
Exemple #7
0
 def test_check_valid_position_bishop_move_down_left(self):
     queen = Queen(4, 4, Color.BLACK)
     self.assertTrue(queen.check_valid_position(1, 7, []))