diff --git a/dlgr/griduniverse/csv_gridworlds.py b/dlgr/griduniverse/csv_gridworlds.py index d60efecc..c21442ba 100644 --- a/dlgr/griduniverse/csv_gridworlds.py +++ b/dlgr/griduniverse/csv_gridworlds.py @@ -21,7 +21,8 @@ def matrix2gridworld(matrix): for row_num, row in enumerate(matrix): for col_num, cell in enumerate(row): - position = [col_num, row_num] + # NB: we use [y, x] format in GU!! (╯°□°)╯︵ ┻━┻ + position = [row_num, col_num] cell = cell.strip() player_match = player_regex.match(cell) if not cell: @@ -31,7 +32,7 @@ def matrix2gridworld(matrix): result["walls"].append(position) elif player_match: id_str, color_str = player_match.groups() - player_id = int(id_str.replace("p", "")) + player_id = id_str.replace("p", "") player_data = { "id": player_id, "position": position, diff --git a/test/test_gridworld.py b/test/test_gridworld.py index d8a33954..093d9dc0 100644 --- a/test/test_gridworld.py +++ b/test/test_gridworld.py @@ -178,32 +178,35 @@ def test_one_row(self): assert result == { "columns": 5, - "rows": 1, "items": [ - { - "id": 1, - "item_id": "stone", - "position": [1, 0], - }, + {"id": 1, "item_id": "stone", "position": [0, 1]}, { "id": 2, "item_id": "gooseberry_bush", - "position": [3, 0], + "position": [0, 3], "remaining_uses": 3, }, ], - "walls": [ - [0, 0], - ], - "players": [ - { - "color": "YELLOW", - "position": [4, 0], - "id": 1, - } - ], + "players": [{"color": "YELLOW", "id": "1", "position": [0, 4]}], + "rows": 1, + "walls": [[0, 0]], } + def test_to_demonstrate_orientation(self): + csv = [ + ["top-left", "top-right"], + ["bottom-left", "bottom-right"], + ] + + result = self.subject(csv) + + assert result["items"] == [ + {"id": 1, "item_id": "top-left", "position": [0, 0]}, + {"id": 2, "item_id": "top-right", "position": [0, 1]}, + {"id": 3, "item_id": "bottom-left", "position": [1, 0]}, + {"id": 4, "item_id": "bottom-right", "position": [1, 1]}, + ] + def test_multirow(self): csv = [ ["w", "stone", "", "gooseberry_bush|3", "p1c1"], @@ -222,66 +225,31 @@ def test_multirow(self): assert result == { "columns": 5, - "rows": 10, "items": [ - { - "id": 1, - "item_id": "stone", - "position": [1, 0], - }, + {"id": 1, "item_id": "stone", "position": [0, 1]}, { "id": 2, "item_id": "gooseberry_bush", - "position": [3, 0], + "position": [0, 3], "remaining_uses": 3, }, { "id": 3, "item_id": "gooseberry_bush", - "position": [0, 6], + "position": [6, 0], "remaining_uses": 4, }, - { - "id": 4, - "item_id": "big_hard_rock", - "position": [1, 7], - }, - ], - "walls": [ - [0, 0], - [1, 1], - [2, 2], - [3, 3], - [4, 4], - [4, 5], + {"id": 4, "item_id": "big_hard_rock", "position": [7, 1]}, ], "players": [ - { - "color": "BLUE", - "position": [4, 0], - "id": 1, - }, - { - "color": "BLUE", - "position": [1, 2], - "id": 2, - }, - { - "color": "YELLOW", - "position": [3, 4], - "id": 3, - }, - { - "color": "YELLOW", - "position": [1, 8], - "id": 4, - }, - { - "color": "ORANGE", - "position": [2, 9], - "id": 5, - }, + {"color": "BLUE", "id": "1", "position": [0, 4]}, + {"color": "BLUE", "id": "2", "position": [2, 1]}, + {"color": "YELLOW", "id": "3", "position": [4, 3]}, + {"color": "YELLOW", "id": "4", "position": [8, 1]}, + {"color": "ORANGE", "id": "5", "position": [9, 2]}, ], + "rows": 10, + "walls": [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [5, 4]], } def test_supports_empty_matrix(self): @@ -298,30 +266,18 @@ def test_not_confused_by_whitepace(self): assert result == { "columns": 5, - "rows": 1, "items": [ - { - "id": 1, - "item_id": "stone", - "position": [1, 0], - }, + {"id": 1, "item_id": "stone", "position": [0, 1]}, { "id": 2, "item_id": "gooseberry_bush", - "position": [3, 0], + "position": [0, 3], "remaining_uses": 3, }, ], - "walls": [ - [0, 0], - ], - "players": [ - { - "color": "YELLOW", - "position": [4, 0], - "id": 1, - } - ], + "players": [{"color": "YELLOW", "id": "1", "position": [0, 4]}], + "rows": 1, + "walls": [[0, 0]], } def test_explains_invalid_player_colors(self): @@ -343,6 +299,6 @@ def test_preserves_empty_edge_rows_and_columns(self): assert result == { "columns": 5, - "items": [{"id": 1, "item_id": "stone", "position": [2, 1]}], + "items": [{"id": 1, "item_id": "stone", "position": [1, 2]}], "rows": 3, }