Esempio n. 1
0
 def play(self, fixed_move=None):
     col = self.fixed_move(fixed_move)
     if col != -1:
         m = Move(self.name, self.board, col)
         m.play()
         win = self.board.check_connect(m)
         return m, win
     else:
         self.board.playing = False
         self.board.full = True
Esempio n. 2
0
 def getPossibleMoves(self):
     board = self.board
     currentPos = self.position
     movements = [C(2, 1), C(2, -1), C(-2, 1), C(-2, -1), C(1, 2),
                  C(1, -2), C(-1, -2), C(-1, 2)]
     for movement in movements:
         newPos = currentPos + movement
         if board.isValidPos(newPos):
             pieceAtNewPos = board.pieceAtPosition(newPos)
             if pieceAtNewPos is None:
                 yield Move(self, newPos)
             elif pieceAtNewPos.side != self.side:
                 yield Move(self, newPos, pieceToCapture=pieceAtNewPos)
Esempio n. 3
0
    def movesInDirectionFromPos(self, pos, direction, side):
        for dis in range(1, 8):
            movement = C(dis * direction[X], dis * direction[Y])
            newPos = pos + movement
            if self.board.isValidPos(newPos):
                pieceAtNewPos = self.board.pieceAtPosition(newPos)
                if pieceAtNewPos is None:
                    yield Move(self, newPos)

                elif pieceAtNewPos is not None:
                    if pieceAtNewPos.side != side:
                        yield Move(self, newPos, pieceToCapture=pieceAtNewPos)
                    return
Esempio n. 4
0
 def play(self, fixed_move=None):
     if fixed_move is None:
         col = self.rlmove()
     else:
         col = fixed_move
     if col != -1:
         m = Move(self.name, self.board, col)
         m.play()
         # print(f'Player {self.name} played column {col}')
         win = self.board.check_connect(m)
         # if result: print(result)
         return m, win
     else:
         self.board.playing = False
         self.board.full = True
Esempio n. 5
0
    def __init__(self,
                 mateInOne=False,
                 castleBoard=False,
                 passant=False,
                 promotion=False):
        self.pieces = []
        self.history = []
        self.points = 0
        self.currentSide = WHITE
        self.movesMade = 0
        self.checkmate = False

        if not mateInOne and not castleBoard and not passant and not promotion:
            self.pieces.extend([
                Rook(self, BLACK, C(0, 7)),
                Knight(self, BLACK, C(1, 7)),
                Bishop(self, BLACK, C(2, 7)),
                Queen(self, BLACK, C(3, 7)),
                King(self, BLACK, C(4, 7)),
                Bishop(self, BLACK, C(5, 7)),
                Knight(self, BLACK, C(6, 7)),
                Rook(self, BLACK, C(7, 7))
            ])
            for x in range(8):
                self.pieces.append(Pawn(self, BLACK, C(x, 6)))
            for x in range(8):
                self.pieces.append(Pawn(self, WHITE, C(x, 1)))
            self.pieces.extend([
                Rook(self, WHITE, C(0, 0)),
                Knight(self, WHITE, C(1, 0)),
                Bishop(self, WHITE, C(2, 0)),
                Queen(self, WHITE, C(3, 0)),
                King(self, WHITE, C(4, 0)),
                Bishop(self, WHITE, C(5, 0)),
                Knight(self, WHITE, C(6, 0)),
                Rook(self, WHITE, C(7, 0))
            ])

        elif promotion:
            pawnToPromote = Pawn(self, WHITE, C(1, 6))
            pawnToPromote.movesMade = 1
            kingWhite = King(self, WHITE, C(4, 0))
            kingBlack = King(self, BLACK, C(3, 2))
            self.pieces.extend([pawnToPromote, kingWhite, kingBlack])

        elif passant:
            pawn = Pawn(self, WHITE, C(1, 4))
            pawn2 = Pawn(self, BLACK, C(2, 6))
            kingWhite = King(self, WHITE, C(4, 0))
            kingBlack = King(self, BLACK, C(3, 2))
            self.pieces.extend([pawn, pawn2, kingWhite, kingBlack])
            self.history = []
            self.currentSide = BLACK
            self.points = 0
            self.movesMade = 0
            self.checkmate = False
            firstMove = Move(pawn2, C(2, 4))
            self.makeMove(firstMove)
            self.currentSide = WHITE
            return
Esempio n. 6
0
    def getPossibleMoves(self):
        currentPos = self.position
        movements = [
            C(0, 1),
            C(0, -1),
            C(1, 0),
            C(-1, 0),
            C(1, 1),
            C(1, -1),
            C(-1, 1),
            C(-1, -1)
        ]
        for movement in movements:
            newPos = currentPos + movement
            if self.board.isValidPos(newPos):
                pieceAtNewPos = self.board.pieceAtPosition(newPos)
                if self.board.pieceAtPosition(newPos) is None:
                    yield Move(self, newPos)
                elif pieceAtNewPos.side != self.side:
                    yield Move(self, newPos, pieceToCapture=pieceAtNewPos)

        # Castling
        if self.movesMade == 0:
            inCheck = False
            kingsideCastleBlocked = False
            queensideCastleBlocked = False
            kingsideCastleCheck = False
            queensideCastleCheck = False
            kingsideRookMoved = True
            queensideRookMoved = True

            kingsideCastlePositions = [
                self.position + C(1, 0), self.position + C(2, 0)
            ]
            for pos in kingsideCastlePositions:
                if self.board.pieceAtPosition(pos):
                    kingsideCastleBlocked = True
                    break

            queensideCastlePositions = [
                self.position - C(1, 0), self.position - C(2, 0),
                self.position - C(3, 0)
            ]
            for pos in queensideCastlePositions:
                if self.board.pieceAtPosition(pos):
                    queensideCastleBlocked = True
                    break

            if kingsideCastleBlocked and queensideCastleBlocked:
                return

            otherSideMoves = \
                self.board.getAllMovesUnfiltered(not self.side,
                                                 includeKing=False)
            for move in otherSideMoves:
                if move.newPos == self.position:
                    inCheck = True
                    break
                if move.newPos == self.position + C(1, 0) or \
                   move.newPos == self.position + C(2, 0):
                    kingsideCastleCheck = True
                if move.newPos == self.position - C(1, 0) or \
                   move.newPos == self.position - C(2, 0):
                    queensideCastleCheck = True

            kingsideRookPos = self.position + C(3, 0)
            kingsideRook = self.board.pieceAtPosition(kingsideRookPos) \
                if self.board.isValidPos(kingsideRookPos) \
                else None
            if kingsideRook and \
               kingsideRook.stringRep == 'R' and \
               kingsideRook.movesMade == 0:
                kingsideRookMoved = False

            queensideRookPos = self.position - C(4, 0)
            queensideRook = self.board.pieceAtPosition(queensideRookPos) \
                if self.board.isValidPos(queensideRookPos) \
                else None
            if queensideRook and \
               queensideRook.stringRep == 'R' and \
               queensideRook.movesMade == 0:
                queensideRookMoved = False

            if not inCheck:
                if not kingsideCastleBlocked and \
                   not kingsideCastleCheck and \
                   not kingsideRookMoved:
                    move = Move(self, self.position + C(2, 0))
                    rookMove = Move(kingsideRook, self.position + C(1, 0))
                    move.specialMovePiece = \
                        self.board.pieceAtPosition(kingsideRookPos)
                    move.kingsideCastle = True
                    move.rookMove = rookMove
                    yield move
                if not queensideCastleBlocked and \
                   not queensideCastleCheck and \
                   not queensideRookMoved:
                    move = Move(self, self.position - C(2, 0))
                    rookMove = Move(queensideRook, self.position - C(1, 0))
                    move.specialMovePiece = \
                        self.board.pieceAtPosition(queensideRookPos)
                    move.queensideCastle = True
                    move.rookMove = rookMove
                    yield move
Esempio n. 7
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, newPosition, pieceToCapture=pieceToTake)
                            move.promotion = True
                            move.specialMovePiece = piece
                            yield move
                    else:
                        yield Move(self, newPosition,
                                   pieceToCapture=pieceToTake)

        # En passant
        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.passant = True
                    move.specialMovePiece = pieceBesidePawn
                    yield move