Пример #1
0
 def blocked_squares(self, source, target):
     """Returns occupied squares between source and target."""
     rtn = []
     xs = x_of(source)
     ys = y_of(source)
     xt = x_of(target)
     yt = y_of(target)
     if xt > xs:
         dx = 1
     elif xt < xs:
         dx = -1
     else:
         dx = 0
     if yt > ys:
         dy = 1
     elif yt < ys:
         dy = -1
     else:
         dy = 0
     max_slide = max(abs(xs - xt), abs(ys - yt))
     if max_slide > 1:
         for t in range(1, max_slide):
             path = to_square(xs + dx * t, ys + dy * t)
             if self._pieces[path] != c.EMPTY:
                 rtn.append(path)
     return rtn
Пример #2
0
    def path_qubits(self, source: str, target: str) -> List[cirq.Qid]:
        """Returns all entangled qubits (or classical pieces)
        between source and target.

        Source and target should be specified in algebraic notation,
        such as 'f4'.
        """
        rtn = []
        xs = move.x_of(source)
        ys = move.y_of(source)
        xt = move.x_of(target)
        yt = move.y_of(target)
        if xt > xs:
            dx = 1
        elif xt < xs:
            dx = -1
        else:
            dx = 0
        if yt > ys:
            dy = 1
        elif yt < ys:
            dy = -1
        else:
            dy = 0
        max_slide = max(abs(xs - xt), abs(ys - yt))
        if max_slide > 1:
            for t in range(1, max_slide):
                path_bit = xy_to_bit(xs + dx * t, ys + dy * t)
                path_qubit = bit_to_qubit(path_bit)
                if (path_qubit in self.entangled_squares
                        or nth_bit_of(path_bit, self.state)):
                    rtn.append(path_qubit)
        return rtn
Пример #3
0
    def path_qubits(self, source: str, target: str) -> List[cirq.Qid]:
        """Returns all entangled qubits (or classical pieces)
        between source and target.

        Source and target should be in the same line, i.e. same row, 
        same column, or same diagonal.

        Source and target should be specified in algebraic notation,
        such as 'f4'.
        """
        rtn = []
        xs = move.x_of(source)
        ys = move.y_of(source)
        xt = move.x_of(target)
        yt = move.y_of(target)
        if xt > xs:
            dx = 1
        elif xt < xs:
            dx = -1
        else:
            dx = 0
        if yt > ys:
            dy = 1
        elif yt < ys:
            dy = -1
        else:
            dy = 0
        x_slide = abs(xt - xs)
        y_slide = abs(yt - ys)
        # Souce and target should always be in the same line.
        if x_slide != y_slide and x_slide * y_slide:
            raise ValueError(
                'Wrong inputs for path_qubits: source and target are not in the same line.'
            )
        max_slide = max(x_slide, y_slide)
        # Only calculates path when max_slide > 1.
        for t in range(1, max_slide):
            path_bit = xy_to_bit(xs + dx * t, ys + dy * t)
            path_qubit = bit_to_qubit(path_bit)
            if (path_qubit in self.entangled_squares
                    or nth_bit_of(path_bit, self.state)):
                rtn.append(path_qubit)
        return rtn
Пример #4
0
 def _ep_flag(move: 'Move', piece_type: int):
     if abs(piece_type) != c.PAWN:
         return None
     if abs(y_of(move.target) - y_of(move.source)) == 2:
         return x_of(move.target)
     return None
Пример #5
0
def square_to_bit(sq: str) -> int:
    """Transform algebraic square notation into a bitboard bit number."""
    return move.y_of(sq) * 8 + move.x_of(sq)