def test_queen_threat_blocking_pieces(self): queen_white = Queen(ChessCoord('E', '6'), white) rook_white = Rook(ChessCoord('E', '7'), white) pawn_black = Pawn(ChessCoord('A', '1'), black, go_south) pieces = [queen_white, pawn_black, rook_white] queen_white.analyze_threats_on_board_for_new_move(pieces, ChessCoord('E', '5')) expected_squares_chess = [ ChessCoord('E', '6'), # north # rest blocked ChessCoord('E', '4'), # south ChessCoord('E', '3'), ChessCoord('E', '2'), ChessCoord('E', '1'), ChessCoord('D', '5'), # east ChessCoord('C', '5'), ChessCoord('B', '5'), ChessCoord('A', '5'), ChessCoord('F', '5'), # west ChessCoord('G', '5'), ChessCoord('H', '5'), ChessCoord('D', '4'), # south west ChessCoord('C', '3'), ChessCoord('B', '2'), # blocked: ChessCoord('A', '1'), ChessCoord('D', '6'), # north west ChessCoord('C', '7'), ChessCoord('B', '8'), ChessCoord('F', '6'), # north east ChessCoord('G', '7'), ChessCoord('H', '8'), ChessCoord('F', '4'), # south east ChessCoord('G', '3'), ChessCoord('H', '2'), ] expected_squares_grid = map(chess_coord_to_grid_coord, expected_squares_chess) self.failUnless(util.compare_lists(expected_squares_grid, queen_white.is_threat_to_these_squares))
def test_queen_threat_empty_board(self): queen_white = Queen(ChessCoord('E', '6'), white) pieces = [queen_white] queen_white.analyze_threats_on_board_for_new_move(pieces, ChessCoord('E', '5')) expected_squares_chess = [ ChessCoord('E', '6'), # north ChessCoord('E', '7'), ChessCoord('E', '8'), ChessCoord('E', '4'), # south ChessCoord('E', '3'), ChessCoord('E', '2'), ChessCoord('E', '1'), ChessCoord('D', '5'), # east ChessCoord('C', '5'), ChessCoord('B', '5'), ChessCoord('A', '5'), ChessCoord('F', '5'), # west ChessCoord('G', '5'), ChessCoord('H', '5'), ChessCoord('D', '4'), # south west ChessCoord('C', '3'), ChessCoord('B', '2'), ChessCoord('A', '1'), ChessCoord('D', '6'), # north west ChessCoord('C', '7'), ChessCoord('B', '8'), ChessCoord('F', '6'), # north east ChessCoord('G', '7'), ChessCoord('H', '8'), ChessCoord('F', '4'), # south east ChessCoord('G', '3'), ChessCoord('H', '2'), ] expected_squares_grid = map(chess_coord_to_grid_coord, expected_squares_chess) self.failUnless(util.compare_lists(expected_squares_grid, queen_white.is_threat_to_these_squares))
class ThreatTests(unittest.TestCase): def setUp(self): self.queen_white = Queen(ChessCoord('E', '6'), white) self.queen_black = Queen(ChessCoord('G', '5'), black) self.rook_white = Rook(ChessCoord('D', '1'), white) self.knight_white = Knight(ChessCoord('G', '1'), white) self.pieces = [self.queen_white, self.queen_black, self.rook_white, self.knight_white] def test_queens_threatening_each_other(self): self.queen_white.analyze_threats_on_board_for_new_move(self.pieces, ChessCoord('E', '5')) self.failUnless(self.queen_black.is_threat_to_these_pieces == [self.queen_white, self.knight_white]) self.failUnless(self.queen_white.is_threat_to_these_pieces == [self.queen_black]) def test_queen_threatening_rook_but_not_vice_versa(self): self.queen_black.analyze_threats_on_board_for_new_move(self.pieces, ChessCoord('G', '4')) self.failUnless(self.queen_black.is_threat_to_these_pieces == [self.queen_white, self.rook_white, self.knight_white]) self.failUnless(self.queen_white.is_threat_to_these_pieces == [self.queen_black]) self.failUnless(self.rook_white.is_threat_to_these_pieces == []) def test_knight_threatening_queen_but_not_vice_versa(self): self.knight_white.analyze_threats_on_board_for_new_move(self.pieces, ChessCoord('F', '3')) self.failUnless(self.knight_white.is_threat_to_these_pieces == [self.queen_black]) self.failUnless(self.queen_black.is_threat_to_these_pieces == []) def test_knight_threatening_none(self): pieces = self.pieces + [Pawn(ChessCoord('C', '3'), black, go_north), Knight(ChessCoord('C', '1'), black), ] self.knight_white.update_coord(ChessCoord('A', '4')) self.knight_white.analyze_threats_on_board_for_new_move(pieces, ChessCoord('B', '2')) self.failUnless(self.knight_white.is_threat_to_these_pieces == []) def test_knight_threatening_none_with_all_pieces(self): pieces = copy.deepcopy(starting_pieces) g8_knight = select_piece(ChessCoord('G', '8'), pieces) e2_pawn = select_piece(ChessCoord('E', '2'), pieces) e2_pawn.analyze_threats_on_board_for_new_move(pieces, ChessCoord('E', '4')) self.failUnless(g8_knight.is_threat_to_these_pieces == []) def test_king_only_threatens_for_one_step_with_all_pieces(self): pieces = copy.deepcopy(starting_pieces) e1_king = select_piece(ChessCoord('E', '1'), pieces) e1_king.analyze_threats_on_board_for_new_move(pieces, ChessCoord('E', '6')) pawns_d7_e7_f7 = select_pieces([ChessCoord('D', '7'), ChessCoord('E', '7'), ChessCoord('F', '7')], pieces) self.failUnless(util.compare_lists(e1_king.is_threat_to_these_pieces, pawns_d7_e7_f7)) def test_pieces_threatens_none_from_start_position(self): pieces = copy.deepcopy(starting_pieces) map(lambda piece: piece.analyze_threats_on_board_for_new_move(pieces, piece.chess_coord ), pieces) all_threats = map(lambda piece: piece.is_threat_to_these_pieces, pieces) should_be_all_empty = filter(lambda threat_list: threat_list is not [], all_threats) should_be_all_empty_filter_away_empty = map(lambda sbae: sbae is not [], should_be_all_empty) self.failIf(False in should_be_all_empty_filter_away_empty) def test_pawn_threatens_none_from_start_position(self): pieces = copy.deepcopy(starting_pieces) b7_pawn = select_piece(ChessCoord('B', '7'), pieces) b7_pawn.analyze_threats_on_board_for_new_move(pieces, ChessCoord('B', '7')) self.failUnless(b7_pawn.is_threat_to_these_pieces == []) def test_pawn_threatens_appropriate_pieces_in_front_of_it(self): pieces = copy.deepcopy(starting_pieces) f2_pawn = select_piece(ChessCoord('F', '2'), pieces) f2_pawn.analyze_threats_on_board_for_new_move(pieces, ChessCoord('F', '6')) pawns_e7_g7 = select_pieces([ChessCoord('E', '7'), ChessCoord('G', '7')], pieces) self.failUnless(util.compare_lists(f2_pawn.is_threat_to_these_pieces, pawns_e7_g7)) def test_pawn_threatens_appropriate_pieces_with_en_passant_in_front_of_it(self): pieces = copy.deepcopy(starting_pieces) g7_pawn = select_piece(ChessCoord('G', '7'), pieces) g7_pawn.update_coord(ChessCoord('G', '5')) f2_pawn = select_piece(ChessCoord('F', '2'), pieces) f2_pawn.analyze_threats_on_board_for_new_move(pieces, ChessCoord('F', '5')) self.failUnless(util.compare_lists(f2_pawn.is_threat_to_these_pieces, [g7_pawn]))