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)
示例#3
0
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_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)
示例#5
0
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())
示例#6
0
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)
示例#7
0
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)
示例#8
0
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)
示例#9
0
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)
示例#10
0
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_propagation_simple_2():

    board = Board(15, 10)
    # 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 = "*"
    print(board.render())

    for pos in board.get_by_propagation((4, 4)):
        print(pos)
        board.get_tile(pos).data = "o"
    print(board.render())

    render_result = """

		...............
		...............
		...............
		..*****........
		..*ooo*........
		..*ooo*........
		..*o***........
		..*o*..........
		..***..........
		...............

	"""
    assert strip_multiline(board.render()) == strip_multiline(render_result)
示例#12
0
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)
示例#13
0
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)
示例#14
0
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)
示例#15
0
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)
示例#16
0
def test_getitem_pos():

    board = Board(3, 3)
    p = Pos(1, 0)
    board[p].data = "|"
    board[Pos(0, -2)].data = "-"
    board[Pos(-2, 1)].data = "*"
    board[Pos(-1, -2)].data = "~"
    board[{"x": 1, "y": 2}].data = "I"

    render_result = """

		.|.
		-*~
		.I.

	"""
    print(board.render())
    assert strip_multiline(board.render()) == strip_multiline(render_result)
示例#17
0
def test_getitem_square():

    board = Board(10, 6)

    for index, tile in enumerate(board[3:7, 2:5]):
        tile.data = hex(index)[2]

    render_result = """

		..........
		..........
		...0123...
		...4567...
		...89ab...
		..........

	"""
    print(board.render())
    assert strip_multiline(board.render()) == strip_multiline(render_result)
示例#18
0
def test_getitem_one_column():

    board = Board(8, 8)

    for index, tile in enumerate(board[5]):
        tile.data = hex(index)[2]

    render_result = """

		.....0..
		.....1..
		.....2..
		.....3..
		.....4..
		.....5..
		.....6..
		.....7..

	"""
    print(board.render())
    assert strip_multiline(board.render()) == strip_multiline(render_result)
示例#19
0
def test_getitem_square_neg_coords():

    board = Board(8, 8)

    for index, tile in enumerate(board[-5:-2, -7:-3]):
        tile.data = hex(index)[2]

    render_result = """

		........
		...012..
		...345..
		...678..
		...9ab..
		........
		........
		........

	"""
    print(board.render())
    assert strip_multiline(board.render()) == strip_multiline(render_result)
示例#20
0
def test_getitem_one_line():

    board = Board(8, 8)

    for index, tile in enumerate(board[:, 6]):
        tile.data = hex(index)[2]

    render_result = """

		........
		........
		........
		........
		........
		........
		01234567
		........

	"""
    print(board.render())
    assert strip_multiline(board.render()) == strip_multiline(render_result)
示例#21
0
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_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)
示例#23
0
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_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)
示例#25
0
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)
示例#26
0
def test_getitem_one_elem():

    board = Board(9, 5)
    board[0, 0].data = "\\"
    board[4, 0].data = "^"
    board[8, 0].data = "/"
    board[0, 2].data = "<"
    board[4, 2].data = "+"
    board[8, 2].data = ">"
    board[0, 4].data = "L"
    board[4, 4].data = "V"
    board[8, 4].data = "J"

    render_result = """

		\...^.../
		.........
		<...+...>
		.........
		L...V...J

	"""
    print(board.render())
    assert strip_multiline(board.render()) == strip_multiline(render_result)
示例#27
0
def test_getitem_one_elem_negative_coords():

    board = Board(9, 5)
    board[-9, -5].data = "{"
    board[-8, -5].data = "("
    board[-9, -4].data = "["
    board[-1, 0].data = '"'
    board[0, -1].data = "'"
    board[-5, 2].data = "#"
    board[-1, -2].data = "]"
    board[-2, -1].data = ")"
    board[-1, -1].data = "}"

    render_result = """

		{(......"
		[........
		....#....
		........]
		'......)}

	"""
    print(board.render())
    assert strip_multiline(board.render()) == strip_multiline(render_result)
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)
示例#29
0
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)
示例#30
0
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)