def move_piece(self, _from: Coordinate, _to: Coordinate): """Moves whichever piece is found in _from to _to positions. If no piece found, nothing is done. It does not check against valid chess moves. """ src: Square = get_square(_from) dst: Square = get_square(_to) piece: Optional[Piece] = self[src] if not piece: # No piece found return self[src] = None self[dst] = piece piece.move(dst)
def path(self, _from: Coordinate, _to: Coordinate) -> List[Square]: """Returns the ordered list of squares that conform the shortest path between 2 board coordinates. """ src: Square = get_square(_from) dst: Square = get_square(_to) path = [] while src != dst: for attr in ("row", "col"): srcx = getattr(src, attr) dstx = getattr(dst, attr) if srcx == dstx: continue if srcx > dstx: setattr(src, attr, srcx - 1) else: setattr(src, attr, srcx + 1) path.append(copy(src)) return path
def move(self, pos: Coordinate) -> None: _pos: Square = get_square(pos) self.pos = _pos
def __init__(self, color: Color, board, pos: Coordinate): self.color = color self.board = board self.pos: Square = get_square(pos) self.init()
def __setitem__(self, pos: Coordinate, piece: Optional[Piece]) -> None: sq = get_square(pos) if piece: piece.board = self piece.move(sq) self._board[sq] = piece
def __getitem__(self, pos: Coordinate) -> Optional[Piece]: return self._board[get_square(pos)]