Example #1
0
    def _queen_moves(self, loc_piece, first_player):
        coord = loc_piece[0]
        piece = loc_piece[1]

        moves = []

        for direction in tuple_add(self._cardinal_directions(),
                                   self._diagonal_directions()):

            move = (coord, coord, piece)

            while (True):

                move = (coord, tuple_add(move[1], direction), piece)

                if self._valid_destination(move, first_player):
                    moves.append(move)

                    if self._space_occupied_by_opponent(move[1], first_player):
                        break

                else:
                    break

        return moves
Example #2
0
    def _king_moves(self, loc_piece, first_player):
        coord = loc_piece[0]
        piece = loc_piece[1]

        moves = []

        for direction in tuple_add(self._cardinal_directions(),
                                   self._diagonal_directions()):
            move = (coord, tuple_add(coord, direction), piece)

            if self._valid_destination(move, first_player):
                moves.append(move)

        return moves
Example #3
0
    def _knight_moves(self, loc_piece, first_player):
        coord = loc_piece[0]
        piece = loc_piece[1]

        moves = []

        for direction in self._knight_directions():
            move = (coord, tuple_add(coord, direction), piece)

            if self._valid_destination(move, first_player):
                moves.append(move)

        return moves
Example #4
0
    def _pawn_moves(self, loc_piece, first_player):
        coord = loc_piece[0]
        piece = loc_piece[1]

        moves = []

        if first_player:  # white

            if coord[1] + 1 == self.board_dimensions[
                    1]:  # If pawn would reach end of board by moving up, always promote to queen

                piece = 1  # White Queen

            move = tuple(
                (coord, tuple_add(coord, (0, 1)), piece))  # move up one space
            if self._in_board(move[1]) and self._space_empty(
                    move[1]) and not self._move_into_check(move, first_player):

                moves.append(move)

                if coord[
                        1] == 1:  # Pawn's first move can be 2 spaces if both unoccupied
                    move = tuple((coord, tuple_add(coord, (0, 2)), piece))
                    if self._in_board(move[1]) and self._space_empty(
                            move[1]) and not self._move_into_check(
                                move, first_player):
                        moves.append(move)

            move = tuple((coord, tuple_add(coord,
                                           (-1, 1)), piece))  # capture up left
            if self._in_board(move[1]) and self._space_occupied_by_opponent(
                    move[1], first_player) and not self._move_into_check(
                        move, first_player):
                moves.append(move)

            move = tuple((coord, tuple_add(coord,
                                           (1, 1)), piece))  # capture up right
            if self._in_board(move[1]) and self._space_occupied_by_opponent(
                    move[1], first_player) and not self._move_into_check(
                        move, first_player):
                moves.append(move)

        else:  # black

            if coord[1] - 1 == 0:  # If pawn would reach end of board by moving up, always promote to queen

                piece = 7  # Black queen

            move = tuple(
                (coord, tuple_add(coord,
                                  (0, -1)), piece))  # move down one space
            if self._in_board(move[1]) and self._space_empty(
                    move[1]) and not self._move_into_check(move, first_player):
                moves.append(move)

                if coord[1] == self.board_dimensions[
                        1] - 1:  # Pawn's first move can be 2 spaces if both unoccupied
                    move = tuple((coord, tuple_add(coord, (0, -2)), piece))
                    if self._in_board(move[1]) and self._space_empty(
                            move[1]) and not self._move_into_check(
                                move, first_player):
                        moves.append(move)

            move = tuple(
                (coord, tuple_add(coord,
                                  (-1, -1)), piece))  # capture down left
            if self._in_board(move[1]) and self._space_occupied_by_opponent(
                    move[1], first_player) and not self._move_into_check(
                        move, first_player):
                moves.append(move)

            move = tuple(
                (coord, tuple_add(coord,
                                  (1, -1)), piece))  # capture down right
            if self._in_board(move[1]) and self._space_occupied_by_opponent(
                    move[1], first_player) and not self._move_into_check(
                        move, first_player):
                moves.append(move)

        return moves
Example #5
0
    def _player_king_threatened(self, first_player):

        if first_player:
            coord = self.white_king_loc_piece[0]
            offset = 6  # first player checks against black pieces

            for direction in ((1, 1),
                              (-1, 1)):  # white king threatened by black pawn?
                if self.get(tuple_add(coord, direction),
                            -1) == 11:  #if occupied by enemy pawn
                    return True
        else:
            coord = self.black_king_loc_piece[0]
            offset = 0  #not first player checks against white pieces

            for direction in ((1, -1),
                              (-1,
                               -1)):  # black king threatened by white pawn?
                if self.get(tuple_add(coord, direction),
                            -1) == 5:  #if occupied by enemy pawn
                    return True

        for direction in self._knight_directions(
        ):  # king threatened by enemy knight?
            if self.get(tuple_add(coord, direction),
                        -1) == 4 + offset:  #if occupied by enemy knight
                return True

        for direction in self._cardinal_directions(
        ):  # king threatened by enemy queen or rook
            loc = tuple_add(coord, direction)

            while (self._in_board(loc)):
                piece = self.get(loc, None)

                if piece is not None:
                    if piece in {1 + offset,
                                 2 + offset}:  #threatened by queen or rook
                        return True
                    else:
                        break  #switch direction

                loc = tuple_add(loc, direction)

        for direction in self._diagonal_directions(
        ):  # king threatened by enemy queen or bishop
            loc = tuple_add(coord, direction)

            while (self._in_board(loc)):
                piece = self.get(loc, None)

                if piece is not None:
                    if piece in {1 + offset,
                                 3 + offset}:  #threatened by queen or bishop
                        return True
                    else:
                        break  #switch direction

                loc = tuple_add(loc, direction)

        return False