def __is_castling(self, _from, to): y_from = _from[1] y_to = to[1] x_rook = 'a' between = ['b', 'c', 'd'] if x.index(_from[0]) - x.index(to[0]) == -2: x_rook = 'h' between = ['f', 'g'] king = self.squares[_from] rook = self.squares[x_rook + y_to] if not isinstance(rook, Rook): return False if y_from != y_to: return False if x.index(_from[0]) - x.index(to[0]) not in [-2, 2]: return False if king.moved or rook.moved: return False if king.color != rook.color: return False for _x in between: if self.squares[_x + y_to] is not None: return False return True
def __is_castling(self, source, destination): y_source = source[1] y_destination = destination[1] x_rook = 'a' between = ['b', 'c', 'd'] if x.index(source[0]) - x.index(destination[0]) == -2: x_rook = 'h' between = ['f', 'g'] king = self.get_piece(source) rook = self.get_piece(x_rook + y_destination) if not isinstance(rook, Rook): return False if y_source != y_destination: return False if x.index(source[0]) - x.index(destination[0]) not in [-2, 2]: return False if king.moved or rook.moved: return False if king.color != rook.color: return False for _x in between: if self.get_piece(_x + y_destination) is not None: return False return True
def can_move_vertically_or_horizontally(self, source, destination): horizontal = x.index(source[0]) == x.index( destination[0]) and y.index(source[1]) != y.index(destination[1]) vertical = x.index(source[0]) != x.index(destination[0]) and y.index( source[1]) == y.index(destination[1]) return horizontal or vertical
def can_move(self, source, destination): x_distance = abs(x.index(source[0]) - x.index(destination[0])) y_distance = abs(y.index(source[1]) - y.index(destination[1])) return x_distance in [0, 1] and y_distance in [ 0, 1 ] and x_distance + y_distance != 0
def can_move(self, _from, to): self.moved = True x_distance = abs(x.index(_from[0]) - x.index(to[0])) y_distance = abs(y.index(_from[1]) - y.index(to[1])) return bool(x_distance in [0, 1] and y_distance in [0, 1] and x_distance + y_distance != 0)
def can_move(self, _from, to): self.moved = True x_distance = abs(x.index(_from[0]) - x.index(to[0])) y_distance = abs(y.index(_from[1]) - y.index(to[1])) return x_distance in [1, 2] and y_distance in [ 1, 2 ] and x_distance != y_distance
def can_move(self, _from, to): self.moved = True horizontal = x.index(_from[0]) == x.index( to[0]) and y.index(_from[1]) != y.index(to[1]) vertical = x.index(_from[0]) != x.index(to[0]) and y.index( _from[1]) == y.index(to[1]) return bool(horizontal or vertical)
def can_move(self, _from, to): self.moved = True if (self.color == 'white' and x.index(_from[0]) == x.index(to[0]) and y.index(to[1]) - y.index(_from[1]) == 1): return True return (self.color == 'black' and x.index(_from[0]) == x.index(to[0]) and y.index(to[1]) - y.index(_from[1]) == -1)
def __move_castling(self, _from, to): y_to = to[1] x_rook = 'a' x_rook_to = 'd' if x.index(_from[0]) - x.index(to[0]) == -2: x_rook = 'h' x_rook_to = 'f' self.squares[to], self.squares[x_rook_to + y_to] = self.squares[_from], self.squares[x_rook + y_to] self.squares[_from], self.squares[x_rook + y_to] = None, None
def can_move(self, source, destination): if self.color == 'white' and x.index(source[0]) == x.index(destination[0]): ordinary_move = y.index(destination[1]) - y.index(source[1]) == 1 two_square_move = not self.moved and y.index(destination[1]) - y.index(source[1]) == 2 return ordinary_move or two_square_move if self.color == 'black' and x.index(source[0]) == x.index(destination[0]): ordinary_move = y.index(destination[1]) - y.index(source[1]) == -1 two_square_move = not self.moved and y.index(destination[1]) - y.index(source[1]) == -2 return ordinary_move or two_square_move return False
def __move_castling(self, _from, to): y_to = to[1] x_rook = 'a' x_rook_to = 'd' if x.index(_from[0]) - x.index(to[0]) == -2: x_rook = 'h' x_rook_to = 'f' self.squares[to], self.squares[ x_rook_to + y_to] = self.squares[_from], self.squares[x_rook + y_to] self.squares[_from], self.squares[x_rook + y_to] = None, None
def __move_castling(self, source, destination): y_destination = destination[1] x_rook = 'a' x_rook_to = 'd' if x.index(source[0]) - x.index(destination[0]) == -2: x_rook = 'h' x_rook_to = 'f' self.__squares[destination], self.__squares[x_rook_to + y_destination] = \ self.get_piece(source), self.get_piece(x_rook + y_destination) self.__squares[source], self.__squares[x_rook + y_destination] = None, None self.get_piece(destination).moved = True self.get_piece(x_rook_to + y_destination).moved = True
def can_move(self, source, destination): if self.color == 'white' and x.index(source[0]) == x.index( destination[0]): ordinary_move = y.index(destination[1]) - y.index(source[1]) == 1 two_square_move = not self.moved and y.index( destination[1]) - y.index(source[1]) == 2 return ordinary_move or two_square_move if self.color == 'black' and x.index(source[0]) == x.index( destination[0]): ordinary_move = y.index(destination[1]) - y.index(source[1]) == -1 two_square_move = not self.moved and y.index( destination[1]) - y.index(source[1]) == -2 return ordinary_move or two_square_move return False
def can_move(self, _from, to): self.moved = True # move as a Rook {{ horizontal = x.index(_from[0]) == x.index( to[0]) and y.index(_from[1]) != y.index(to[1]) vertical = x.index(_from[0]) != x.index(to[0]) and y.index( _from[1]) == y.index(to[1]) if horizontal or vertical: return True # }} # move as a Bishop {{ return abs(x.index(_from[0]) - x.index(to[0])) == abs(y.index(to[1]) - y.index(_from[1]))
def can_move_diagonally(self, source, destination): return abs(x.index(source[0]) - x.index(destination[0])) == abs(y.index(destination[1]) - y.index(source[1]))
def can_move(self, source, destination): x_distance = abs(x.index(source[0]) - x.index(destination[0])) y_distance = abs(y.index(source[1]) - y.index(destination[1])) return x_distance in [1, 2] and y_distance in [1, 2] and x_distance != y_distance
def __is_valid_pawn_move(self, _from, to): if not self.squares[_from].moved: return (x.index(_from[0]) == x.index(to[0]) and abs(y.index(to[1]) - y.index(_from[1])) == 2) return False
def can_move_diagonally(self, source, destination): return abs(x.index(source[0]) - x.index(destination[0])) == abs( y.index(destination[1]) - y.index(source[1]))
def can_move(self, _from, to): self.moved = True return abs(x.index(_from[0]) - x.index(to[0])) == abs(y.index(to[1]) - y.index(_from[1]))
def can_move_vertically_or_horizontally(self, source, destination): horizontal = x.index(source[0]) == x.index(destination[0]) and y.index(source[1]) != y.index(destination[1]) vertical = x.index(source[0]) != x.index(destination[0]) and y.index(source[1]) == y.index(destination[1]) return horizontal or vertical