def test_adj_default(): board_adj_default_cross = Board(10, 10) p1 = Pos(5, 5) p2 = Pos(6, 5) p3 = Pos(4, 4) # cross: assert board_adj_default_cross.is_adjacent(p1, p2) == True # diag : assert board_adj_default_cross.is_adjacent(p1, p3) == False set_default_adjacency(AdjacencyEvaluatorCross) board_adj_default_cross_2 = Board(10, 10) # cross: assert board_adj_default_cross_2.is_adjacent(p1, p2) == True # diag : assert board_adj_default_cross_2.is_adjacent(p1, p3) == False set_default_adjacency(AdjacencyEvaluatorCrossDiag) board_adj_default_cross_diag = Board(10, 10) # cross: assert board_adj_default_cross_diag.is_adjacent(p1, p2) == True # diag : assert board_adj_default_cross_diag.is_adjacent(p1, p3) == True
def test_comp_simple(): board_1 = Board(5, 3) setting_data_1 = ("ABCDE", "FGHIJ", "KLMNO") board_1.set_data_from_string(setting_data_1) board_2 = Board(5, 3) setting_data_2 = ("ABCDX", "FGHIJ", "KZMNO") board_2.set_data_from_string(setting_data_2) index_diff = 0 for tile_1, tile_2 in IteratorGetDifferences(board_1[:], board_2[:]): print(tile_1, tile_2) if index_diff == 0: first = False assert tile_1.x == tile_2.x == 4 assert tile_1.y == tile_2.y == 0 assert tile_1.data == "E" assert tile_2.data == "X" elif index_diff == 1: assert tile_1.x == tile_2.x == 1 assert tile_1.y == tile_2.y == 2 assert tile_1.data == "L" assert tile_2.data == "Z" else: assert False index_diff += 1
def test_comp_no_diff(): setting_data = ("ABCDE", "FGHIJ", "KLMNO") board_1 = Board(5, 3) board_1.set_data_from_string(setting_data) board_2 = Board(5, 3) board_2.set_data_from_string(setting_data) assert list(IteratorGetDifferences(board_1[:], board_2[:])) == []
def test_both_coord_changed(): my_board_renderer = BoardRenderer(tile_w=2, tile_padding_w=1, tile_padding_h=1, chr_fill_tile=".") board = Board(2, 4, default_renderer=my_board_renderer) board_iter_rect = BoardIteratorRect(board) DICT_MARKERS = {False: "_", True: "B"} for index, tile in enumerate(board_iter_rect): both_coord_marker = DICT_MARKERS[board_iter_rect.both_coord_changed] tile.data = str(index) + both_coord_marker render_result = """ 0B 1_ 2B 3_ 4B 5_ 6B 7_ """ assert strip_multiline(board.render()) == strip_multiline(render_result)
def test_jump_and_dir_change(): board = Board(10, 10) slice_x = slice(5) slice_y = slice(3) rect_iter = BoardIteratorRect(board, slice_x, slice_y) for tile in rect_iter: pos = tile.pos print(pos) if pos == (0, 0): assert rect_iter.jumped == True assert rect_iter.changed_direction == False assert rect_iter.both_coord_changed == True elif pos == (1, 0): assert rect_iter.jumped == False assert rect_iter.changed_direction == False assert rect_iter.both_coord_changed == False elif pos.x == 0: assert rect_iter.jumped == True assert rect_iter.changed_direction == True assert rect_iter.both_coord_changed == True elif pos.x == 1: assert rect_iter.jumped == False assert rect_iter.changed_direction == True assert rect_iter.both_coord_changed == False else: assert rect_iter.jumped == False assert rect_iter.changed_direction == False assert rect_iter.both_coord_changed == False
def test_find_path_simple_2(): board = Board(15, 10, class_adjacency=AdjacencyEvaluatorCross) iter_find_path = board.get_by_pathfinding((3, 2), (6, 9)) for index, tile in enumerate(iter_find_path): tile.data = hex(index)[2] print(board.render()) render_result = """ ............... ............... ...0123........ ......4........ ......5........ ......6........ ......7........ ......8........ ......9........ ......a........ """ assert strip_multiline(board.render()) == strip_multiline(render_result)
def test_propagation_dist_with_iter(): board = Board(15, 10) def can_propag(source, dest): return any((dest.x < 7 and dest.y < 6, dest.y == 3, dest.x == 2)) for propag_dist, pos in BoardIteratorPropagation( board, (1, 3), can_propag).tell_indicators((ItInd.PROPAG_DIST, )): board.get_tile(pos).data = propag_dist print(board.render()) render_result = """ 3333345........ 2222345........ 1112345........ 101234567891111 1112345........ 2222345........ ..3............ ..4............ ..5............ ..6............ """ assert strip_multiline(board.render()) == strip_multiline(render_result)
def test_propagation_dist(): board = Board(15, 10, class_adjacency=AdjacencyEvaluatorCross) def can_propag(source, dest): return any((dest.x < 7 and dest.y < 6, dest.y == 3, dest.x == 2)) propag_iter = BoardIteratorPropagation(board, (4, 4), can_propag) for pos in propag_iter: board.get_tile(pos).data = propag_iter.propag_dist print(board.render()) render_result = """ 8765456........ 7654345........ 6543234........ 543212345678911 4321012........ 5432123........ ..4............ ..5............ ..6............ ..7............ """ assert strip_multiline(board.render()) == strip_multiline(render_result)
def test_simple_iteration_main_y(): board = Board(120, 120) slice_x = slice(1, 13, 3) slice_y = slice(15, 135, 30) positions_check = [ (1, 15), (1, 45), (1, 75), (1, 105), (4, 15), (4, 45), (4, 75), (4, 105), (7, 15), (7, 45), (7, 75), (7, 105), (10, 15), (10, 45), (10, 75), (10, 105), ] for tile in BoardIteratorRect(board, slice_x, slice_y, Coord.Y): print(tile.pos) position_check = positions_check.pop(0) assert tile.pos == position_check assert positions_check == []
def test_both_coord_changed_render(): positions = [(0, 0), (0, 2), (2, 2), (1, 1), (1, 0), (3, 3), (0, 3)] my_board_renderer = BoardRenderer(tile_w=2, tile_padding_w=1, tile_padding_h=1, chr_fill_tile=".") board = Board(4, 5, default_renderer=my_board_renderer) board_iter_positions = BoardIteratorPositions(board, positions) for index, tile in enumerate(board_iter_positions): DICT_MARKERS = {False: "_", True: "B"} both_coord_marker = DICT_MARKERS[ board_iter_positions.both_coord_changed] tile.data = str(index) + both_coord_marker # Pour la toute première itération, both_coord_changed est à True. # On considère que le curseur passe de (rien, rien) à (0, 0), et que les deux coords changent. render_result = """ 0B 4_ .. .. .. 3B .. .. 1_ .. 2_ .. 6_ .. .. 5B .. .. .. .. """ assert strip_multiline(board.render()) == strip_multiline(render_result)
def test_permute_column(): board = Board(5, 7) setting_data = ("ABCDE", "FGHIJ", "KLMNO", "PQRST", "UVWXY", "01234", "56789") board.set_data_from_string(setting_data) pos_to_permute = [Pos(tile.x, tile.y) for tile in board[2, :]] assert len(pos_to_permute) == 7 board.circular_permute_tiles(pos_to_permute) print(board.render()) render_result = """ ABHDE FGMIJ KLRNO PQWST UV2XY 01734 56C89 """ assert strip_multiline(board.render()) == strip_multiline(render_result) # Pour vérifier que la fonction de permutation ne vide pas la liste. # Ça le faisait avant, et c'était mal. assert len(pos_to_permute) == 7
def test_replace_simple(): board = Board(5, 3) setting_data = ("ABCDE", "FGHIJ", "KLMNO") board.set_data_from_string(setting_data) new_tile = Tile() new_tile.data = "Z" board.replace_tile(new_tile, Pos(3, 1)) print(board.render()) assert new_tile.x == 3 assert new_tile.y == 1 assert board[3, 1].data == "Z" render_result = """ ABCDE FGHZJ KLMNO """ assert strip_multiline(board.render()) == strip_multiline(render_result) print(board.render())
def test_permute_simple(): board = Board(5, 3) setting_data = ("ABCDE", "FGHIJ", "KLMNO") board.set_data_from_string(setting_data) tile_with_c = board[2, 0] tile_with_n = board[3, 2] board.circular_permute_tiles([Pos(2, 0), Pos(3, 2)]) print(board.render()) assert tile_with_c.x == 3 assert tile_with_c.y == 2 assert tile_with_c.data == "C" assert board[3, 2].data == "C" assert tile_with_n.x == 2 assert tile_with_n.y == 0 assert tile_with_n.data == "N" assert board[2, 0].data == "N" render_result = """ ABNDE FGHIJ KLMCO """ assert strip_multiline(board.render()) == strip_multiline(render_result)
def test_jump_and_dir_change_check_indic(): board = Board(20, 20) positions = [(1, 2), (1, 3), (1, 4), (2, 4), (3, 4), (5, 4), (6, 4), (9, 0)] pos_iterator = BoardIteratorPositions(board, positions) for tile in pos_iterator: pos = tile.pos if pos == (1, 2): assert pos_iterator.jumped == True assert pos_iterator.changed_direction == False elif pos == (2, 4): assert pos_iterator.jumped == False assert pos_iterator.changed_direction == True elif pos == (5, 4): assert pos_iterator.jumped == True assert pos_iterator.changed_direction == False elif pos == (9, 0): assert pos_iterator.jumped == True assert pos_iterator.changed_direction == True else: assert pos_iterator.jumped == False assert pos_iterator.changed_direction == False
def test_getitem_square_reversed_stepped_on_y_grouped(): my_board_renderer = BoardRenderer(tile_w=2) board = Board(12, 13, default_renderer=my_board_renderer) for group_index, column in enumerate(board[9:2:-3, 11:3:-2, "y"].group_by_subcoord()): for index, tile in enumerate(column): tile.data = chr(group_index + ord("A")) + str(index) render_result = """ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C3. . B3. . A3. . . . . . . . . . . . . . . . . C2. . B2. . A2. . . . . . . . . . . . . . . . . C1. . B1. . A1. . . . . . . . . . . . . . . . . C0. . B0. . A0. . . . . . . . . . . . . . """ print(board.render()) assert strip_multiline(board.render()) == strip_multiline(render_result)
def test_getitem_square_reversed_stepped_on_y(): board = Board(12, 13) for index, tile in enumerate(board[9:2:-3, 11:3:-2, "y"]): tile.data = hex(index)[2] render_result = """ ............ ............ ............ ............ ............ ...b..7..3.. ............ ...a..6..2.. ............ ...9..5..1.. ............ ...8..4..0.. ............ """ print(board.render()) assert strip_multiline(board.render()) == strip_multiline(render_result)
def test_getitem_stepped(): board = Board(8, 13) for index, tile in enumerate(board[::3, ::4]): tile.data = hex(index)[2] render_result = """ 0..1..2. ........ ........ ........ 3..4..5. ........ ........ ........ 6..7..8. ........ ........ ........ 9..a..b. """ print(board.render()) assert strip_multiline(board.render()) == strip_multiline(render_result)
def test_getitem_fail(): board = Board(5, 14) failed_at_failing = False try: a = board[0, 14] failed_at_failing = True except BoardIndexError as e: print(e) try: p = Pos(5, 0) a = board[p] failed_at_failing = True except BoardIndexError as e: print(e) try: a = board[0, -15] failed_at_failing = True except BoardIndexError as e: print(e) try: p = Pos(-6, 0) a = board[p] failed_at_failing = True except BoardIndexError as e: print(e) assert failed_at_failing == False
def test_sur_iter_directly_from_board(): board = Board(3, 2) for both_coord_changed, tile in board[:, :].tell_indicators(): if tile.x == 0: assert both_coord_changed == True else: assert both_coord_changed == False
def test_sur_iter_tell_both_coord_changed(): board = Board(3, 2) for both_coord_changed, tile in BoardIteratorRect(board).tell_indicators(): if tile.x == 0: assert both_coord_changed == True else: assert both_coord_changed == False
def test_sur_iter_tell_everything(): itind_everything = ( ItInd.PREV_POS, ItInd.PREV_PREV_POS, ItInd.JUMPED, ItInd.CHANGED_DIRECTION, ItInd.BOTH_COORD_CHANGED, ) board = Board(20, 20) positions = [(1, 2), (1, 3), (1, 4), (2, 4), (3, 4), (5, 4), (6, 4), (9, 0)] prev_positions = [None] + positions prev_prev_positions = [None, None] + positions for iter_everything in BoardIteratorPositions(board, positions).tell_indicators( itind_everything ): print(iter_everything) ( prev_pos, prev_prev_pos, jumped, changed_direction, both_coord_changed, tile, ) = iter_everything check_prev_pos = prev_positions.pop(0) check_prev_prev_pos = prev_prev_positions.pop(0) assert prev_pos == check_prev_pos assert prev_prev_pos == check_prev_prev_pos pos = tile.pos if pos == (1, 2): assert jumped == True assert changed_direction == False assert both_coord_changed == True elif pos == (2, 4): assert jumped == False assert changed_direction == True assert both_coord_changed == False elif pos == (5, 4): assert jumped == True assert changed_direction == False elif pos == (9, 0): assert jumped == True assert changed_direction == True assert both_coord_changed == True else: assert jumped == False assert changed_direction == False assert both_coord_changed == False
def test_simple_iteration_directly_from_board(): board = Board(20, 20) positions = [(1, 2), (3, 4), (5, 6), (7, 8)] check_positions = list(positions) for tile in board.iter_positions(positions): print(tile.pos) check_pos = check_positions.pop(0) assert tile.pos == check_pos assert check_positions == []
def test_simple_iteration_check_pos(): board = Board(20, 20) positions = [(1, 2), (3, 4), (5, 6), (7, 8)] check_positions = list(positions) for tile in BoardIteratorPositions(board, positions): print(tile.pos) check_pos = check_positions.pop(0) assert tile.pos == check_pos assert check_positions == []
def test_sur_iter_group_by_simple(): board = Board(5, 8) y_coord = 0 for tile_group in BoardIteratorRect(board).group_by_subcoord(): print(*map(str, tile_group)) check_coords = [(x, y_coord) for x in range(5)] for tile, check_coord in zip(tile_group, check_coords): assert tile.pos == check_coord y_coord += 1 assert y_coord == 8
def test_sur_iter_groub_by_dir_changes_directly_from_board(): board = Board(10, 10) positions = [ (1, 2), (1, 3), (1, 4), (1, 5), (2, 5), (3, 5), (4, 5), (5, 5), (6, 5), (7, 5), (8, 5), (7, 6), (6, 7), (5, 8), (4, 9), (3, 8), (2, 7), (1, 6), (0, 5), ] group_marker = "a" for tile_group in board.iter_positions(positions).group_by( lambda b: b.changed_direction ): print(*map(str, tile_group)) tile_group[0].data = group_marker.upper() for tile in tile_group[1:]: tile.data = group_marker group_marker = chr(ord(group_marker) + 1) render_result = """ .......... .......... .A........ .a........ .a........ daBbbbbbb. .d.....C.. ..d...c... ...D.c.... ....c..... """ assert strip_multiline(board.render()) == strip_multiline(render_result)
def test_iteration_all_board_render(): board = Board(5, 2) for index, tile in enumerate(board): tile.data = index render_result = """ 01234 56789 """ assert strip_multiline(board.render()) == strip_multiline(render_result)
def test_init_simple(): board = Board(5, 3) setting_data = ("ABCDE", "FGHIJ", "KLMNO") board.set_data_from_string(setting_data) render_result = """ ABCDE FGHIJ KLMNO """ assert strip_multiline(board.render()) == strip_multiline(render_result)
def test_find_path_obstacle(): board = Board(15, 10, class_adjacency=AdjacencyEvaluatorCross) # TODO : il faudra faire une init du board from input. Pour que ce soit plus compréhensible. pos_walls = [ (2, 3), (3, 3), (4, 3), (5, 3), (6, 3), (6, 4), (6, 5), (6, 6), (5, 6), (4, 6), (4, 7), (4, 8), (3, 8), (2, 8), (2, 7), (2, 6), (2, 5), (2, 4), ] for pos in pos_walls: board.get_tile(pos).data = "*" pos_start = (5, 2) pos_end = (4, 9) for index, tile in enumerate( BoardIteratorFindPath(board, pos_start, pos_end)): tile.data = hex(index)[2] render_result = """ ............... ............... .....012....... ..*****3....... ..*...*4....... ..*...*5....... ..*.***6....... ..*.*987....... ..***a......... ....cb......... """ assert strip_multiline(board.render()) == strip_multiline(render_result)
def test_getitem_square_reversed_all(): board = Board(3, 2) for index, tile in enumerate(board[::-1, ::-1]): tile.data = hex(index)[2] render_result = """ 543 210 """ print(board.render()) assert strip_multiline(board.render()) == strip_multiline(render_result)
def test_basic_renderer(): class MyTileTellCoordsShort(Tile): def render(self, w=1, h=1): return hex(self.x * self.y)[2:].upper() board = Board(7, 4, MyTileTellCoordsShort) render_result = """ 0000000 0123456 02468AC 0369CF1 """ assert board.render() == strip_multiline(render_result)