def is_threatened(cls, attacking_piece, attacking_row, attacking_column, row, column): """Override ChessPiece is_threatened abstract class method.""" if diagonals_danger(attacking_row, attacking_column, row, column): return True if cls._check_common_threats(attacking_piece, attacking_row, attacking_column, row, column): return True return False
def _check_common_threats(cls, attacking_piece, attacking_row, attacking_column, row, column): """Check for threats from already allocated pieces. Arguments: attacking_piece -- A class that extends ChessPiece attacking_row -- the row at which the attacking piece is located. attacking_column -- the column at which the attacking piece is located. row -- the row at which the current piece is located. column -- the column at which the current piece is located. """ if attacking_piece.piece_type == PieceType.Rook and \ row_or_column_danger(attacking_row, attacking_column, row, column): return True elif attacking_piece.piece_type == PieceType.Queen and \ (diagonals_danger(attacking_row, attacking_column, row, column) or row_or_column_danger( attacking_row, attacking_column, row, column)): return True elif attacking_piece.piece_type == PieceType.Bishop and \ diagonals_danger(attacking_row, attacking_column, row, column): return True elif attacking_piece.piece_type == PieceType.Knight and knight_danger( attacking_row, attacking_column, row, column): return True elif attacking_piece.piece_type == PieceType.King and king_danger( attacking_row, attacking_column, row, column): return True return False
def is_threatened(cls, attacking_piece, attacking_row, attacking_column, row, column): """Override ChessPiece is_threatened abstract class method.""" if diagonals_danger(attacking_row, attacking_column, row, column) or \ row_or_column_danger(attacking_row, attacking_column, row, column): return True # Queen can only be threatened by a knight, as an optimization, # let it check for knight attacks only. if attacking_piece.piece_type == PieceType.Knight and knight_danger( attacking_row, attacking_column, row, column): return True return False
def test_diagonals_danger_5(self): """Check two pieces are NOT placed on the same diagonal.""" danger = diagonals_danger(self.row, self.column, self.row, self.column - 2) self.assertFalse(danger)
def test_diagonals_danger_4(self): """Check two pieces are placed on the same diagonal.""" danger = diagonals_danger(self.row, self.column, self.row - 2, self.column - 2) self.assertTrue(danger)