示例#1
0
 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
示例#2
0
   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
示例#3
0
 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
示例#4
0
 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
示例#5
0
 def getMoves(self):
    moveNumber = "1."
    validMoves = True
    while validMoves:
       if moveNumber in self.moves:
          yield self.moves[moveNumber]
       else:
          validMoves = False
          break
       moveNumber = Util.getNextTurnString(moveNumber)
    if not validMoves:
       raise StopIteration()
示例#6
0
 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
示例#7
0
 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
示例#8
0
 def getTurnString(self,turn="current"):
    """Returns a string informing of the current round and player"""
    turnString = "Invalid"
    if turn in ("current","first","pending","last"):
       if turn == "current":
          turn = self.currentTurn
       elif turn == "first":
          turn = self.initialSetup
       elif turn == "last":
          turn = self.pendingTurn - 1
       else:
          turn = self.pendingTurn
    if turn == self.initialSetup:
       turnString = "0"
    else:
       turnString = Util.getTurnStringFromOnesBasedIndex(turn)
    return turnString
示例#9
0
 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
示例#10
0
 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
示例#11
0
 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]
示例#12
0
 def saveMove(self, moveSan):
    moveNumber = Util.getNextTurnString(self.currentGame.lastMove.number)
    self.currentGame.saveMove(moveNumber, moveSan)
示例#13
0
 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)]