def getValidMoves(self, vBoard): """Get the valid moves for a Pawn""" currentPosition = self.position if vBoard.getPiece(currentPosition) == self: fileNum = ord(currentPosition[0]) rankNum = int(currentPosition[1]) captures = [ chr(fileNum-1) + str(rankNum + self.color.pawnRankModifier), chr(fileNum +1) + str(rankNum + self.color.pawnRankModifier)] regular = [ chr(fileNum) + str(rankNum + self.color.pawnRankModifier) ] if (not self.moved): regular.append(chr(fileNum) + str(rankNum + (self.color.pawnRankModifier * 2))) validMoves = [] for move in captures: if Util.isCoordValid(move): piece = vBoard.getPiece(move) if piece != None and piece.color != self.color: validMoves.append(move) for move in regular: if Util.isCoordValid(move): piece = vBoard.getPiece(move) if piece == None: validMoves.append(move) else: #If there is a piece in our regular move path do not continue to the #(possbily) next move break return validMoves
def twoCoordMove(self,firstCoord,secondCoord,promotionAbbreviation=None): """Move a piece, this function takes two chess coordinates and an optional Piece to use for promotion if necessary, the first being the starting square of the piece to move and the second being the ending square of the move.\n In order to perform a castle move, move the king to the final position required for the castle.""" moves = [firstCoord, secondCoord] if promotionAbbreviation != None: if promotionAbbreviation not in Util.invPieces: self.lastError = "A valid piece abbreviation must be given for promotions." return False else: promotionAbbreviation = Util.invPieces[promotionAbbreviation] else: promotionAbbreviation = "" for move in moves: if not Util.isCoordValid(move): self.lastError = "Two valid chess coordinates must be given." return False currentPlayer = self._getNextPlayer() if currentPlayer.move(moves[0], moves[1], promotionAbbreviation): self.gameBoard.setTurn(self.whitePlayer, self.blackPlayer) self.moveList.append(currentPlayer.lastMoveString) return True else: self.lastError = "Move Failed:\n"+currentPlayer.moveResultReason return False
def getDestinationStringFromClassAndValidate(self, classValue): destLen = len(classValue) if destLen > 2 or destLen == 0: self.valid = False elif destLen == 2 and not Util.isCoordValid(classValue): self.valid = False return classValue
def getDisambiguationStringFromClassAndValidate(self, classValue): disambigLen = len(classValue) if disambigLen > 2: self.valid = False elif disambigLen == 2 and not Util.isCoordValid(classValue): self.valid = False elif disambigLen == 1 and not (classValue in Util.ranks or classValue in Util.files): self.valid = False return classValue
def move(self,coord): """Attempt to move this piece, it will fail if the movement places it outside the board or if it does not have an initial position""" self.moveResultReason = "Success" if self.placed: if Util.isCoordValid(coord): self.lastMove = (self.position, self.moved) self.position = coord self.moved = True return True self.moveResultReason = "Destination is not a valid chess square." return False self.moveResultReason = "Piece has not been placed on the board." return False
def __init__(self,piece="",color="",position=""): self.position = "" self.color = Util.colors.NONE self.piece = "" self.placed = False self.moved = False self.lastMove = () self.moveResultReason = "Success" if Util.isCoordValid(position): self.position = position self.placed = True if color in [Util.colors.WHITE, Util.colors.BLACK]: self.color = color if piece in Util.pieces: self.piece = piece
def _getMoveValidityAndTermination(self, vBoard, coord): """Returns whether or not the move is valid, and if we should stop looking""" stopLooking = True valid = True if not Util.isCoordValid(coord): return False, stopLooking piece = vBoard.getPiece(coord) #If there is no piece here than it is a valid move and we can keep looking #If the color is not ours then it is a capture move, but still valid, but we cannot go past #If the color is ours then we need to stop if piece == None: stopLooking = False elif piece.color != self.color: pass else: valid = False return valid, stopLooking
def move(self,coord): """Attempt to move this piece, it will fail if the movement places it outside the board or if it does not have an initial position""" self.moveResultReason = "Success" if self.placed: if Util.isCoordValid(coord): self.lastState = (self.position, self.moved, self.enPassantCapturable) if self.color.pawnRank in self.position and self.color.pawnChargeRank in coord: self.enPassantCapturable = True else: self.enPassantCapturable = False self.position = coord self.moved = True return True self.moveResultReason = "Destination is not a valid chess square." return False self.moveResultReason = "Piece has not been placed on the board." return False
def getPiece(self, coordinate): """Return the piece found at the coordinate or None if the coordinate is not valid""" if Util.isCoordValid(coordinate): return self.board[coordinate][1]
def getCaptureCoords(self): fileNum = ord(self.position[0]) rankNum = int(self.position[1]) return [coord for coord in [chr(fileNum-1) + str(rankNum + self.color.pawnRankModifier), chr(fileNum +1) + str(rankNum + self.color.pawnRankModifier)] if Util.isCoordValid(coord)]