Skip to content

Commit

Permalink
feat(Map): ✨ implement data-management functions to load a map from a…
Browse files Browse the repository at this point in the history
…n string and classify agents

Implement function which receives a file path to load a map. Then process the string to find agents and store them along with their coordinates
  • Loading branch information
JuanJoseGonGi committed Nov 19, 2023
1 parent acee98f commit ec7c406
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"cSpell.words": [
"pathfinding",
"Sokoban"
]
],
"python.analysis.typeCheckingMode": "basic"
}
47 changes: 47 additions & 0 deletions src/data-management/map_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
def load_map_file(path: str):
with open(path, "r") as f:
return f.read().splitlines()


def process_map_file_row(row: str):
sanitized_row = (
row.replace(" ", "").replace("\t", "").replace("\n", "").replace("\r", "")
)
map_file_row_agents = sanitized_row.split(",")
map_file_row_agents = [agent for agent in map_file_row_agents if agent != ""]
return map_file_row_agents


def process_map_file(map_file: list[str]):
map_structure: dict[str, list[tuple[str, tuple[int, int]]]] = {
"paths": [],
"rocks": [],
"goals": [],
"robots": [],
"boxes": [],
}

for y, map_file_row in enumerate(map_file):
map_file_row_agents = process_map_file_row(map_file_row)
for x, map_file_cell_agents in enumerate(map_file_row_agents):
map_file_agents = map_file_cell_agents.split("-", 1)
for map_file_agent in map_file_agents:
upper_map_file_agent = map_file_agent.upper()
if upper_map_file_agent == "C":
map_structure["paths"].append((upper_map_file_agent, (x, y)))
elif upper_map_file_agent == "R":
map_structure["rocks"].append((upper_map_file_agent, (x, y)))
elif upper_map_file_agent == "M":
map_structure["goals"].append((upper_map_file_agent, (x, y)))
elif "B" in upper_map_file_agent:
map_structure["boxes"].append((upper_map_file_agent, (x, y)))
elif "A" in upper_map_file_agent:
map_structure["robots"].append((upper_map_file_agent, (x, y)))

return map_structure


if __name__ == "__main__":
map_file = load_map_file("samples/map1.txt")
map_structure = process_map_file(map_file)
print(map_structure)
7 changes: 7 additions & 0 deletions src/data-management/samples/map1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
R, R, R, R, R,
R, C-a-1, C, C, R,
R, C, C-b-1, C, R,
R, R, R, C, R,
R, M, C-b-2, C, R,
R, C-a-2, C, M, R,
R, R, R, R, R,

0 comments on commit ec7c406

Please sign in to comment.