Skip to content

Commit

Permalink
Fix x, y vs. y, x grid layout bug, and player ID's are strings, not ints
Browse files Browse the repository at this point in the history
  • Loading branch information
jessesnyder committed Oct 14, 2023
1 parent faa4245 commit 00e14cc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 83 deletions.
5 changes: 3 additions & 2 deletions dlgr/griduniverse/csv_gridworlds.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
Expand Down
118 changes: 37 additions & 81 deletions test/test_gridworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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,
}

0 comments on commit 00e14cc

Please sign in to comment.