Exemplo n.º 1
0
    def getPossibleMoves(self):
        # Function run to yield a group of all possible legal and illegal
        # 'Move' objects to the calling function. Does this by running through
        # a list of movements and testing for valid moves before yielding them.
        # Also handles the promotion of pieces and the ability for a piece to
        # move two spaces forward on its first move.
        pos = self.position
        movement = C(0, 1) if self.side == WHITE else C(0, -1)
        advanceOnePos = pos + movement
        if self.board.isValidPos(advanceOnePos):
            if self.board.pieceAtPosition(advanceOnePos) is None:
                col = advanceOnePos[1]
                if col in [7, 0]:
                    piecesForPromotion = \
                        [Rook(self.board, self.side, advanceOnePos),
                         Knight(self.board, self.side, advanceOnePos),
                         Bishop(self.board, self.side, advanceOnePos),
                         Queen(self.board, self.side, advanceOnePos)]
                    for piece in piecesForPromotion:
                        move = Move(self, advanceOnePos)
                        move.promotion = True
                        piece.movesMade = self.movesMade
                        move.specialMovePiece = piece
                        yield move
                else:
                    yield Move(self, advanceOnePos)

        if self.movesMade == 0:
            advanceTwoPos = pos + movement + movement
            if self.board.isValidPos(advanceTwoPos):
                if self.board.pieceAtPosition(advanceTwoPos) is None and \
                        self.board.pieceAtPosition(advanceOnePos) is None:
                    yield Move(self, advanceTwoPos)

        movements = [C(1, 1), C(
            -1, 1)] if self.side == WHITE else [C(1, -1), C(-1, -1)]
        for movement in movements:
            newPos = self.position + movement
            if self.board.isValidPos(newPos):
                pieceToTake = self.board.pieceAtPosition(newPos)
                if pieceToTake and pieceToTake.side != self.side:
                    col = newPos[1]
                    if col in [7, 0]:
                        piecesForPromotion = \
                            [Rook(self.board, self.side, newPos),
                             Knight(self.board, self.side, newPos),
                             Bishop(self.board, self.side, newPos),
                             Queen(self.board, self.side, newPos)]
                        for piece in piecesForPromotion:
                            move = Move(self,
                                        newPos,
                                        pieceToCapture=pieceToTake)
                            move.promotion = True
                            move.specialMovePiece = piece
                            yield move
                    else:
                        yield Move(self, newPos, pieceToCapture=pieceToTake)
Exemplo n.º 2
0
    def get_possible_moves(self):
        pos = self.position

        # Pawn moves one up
        movement = C(0, 1) if self.side == WHITE else C(0, -1)
        up_pos = pos + movement
        if self.board.is_valid_pos(up_pos):
            # Promotion moves
            if self.board.get_piece_at_pos(up_pos) is None:
                col = up_pos[1]
                if col == 7 or col == 0:
                    piecesForPromotion = \
                        [Rook(self.board, self.side, up_pos),
                         Knight(self.board, self.side, up_pos),
                         Bishop(self.board, self.side, up_pos),
                         Queen(self.board, self.side, up_pos)]
                    for piece in piecesForPromotion:
                        move = Move(self, up_pos)
                        move.promotion = True
                        move.specialMovePiece = piece
                        yield move
                else:
                    yield Move(self, up_pos)

        # Pawn moves two up
        if self.movesMade == 0:
            movement = C(0, 2) if self.side == WHITE else C(0, -2)
            up2_pos = pos + movement
            if self.board.is_valid_pos(up2_pos):
                if self.board.get_piece_at_pos(up2_pos) is None and \
                   self.board.get_piece_at_pos(up_pos) is None:
                    yield Move(self, up2_pos)

        # Pawn takes
        movements = [C(1, 1), C(-1, 1)] \
            if self.side == WHITE else [C(1, -1), C(-1, -1)]

        for movement in movements:
            new_pos = self.position + movement
            if self.board.is_valid_pos(new_pos):
                pieceToTake = self.board.get_piece_at_pos(new_pos)
                if pieceToTake and pieceToTake.side != self.side:
                    col = new_pos[1]
                    # Promotions
                    if col == 7 or col == 0:
                        piecesForPromotion = \
                            [Rook(self.board, self.side, new_pos),
                             Knight(self.board, self.side, new_pos),
                             Bishop(self.board, self.side, new_pos),
                             Queen(self.board, self.side, new_pos)]
                        for piece in piecesForPromotion:
                            move = Move(self, up_pos)
                            move.promotion = True
                            move.specialMovePiece = piece
                            yield move
                    else:
                        yield Move(self, new_pos,
                                   piece_to_capture=pieceToTake)

        # En pessant
        movements = [C(1, 1), C(-1, 1)] \
            if self.side == WHITE else [C(1, -1), C(-1, -1)]
        for movement in movements:
            posBesidePawn = self.position + C(movement[0], 0)
            if self.board.is_valid_pos(posBesidePawn):
                pieceBesidePawn = self.board.get_piece_at_pos(posBesidePawn)
                lastPieceMoved = self.board.getLastPieceMoved()
                lastMoveWasAdvanceTwo = False
                lastMove = self.board.getLastMove()

                if lastMove:
                    if lastMove.new_pos - lastMove.old_pos == C(0, 2) or \
                       lastMove.new_pos - lastMove.old_pos == C(0, -2):
                        lastMoveWasAdvanceTwo = True

                if pieceBesidePawn and \
                   pieceBesidePawn.stringRep == 'p' and \
                   pieceBesidePawn.side != self.side and \
                   lastPieceMoved is pieceBesidePawn and \
                   lastMoveWasAdvanceTwo:
                    move = Move(self, self.position + movement,
                                piece_to_capture=pieceBesidePawn)
                    move.pessant = True
                    move.specialMovePiece = pieceBesidePawn
                    yield move
Exemplo n.º 3
0
    def getPossibleMoves(self):
        currentPosition = self.position

        # Pawn moves one up
        movement = C(0, 1) if self.side == WHITE else C(0, -1)
        advanceOnePosition = currentPosition + movement
        if self.board.isValidPos(advanceOnePosition):
            #Check if piece at position to move to
            if self.board.pieceAtPosition(advanceOnePosition) is None:
                col = advanceOnePosition[1]
                if col == 7 or col == 0:
                    #print("promotion is happening without a capture")
                    '''
                    piecesForPromotion = \
                        [Rook(self.board, self.side, advanceOnePosition, self.number),      #added self.number to these
                         Knight(self.board, self.side, advanceOnePosition, self.number),
                         Bishop(self.board, self.side, advanceOnePosition, self.number),
                         Queen(self.board, self.side, advanceOnePosition, self.number)]
                    for piece in piecesForPromotion:
                    '''
                    move = Move(self, advanceOnePosition)
                    move.promotion = True
                    move.specialMovePiece = Queen(self.board, self.side, advanceOnePosition, self.number)
                    #print(move.specialMovePiece)
                    #print(move)
                    yield (move)
                else:
                    yield (Move(self, advanceOnePosition))

        # Pawn moves two up
        if self.movesMade == 0:
            movement = C(0, 2) if self.side == WHITE else C(0, -2)
            advanceTwoPosition = currentPosition + movement
            if self.board.isValidPos(advanceTwoPosition):
                if self.board.pieceAtPosition(advanceTwoPosition) is None and \
                   self.board.pieceAtPosition(advanceOnePosition) is None:
                    yield Move(self, advanceTwoPosition)

        # Pawn takes
        movements = [C(1, 1), C(-1, 1)] \
            if self.side == WHITE else [C(1, -1), C(-1, -1)]
        
        # Pawn captures another piece
        for m in movements:
            newPosition = self.position + m
            if self.board.isValidPos(newPosition):
                pieceToTake = self.board.pieceAtPosition(newPosition)
                if pieceToTake and pieceToTake.side != self.side:
                    col = newPosition[1]
                    #print("Piece for pawn to take: ")
                    #print(pieceToTake)
                    #print(pieceToTake.board)
                    
                    # Promotion with a capture
                    if col == 7 or col == 0:
                        #print("promotion is happening with capture")
                        '''
                        piecesForPromotion = \
                            [Knight(self.board, self.side, newPosition, self.number),
                             Rook(self.board, self.side, newPosition, self.number),
                             Bishop(self.board, self.side, newPosition, self.number),
                             Queen(self.board, self.side, newPosition, self.number)]
                        for piece in piecesForPromotion:
                        '''
                        move = Move(self, newPosition, pieceToCapture=pieceToTake)
                        move.promotion = True
                        move.specialMovePiece = Queen(self.board, self.side, newPosition, self.number)
                        #print("reset special move piece")
                        #print(move.specialMovePiece)
                        #print(move)
                        yield (move)
                    else:
                        yield (Move(self, newPosition,
                                   pieceToCapture=pieceToTake))

        # En pessant
        movements = [C(1, 1), C(-1, 1)] \
            if self.side == WHITE else [C(1, -1), C(-1, -1)]
        for movement in movements:
            posBesidePawn = self.position + C(movement[0], 0)
            if self.board.isValidPos(posBesidePawn):
                pieceBesidePawn = self.board.pieceAtPosition(posBesidePawn)
                lastPieceMoved = self.board.getLastPieceMoved()
                lastMoveWasAdvanceTwo = False
                lastMove = self.board.getLastMove()

                if lastMove:
                    if lastMove.newPos - lastMove.oldPos == C(0, 2) or \
                       lastMove.newPos - lastMove.oldPos == C(0, -2):
                        lastMoveWasAdvanceTwo = True

                if pieceBesidePawn and \
                   pieceBesidePawn.stringRep == 'p' and \
                   pieceBesidePawn.side != self.side and \
                   lastPieceMoved is pieceBesidePawn and \
                   lastMoveWasAdvanceTwo:
                    move = Move(self, self.position + movement,
                                pieceToCapture=pieceBesidePawn)
                    move.pessant = True
                    move.specialMovePiece = pieceBesidePawn
                    yield (move)
Exemplo n.º 4
0
    def getPossibleMoves(self):
        currentPosition = self.position

        # Pawn moves one up
        movement = C(0, 1) if self.side == WHITE else C(0, -1)
        advanceOnePosition = currentPosition + movement
        if self.board.isValidPos(advanceOnePosition):
            #Promotion moves
            if self.board.pieceAtPosition(advanceOnePosition) is None:
                col = advanceOnePosition[1]
                if col == 7 or col == 0:
                    piecesForPromotion = [
                        Rook(self.board, self.side, advanceOnePosition),
                        Knight(self.board, self.side, advanceOnePosition),
                        Bishop(self.board, self.side, advanceOnePosition),
                        Queen(self.board, self.side, advanceOnePosition)
                    ]
                    for piece in piecesForPromotion:
                        move = Move(self, advanceOnePosition)
                        move.promotion = True
                        move.specialMovePiece = piece
                        yield move
                else:
                    yield Move(self, advanceOnePosition)

        #Pawn moves two up
        if self.movesMade == 0:
            movement = C(0, 2) if self.side == WHITE else C(0, -2)
            advanceTwoPosition = currentPosition + movement
            if self.board.isValidPos(advanceTwoPosition):
                if self.board.pieceAtPosition(
                        advanceTwoPosition
                ) is None and self.board.pieceAtPosition(
                        advanceOnePosition) is None:
                    yield Move(self, advanceTwoPosition)

        #Pawn takes
        movements = [C(1, 1), C(
            -1, 1)] if self.side == WHITE else [C(1, -1), C(-1, -1)]

        for movement in movements:
            newPosition = self.position + movement
            if self.board.isValidPos(newPosition):
                pieceToTake = self.board.pieceAtPosition(newPosition)
                if pieceToTake and pieceToTake.side != self.side:
                    col = newPosition[1]
                    #Promotions
                    if col == 7 or col == 0:
                        piecesForPromotion = [
                            Rook(self.board, self.side, newPosition),
                            Knight(self.board, self.side, newPosition),
                            Bishop(self.board, self.side, newPosition),
                            Queen(self.board, self.side, newPosition)
                        ]
                        for piece in piecesForPromotion:
                            move = Move(self, advanceOnePosition)
                            move.promotion = True
                            move.specialMovePiece = piece
                            yield move
                    else:
                        yield Move(self,
                                   newPosition,
                                   pieceToCapture=pieceToTake)

        #En pessant
        movements = [C(1, 1), C(
            -1, 1)] if self.side == WHITE else [C(1, -1), C(-1, -1)]
        for movement in movements:
            posBesidePawn = self.position + C(movement[0], 0)
            if self.board.isValidPos(posBesidePawn):
                pieceBesidePawn = self.board.pieceAtPosition(posBesidePawn)
                lastPieceMoved = self.board.getLastPieceMoved()
                lastMoveWasAdvanceTwo = False
                lastMove = self.board.getLastMove()

                if lastMove:
                    if lastMove.newPos - lastMove.oldPos == C(
                            0, 2) or lastMove.newPos - lastMove.oldPos == C(
                                0, -2):
                        lastMoveWasAdvanceTwo = True

                if pieceBesidePawn and pieceBesidePawn.stringRep == 'p' and pieceBesidePawn.side != self.side and lastPieceMoved is pieceBesidePawn and lastMoveWasAdvanceTwo:
                    move = Move(self,
                                self.position + movement,
                                pieceToCapture=pieceBesidePawn)
                    move.pessant = True
                    move.specialMovePiece = pieceBesidePawn
                    yield move
Exemplo n.º 5
0
    def getPossibleMoves(self) :
        currentPosition = self.position

        # Pawn moves one up
        movement = C(0, 1) if self.side == WHITE else C(0, -1)
        advanceOnePosition = currentPosition + movement
        if self.board.isValidPos(advanceOnePosition) :
            #Promotion moves
            if self.board.pieceAtPosition(advanceOnePosition) is None :
                col = advanceOnePosition[1]
                if col == 7 or col == 0:
                    piecesForPromotion = [Rook(self.board, self.side, advanceOnePosition), Knight(self.board, self.side, advanceOnePosition), Bishop(self.board, self.side, advanceOnePosition), Queen(self.board, self.side, advanceOnePosition)]
                    for piece in piecesForPromotion :
                        move = Move(self, advanceOnePosition)
                        move.promotion = True
                        move.specialMovePiece = piece
                        yield move
                else :
                    yield Move(self, advanceOnePosition)

        #Pawn moves two up
        if self.movesMade == 0 :
            movement = C(0, 2) if self.side == WHITE else C(0, -2)
            advanceTwoPosition = currentPosition + movement
            if self.board.isValidPos(advanceTwoPosition) :
                if self.board.pieceAtPosition(advanceTwoPosition) is None and self.board.pieceAtPosition(advanceOnePosition) is None:
                    yield Move(self, advanceTwoPosition)

        #Pawn takes
        movements = [C(1,1), C(-1,1)] if self.side == WHITE else [C(1,-1), C(-1,-1)]

        for movement in movements :
            newPosition = self.position + movement
            if self.board.isValidPos(newPosition) :
                pieceToTake = self.board.pieceAtPosition(newPosition)
                if pieceToTake and pieceToTake.side != self.side :
                    col = newPosition[1]
                    #Promotions
                    if col == 7 or col == 0:
                        piecesForPromotion = [Rook(self.board, self.side, newPosition), Knight(self.board, self.side, newPosition), Bishop(self.board, self.side, newPosition), Queen(self.board, self.side, newPosition)]
                        for piece in piecesForPromotion :
                            move = Move(self, advanceOnePosition)
                            move.promotion = True
                            move.specialMovePiece = piece
                            yield move
                    else :
                        yield Move(self, newPosition, pieceToCapture = pieceToTake)

        #En pessant
        movements = [C(1,1), C(-1,1)] if self.side == WHITE else [C(1,-1), C(-1,-1)]
        for movement in movements :
            posBesidePawn = self.position + C(movement[0], 0)
            if self.board.isValidPos(posBesidePawn) :
                pieceBesidePawn = self.board.pieceAtPosition(posBesidePawn)
                lastPieceMoved = self.board.getLastPieceMoved()
                lastMoveWasAdvanceTwo = False
                lastMove = self.board.getLastMove()

                if lastMove :
                    if lastMove.newPos - lastMove.oldPos == C(0,2) or lastMove.newPos - lastMove.oldPos == C(0,-2) :
                        lastMoveWasAdvanceTwo = True


                if pieceBesidePawn and pieceBesidePawn.stringRep == 'p' and pieceBesidePawn.side != self.side and lastPieceMoved is pieceBesidePawn and lastMoveWasAdvanceTwo:
                    move = Move(self, self.position + movement, pieceToCapture = pieceBesidePawn)
                    move.pessant = True
                    move.specialMovePiece = pieceBesidePawn
                    yield move
Exemplo n.º 6
0
    def getPossibleMoves(self):
        currentPosition = self.position

        # Движение пешки вперед на 1 клетку
        movement = C(0, 1) if self.side == WHITE else C(0, -1)
        advanceOnePosition = currentPosition + movement
        if self.board.isValidPos(advanceOnePosition):
            # Продвижение
            if self.board.pieceAtPosition(advanceOnePosition) is None:
                col = advanceOnePosition[1]
                if col == 7 or col == 0:
                    piecesForPromotion = \
                        [Rook(self.board, self.side, advanceOnePosition),
                         Knight(self.board, self.side, advanceOnePosition),
                         Bishop(self.board, self.side, advanceOnePosition),
                         Queen(self.board, self.side, advanceOnePosition)]
                    for piece in piecesForPromotion:
                        move = Move(self, advanceOnePosition)
                        move.promotion = True
                        move.specialMovePiece = piece
                        yield move
                else:
                    yield Move(self, advanceOnePosition)

        # Пешка ходит вперед на 2 клетки
        if self.movesMade == 0:
            movement = C(0, 2) if self.side == WHITE else C(0, -2)
            advanceTwoPosition = currentPosition + movement
            if self.board.isValidPos(advanceTwoPosition):
                if self.board.pieceAtPosition(advanceTwoPosition) is None and \
                   self.board.pieceAtPosition(advanceOnePosition) is None:
                    yield Move(self, advanceTwoPosition)

        # Пешка ест фигуру противника
        movements = [C(1, 1), C(-1, 1)] \
            if self.side == WHITE else [C(1, -1), C(-1, -1)]

        for movement in movements:
            newPosition = self.position + movement
            if self.board.isValidPos(newPosition):
                pieceToTake = self.board.pieceAtPosition(newPosition)
                if pieceToTake and pieceToTake.side != self.side:
                    col = newPosition[1]
                    # достижения другого конца доски, выбор новой фигуры
                    if col == 7 or col == 0:
                        piecesForPromotion = \
                            [Rook(self.board, self.side, newPosition),
                             Knight(self.board, self.side, newPosition),
                             Bishop(self.board, self.side, newPosition),
                             Queen(self.board, self.side, newPosition)]
                        for piece in piecesForPromotion:
                            move = Move(self, advanceOnePosition)
                            move.promotion = True
                            move.specialMovePiece = piece
                            yield move
                    else:
                        yield Move(self, newPosition,
                                   pieceToCapture=pieceToTake)

        #
        movements = [C(1, 1), C(-1, 1)] \
            if self.side == WHITE else [C(1, -1), C(-1, -1)]
        for movement in movements:
            posBesidePawn = self.position + C(movement[0], 0)
            if self.board.isValidPos(posBesidePawn):
                pieceBesidePawn = self.board.pieceAtPosition(posBesidePawn)
                lastPieceMoved = self.board.getLastPieceMoved()
                lastMoveWasAdvanceTwo = False
                lastMove = self.board.getLastMove()

                if lastMove:
                    if lastMove.newPos - lastMove.oldPos == C(0, 2) or \
                       lastMove.newPos - lastMove.oldPos == C(0, -2):
                        lastMoveWasAdvanceTwo = True

                if pieceBesidePawn and \
                   pieceBesidePawn.stringRep == 'p' and \
                   pieceBesidePawn.side != self.side and \
                   lastPieceMoved is pieceBesidePawn and \
                   lastMoveWasAdvanceTwo:
                    move = Move(self, self.position + movement,
                                pieceToCapture=pieceBesidePawn)
                    move.pessant = True
                    move.specialMovePiece = pieceBesidePawn
                    yield move