Example #1
0
 def _check_en_passant(self):
     if not self._session.last_move:
         raise UserActionError('Invalid en passant attack.')
     last_move = self._session.last_move
     if not isinstance(self.piece, Pawn) or not isinstance(last_move.piece, Pawn):
         raise UserActionError('Invalid en passant attack.')
     if not self._between(
             self.dst.y_label,
             last_move.src.y_label,
             last_move.dst.y_label):
         raise UserActionError('Invalid en passant attack.')
     self._en_passant_attack_dst = last_move.dst
Example #2
0
 def _check_dst_field(self, route):
     dst_piece = self._session.board[self.dst]
     if route.attack_required:
         assert isinstance(self.piece, Pawn)
         if not dst_piece:
             self._check_en_passant()
         elif self.piece.is_white == dst_piece.is_white:
             raise UserActionError('This move has to be an attack.')
     if route.attack_forbidden:
         if dst_piece:
             raise UserActionError('This move can\'t be an attack.')
     if dst_piece and self.piece.is_white == dst_piece.is_white:
         raise UserActionError('You tried to attack your\'s piece.')
Example #3
0
 def _parse_loc_label(self, loc_label):
     try:
         x_label, y_label = loc_label
         x = self._parse_x_label(x_label)
         y = self._parse_y_label(y_label)
     except (ValueError, KeyError):
         raise UserActionError(
             '{!r} is not a valid location label.'.format(loc_label))
     return x_label, y_label, x, y
Example #4
0
 def _set_queen(self, dst):
     if self._queen_resolver.is_usable_field(dst):
         self._queen_resolver.reserve_field(dst, self._queen)
         self.board[dst] = self._queen
         self._queen_count += 1
         if self._queen_count == self.max_queen_count:
             return True
     else:
         raise UserActionError('You cannot place a queen here!')
     return False
Example #5
0
 def execute(self):
     try:
         if not self.can_be_done():
             raise UserActionError
         self._move_the_rook()
     except UserActionError:
         raise UserActionError(self.move_error_msg)
     with critical_part():
         # *no* exception should occur here because
         # self._move_the_rook() mutated the session
         self._move_the_king()
         self._disable_future_castlings()
Example #6
0
 def _check_src_dst_are_distinct(self):
     if self.get_vector() == (0, 0):
         raise UserActionError('You tried to move to the same location.')
Example #7
0
 def _check_if_player_owns_src_piece(self):
     if self.piece.is_white != self._session.is_white_turn:
         raise UserActionError('You tried to move not your piece.')
Example #8
0
 def _check_src_not_empty(self):
     if self.piece is None:
         raise UserActionError('Source location does not contain any figure.')
Example #9
0
 def _check_path(self, route):
     for loc in route.path:
         if self._session.board[loc] is not None:
             raise UserActionError('Other piece on move path.')