예제 #1
0
 def addPiece(self, piece, team, mailbox):
     piece = str.capitalize(piece)
     team = str.lower(team)
     if type(mailbox) is str:
         mailbox = Tile.coordinateToMailbox(mailbox)
     if (piece == 'P'):
         self.board[mailbox].piece = Pawn(team, mailbox)
         print("Successfully added", team, "Pawn to",
               Tile.mailboxToCoordinate(mailbox))
     elif (piece == 'R'):
         self.board[mailbox].piece = Rook(team, mailbox)
         print("Successfully added", team, "Rook to",
               Tile.mailboxToCoordinate(mailbox))
     elif (piece == 'N'):
         self.board[mailbox].piece = Knight(team, mailbox)
         print("Successfully added", team, "Knight to",
               Tile.mailboxToCoordinate(mailbox))
     elif (piece == 'B'):
         self.board[mailbox].piece = Bishop(team, mailbox)
         print("Successfully added", team, "Bishop to",
               Tile.mailboxToCoordinate(mailbox))
     elif (piece == 'Q'):
         self.board[mailbox].piece = Queen(team, mailbox)
         print("Successfully added", team, "Queen to",
               Tile.mailboxToCoordinate(mailbox))
     elif (piece == 'K'):
         self.board[mailbox].piece = King(team, mailbox)
         print("Successfully added", team, "King to",
               Tile.mailboxToCoordinate(mailbox))
     else:
         print("Incorrect piece code")
예제 #2
0
    def boardFromFEN(self, FEN):
        self.clearBoard()
        FEN = self.formatFEN(FEN)
        print(FEN)
        index = 0
        for section in FEN:
            if index < 8:
                target = 91 - (index * 10)  # FEN moves from top to bottom
                for piece in section:
                    try:
                        piece = int(
                            piece
                        )  # If value is empty space(s), move the target by that much
                        target += piece
                        continue
                    except Exception:
                        pass

                    if piece.islower():
                        self.addPiece(piece, 'black', target)
                        target += 1
                    else:
                        self.addPiece(piece, 'white', target)
                        target += 1
            elif index == 8:
                if FEN[8] == 'w':
                    self.playerTurnToPlay = 'white'
                else:
                    self.playerTurnToPlay = 'black'
            elif index == 9:
                castlingRights = FEN[9]  # Optimized variable usage
                for letter in castlingRights:
                    if letter == '-':
                        self.kingMoved['white'] = True
                        self.kingMoved['black'] = True
                    elif letter == 'K':
                        self.rookMoved['white'][
                            'kingside'] = False  # Castling is handled in an orderly KQkq or - format in the FEN, see FEN notation reference
                    elif letter == 'Q':  # Here the rooks being moved might be a false statement depending on the board state,
                        self.rookMoved['white'][
                            'queenside'] = False  # but it simplifies the meaning and results in an equivalent board state
                    elif letter == 'k':
                        self.rookMoved['black']['kingside'] = False
                    elif letter == 'q':
                        self.rookMoved['black']['kingside'] = False
            elif index == 10:
                if FEN[10] == '-':
                    self.enPassantSquare = None
                else:
                    self.enPassantSquare = Tile.coordinateToMailbox(FEN[10])
            elif index == 11:
                self.halfmoveClock = FEN[
                    11]  # Halfmove clock is number of halfmoves since last capture or pawn advance, can be used for draw under 50-move rule
            elif index == 12:
                self.currentTurn = FEN[12]
            index += 1
예제 #3
0
    def movePiece(self, mailbox, destination):
        mailbox = Tile.coordinateToMailbox(mailbox)
        destination = Tile.coordinateToMailbox(destination)
        movingPiece = self.board[mailbox].piece
        if movingPiece is None:
            print('Illegal Move, chosen tile is empty.')
            return False
        if movingPiece.team != self.playerTurnToPlay:
            print('Illegal Move, piece is not yours to move.')
            return False
        targetPiece = self.board[destination].piece
        for (origin, moveType, target) in movingPiece.listAllLegalMoves():
            if destination == target:
                movingPiece.mailbox = destination

                if moveType == 'move':
                    self.board[destination].piece = movingPiece
                    self.board[mailbox].piece = None

                elif moveType == 'doubleMove':
                    self.board[destination].piece = movingPiece
                    self.board[mailbox].piece = None
                    if movingPiece.team == 'white':
                        self.enPassantSquare = destination - 10
                    else:
                        self.enPassantSquare = destination + 10
                    self.generateEnPassantMoves()

                elif moveType == 'capture':
                    self.deadPieces[targetPiece.team][targetPiece.name] += 1
                    self.board[destination].piece = movingPiece
                    self.board[mailbox].piece = None
                    self.pieceList[targetPiece.team][targetPiece.name]

                elif moveType == 'castle':
                    if movingPiece.team == 'white':
                        if destination == 23:
                            targetRook = self.board[21].piece
                            targetRook.mailbox = 23
                            self.board[destination].piece = movingPiece
                            self.board[mailbox].piece = None
                            self.board[23].piece = targetRook
                            self.board[21].piece = None
                        elif destination == 27:
                            targetRook = self.board[28].piece
                            targetRook.mailbox = 25
                            self.board[destination].piece = movingPiece
                            self.board[mailbox].piece = None
                            self.board[25].piece = targetRook
                            self.board[28].piece = None

                    elif movingPiece.team == 'black':
                        if destination == 93:
                            targetRook = self.board[91].piece
                            targetRook.mailbox = 93
                            self.board[destination].piece = movingPiece
                            self.board[mailbox].piece = None
                            self.board[93].piece = targetRook
                            self.board[91].piece = None
                        elif destination == 97:
                            targetRook = self.board[98].piece
                            targetRook.mailbox = 95
                            self.board[destination].piece = movingPiece
                            self.board[mailbox].piece = None
                            self.board[95].piece = targetRook
                            self.board[98].piece = None

                elif moveType == 'enPassant':
                    if movingPiece.team == 'white':  # If a white pawn en passant captures a black one
                        targetPiece = self.board[destination - 10].piece
                        self.board[destination].piece = movingPiece
                        self.board[mailbox].piece = None
                        self.board[destination - 10].piece = None
                        self.deadPieces[targetPiece.team][
                            targetPiece.name] += 1

                    elif movingPiece.team == 'black':  # If a black pawn en passant captures a white one
                        targetPiece = self.board[destination + 10].piece
                        self.board[destination].piece = movingPiece
                        self.board[mailbox].piece = None
                        self.board[destination + 10].piece = None
                        self.deadPieces[targetPiece.team][
                            targetPiece.name] += 1

                return True  # True for success, false for failure
        print('Illegal Move.')
        return False
예제 #4
0
 def clearTile(self, coordinate):
     if type(coordinate) is str:
         mailbox = Tile.coordinateToMailbox(coordinate)
     self.board[mailbox].piece = None
     print("Tile", coordinate, "cleared successfully.")
예제 #5
0
 def parseMoveRequest(request):
     request = request.split(' ')
     request = [Tile.coordinateToMailbox(x) for x in request]
     return request