def canMove(self, newRow, newCol): if (newRow - self.row, newCol - self.col) not in self.moves: return False if (not board.isEmpty(newRow, newCol)) and board.getPiece( newRow, newCol).notation[0] != self.notation[0]: return True if board.isEmpty(newRow, newCol): return True return False
def canMove(self, newRow, newCol): # it can't move there if it's not in the moves set if not (newRow - self.row, newCol - self.col) in self.moves: return False # finding out the direction the piece will go, on the x and y axes signRow = 0 signCol = 0 if self.row != newRow: signRow = int(abs(newRow - self.row) / (newRow - self.row)) if self.col != newCol: signCol = int(abs(newCol - self.col) / (newCol - self.col)) # it can't move if there is a piece between its current and new position # we check every single suare between the current and next position, in the direction that we found out earlier diff = 0 while self.row + signRow * diff != newRow or self.col + signCol * diff != newCol: diff += 1 # if there is a piece between the current and next position if not board.isEmpty(self.row + signRow * diff, self.col + signCol * diff): # if the piece is exactly on the next position and its color is opposite to this piece's, then the piece can capture it if (self.row + signRow * diff == newRow and self.col + signCol * diff == newCol and board.getPiece( newRow, newCol).notation[0] != self.notation[0]): return True # otherwise, it can't move there return False return True
def canCastle(self, row, col): # the king cannot castle if it has already moved or if it's in check if self.hasMoved or self.isInCheck(self.row, self.col): return False # short castle if row == self.row and col == 6: # a lot of conditions have to be met: the king and the rook must not have moved, # there has to be no piece between the king and the rook and every square between the king and the rook has to be safe from # checks from the enemy pieces return (board.isEmpty(self.row, 5) and board.isEmpty(self.row, 6) and not board.isEmpty(self.row, 7) and board.getPiece(self.row, 7).hasMoved == False and not self.isInCheck(self.row, 5) and not self.isInCheck(self.row, 6)) # long castle elif row == self.row and col == 2: return (board.isEmpty(self.row, 3) and board.isEmpty(self.row, 2) and board.isEmpty(self.row, 1) and not board.isEmpty(self.row, 0) and board.getPiece(self.row, 0).hasMoved == False and not self.isInCheck(self.row, 3) and not self.isInCheck(self.row, 2)) return False
def canTakeEnPassant(self, row, col): if not board.isEmpty(row, col): return False # check the position of the pawn and the next position if self.notation[0] == "w": if row != self.row - 1 or abs(col - self.col) != 1: return False if board.isEmpty(self.row, col) or board.getPiece( self.row, col).notation != "bp" or not board.getPiece( self.row, col).hasJustMovedTwo: return False else: if row != self.row + 1 or abs(col - self.col) != 1: return False if board.isEmpty(self.row, col) or board.getPiece( self.row, col).notation != "wp" or not board.getPiece( self.row, col).hasJustMovedTwo: return False otherPawn = board.getPiece(self.row, col) currentRow = self.row currentCol = self.col board.board[self.row][col] = None board.board[row][col] = self self.row = row self.col = col valid = True # if the move puts the pawn's king in check, it's not valid if (self.notation[0] == "w" and whiteKing.isInCheck(whiteKing.row, whiteKing.col) or self.notation[0] == "b" and blackKing.isInCheck(blackKing.row, blackKing.col)): valid = False self.row = currentRow self.col = currentCol board.board[self.row][self.col] = self board.board[row][col] = None board.board[self.row][col] = otherPawn return valid
def canMove(self, newRow, newCol): # if the piece can't attack nor move there, it is not valid if ((newRow - self.row, newCol - self.col) not in self.moves) and ( (newRow - self.row, newCol - self.col) not in self.attackMoves): return False # if that square is not empty if not board.isEmpty(newRow, newCol): # the pawn can capture the piece on that square if the square is in the attackMoves set # and the piece is not of the same color if board.getPiece(newRow, newCol).notation[0] != self.notation[0]: if (newRow - self.row, newCol - self.col) in self.attackMoves: return True # otherwise, the piece is of the same color as this pawn or the pawn is just not allowed to go there return False # if it is in the moves set if (newRow - self.row, newCol - self.col) in self.moves: # after the first move, the 2-square move is not valid anymore if self.notation[0] == "w" and self.col == 6: self.moves = {(-1, 0)} elif self.notation[0] == "b" and self.col == 1: self.moves = {(1, 0)} # if the pawn wants to move 2 squares, there have to be no pieces between those squares if abs(self.row - newRow) == 2: if (self.notation[0] == "w" and not board.isEmpty(self.row - 1, self.col) or self.notation[0] == "b" and not board.isEmpty(self.row + 1, self.col)): return False else: self.hasJustMovedTwo = True return True return False
def canMove(self, newRow, newCol): # if it doesn't move in an L shape, the move isn't valid if (newRow - self.row, newCol - self.col) not in self.moves: return False #if there is a piece on that square, it can capture it if it's of a different color, or otherwise it can't move there if not board.isEmpty(newRow, newCol): if board.getPiece(newRow, newCol).notation[0] != self.notation[0]: return True else: return False return True
def isAttacking(self, row, col): # it is in their moves set if not (row - self.row, col - self.col) in self.moves: return False signRow = 0 signCol = 0 if self.row != row: signRow = int(abs(row - self.row) / (row - self.row)) if self.col != col: signCol = int(abs(col - self.col) / (col - self.col)) # and there is no piece between those 2 squares diff = 1 while self.row + signRow * diff != row or self.col + signCol * diff != col: if not board.isEmpty(self.row + signRow * diff, self.col + signCol * diff): return False diff += 1 return True
def __init__(self, value): self.value = value if board.isEmpty(): board.move((1, 1), self.value)