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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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)
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_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_getitem_all_on_y(): board = Board(5, 3) for index, tile in enumerate(board[:, :, "y"]): tile.data = hex(index)[2] render_result = """ 0369c 147ad 258be """ 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_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_init_separators(): board = Board(6, 4) # setting_data = ('A;B;C;D;E;F\n', 'GHIJKL', 'MNOPQR', 'STUVWX') setting_data = "A;B;C;D;E;F\nG;H;I;J;K;L\nM;N;O;P;Q;R\nS;T;U;V;W;X" board.set_data_from_string(setting_data, "\n", ";") render_result = """ ABCDEF GHIJKL MNOPQR STUVWX """ assert strip_multiline(board.render()) == strip_multiline(render_result)
def test_getitem_all(): board = Board(2, 3) for index, tile in enumerate(board[:]): tile.data = hex(index)[2] render_result = """ 01 23 45 """ print(board.render()) 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_padded_renderer(): class MyTileTellCoordsLong(Tile): def render(self, w=1, h=1): return [ "", "_" + str(self.x) + "," + str(self.y), # Attention, ici on ne met pas une string, mais un int. # C'est fait exprès. Et ça doit quand même fonctionner. self.x * self.y, ] my_board_renderer = BoardRenderer( tile_w=5, tile_h=4, tile_padding_w=3, tile_padding_h=2, chr_fill_tile=".", chr_fill_tile_padding="#", ) board = Board(7, 4, MyTileTellCoordsLong) render_result = """ .....###.....###.....###.....###.....###.....###..... _0,0.###_1,0.###_2,0.###_3,0.###_4,0.###_5,0.###_6,0. 0....###0....###0....###0....###0....###0....###0.... .....###.....###.....###.....###.....###.....###..... ##################################################### ##################################################### .....###.....###.....###.....###.....###.....###..... _0,1.###_1,1.###_2,1.###_3,1.###_4,1.###_5,1.###_6,1. 0....###1....###2....###3....###4....###5....###6.... .....###.....###.....###.....###.....###.....###..... ##################################################### ##################################################### .....###.....###.....###.....###.....###.....###..... _0,2.###_1,2.###_2,2.###_3,2.###_4,2.###_5,2.###_6,2. 0....###2....###4....###6....###8....###10...###12... .....###.....###.....###.....###.....###.....###..... ##################################################### ##################################################### .....###.....###.....###.....###.....###.....###..... _0,3.###_1,3.###_2,3.###_3,3.###_4,3.###_5,3.###_6,3. 0....###3....###6....###9....###12...###15...###18... .....###.....###.....###.....###.....###.....###..... """ assert board.render(my_board_renderer) == strip_multiline(render_result)
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_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_iteration_all_board(): board = Board(4, 5) board_iter_rect = BoardIteratorRect(board) for index, tile in enumerate(board_iter_rect): tile.data = chr(index + ord("A")) render_result = """ ABCD EFGH IJKL MNOP QRST """ assert strip_multiline(board.render()) == strip_multiline(render_result)