Skip to content

Commit

Permalink
Map generator (#15)
Browse files Browse the repository at this point in the history
* maps review

* wip

* update lib

* csv to txt

* add maze generator

---------

Co-authored-by: Matheus Jardim <[email protected]>
  • Loading branch information
matheusjardimb and Matheus Jardim authored Aug 2, 2024
1 parent 7dc4cdd commit 4013971
Show file tree
Hide file tree
Showing 28 changed files with 142 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pip_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
publish-to-pypi:
name: >-
Publish Python 🐍 distribution 📦 to PyPI
# if: startsWith(github.ref, 'refs/tags/') # only publish to PyPI on tag pushes
if: startsWith(github.ref, 'refs/tags/') # only publish to PyPI on tag pushes
needs:
- build
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.1
0.2.0
2 changes: 1 addition & 1 deletion example/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from maze_runner import Maze

if "__main__" == __name__:
maze = Maze("../maps/02.csv")
maze = Maze("../maps/02.txt")
maze.clear_console()

maze.print_maze_status()
Expand Down
4 changes: 4 additions & 0 deletions example/maze_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from maze_runner.maze_creator import MazeGenerator

maze = MazeGenerator.generate_maze_structure()
MazeGenerator.save_maze_to_file(maze, "labirinto.txt")
3 changes: 0 additions & 3 deletions maps/01.csv

This file was deleted.

3 changes: 3 additions & 0 deletions maps/01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#######
#S F#
#######
3 changes: 0 additions & 3 deletions maps/02.csv

This file was deleted.

3 changes: 3 additions & 0 deletions maps/02.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#######
#F S#
#######
5 changes: 0 additions & 5 deletions maps/03.csv

This file was deleted.

5 changes: 5 additions & 0 deletions maps/03.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#######
# F#
#S #
# F#
#######
6 changes: 0 additions & 6 deletions maps/04.csv

This file was deleted.

6 changes: 6 additions & 0 deletions maps/04.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
########
# S#
# #
# #
#F #
########
5 changes: 0 additions & 5 deletions maps/05.csv

This file was deleted.

5 changes: 5 additions & 0 deletions maps/05.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
########
# S#
# ######
# F#
########
7 changes: 0 additions & 7 deletions maps/06.csv

This file was deleted.

7 changes: 7 additions & 0 deletions maps/06.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
########
# S#
# ######
# #
###### #
#F #
########
7 changes: 0 additions & 7 deletions maps/07.csv

This file was deleted.

7 changes: 7 additions & 0 deletions maps/07.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
##########
#S #
######## #
# #
# ########
# F#
##########
7 changes: 0 additions & 7 deletions maps/08.csv

This file was deleted.

7 changes: 7 additions & 0 deletions maps/08.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
##########
#S# # #
# # # # #
# # # # #
# # # # #
# # F#
##########
3 changes: 1 addition & 2 deletions maps/09.csv → maps/09.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
##################
############ #
# # # #
# # #
# # #### # # # #
# # # S# # # # #
# # # ## # # # #
Expand Down
10 changes: 0 additions & 10 deletions maps/10.csv

This file was deleted.

10 changes: 10 additions & 0 deletions maps/10.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
##########
#F #F#
###### # #
# S # # #
# # # #
#### # # #
# #
######## #
#F #
##########
21 changes: 21 additions & 0 deletions maps/50.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#####################
#S# # # #
# # # # # ### ### # #
# # # # # # #
# ##### ### ####### #
# # # # #
##### ##### # # #####
# # # # # # # #
# # # # # ### # # # #
# # # # # # # # #
# # ####### # # ### #
# # # # # # #
# # # ### # # #######
# # # # # # #
# ##### # # # ##### #
# # # # # # # #
# # ##### # ### # # #
# # # # #
### # ############# #
# # F#
#####################
3 changes: 2 additions & 1 deletion maze_runner/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .maze import Maze
from .maze import Position
from .maze_creator import MazeGenerator

__all__ = [Maze, Position]
__all__ = [Maze, Position, MazeGenerator]
17 changes: 14 additions & 3 deletions maze_runner/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,15 @@ def plot_maze(
return image


def create_gif(header, maze, path, start_position, finish_positions, fps: int = 1):
def create_gif(
header,
maze,
path,
start_position,
finish_positions,
fps: int = 1,
filename: str = None,
):
if fps < 1:
raise Exception("fps must be >= 1")

Expand Down Expand Up @@ -117,6 +125,9 @@ def create_gif(header, maze, path, start_position, finish_positions, fps: int =
)
frames.append(image)

if not filename:
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
filename = f"maze_path_{timestamp}.gif"

# Save frames as a GIF
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
imageio.mimsave(f"maze_path_{timestamp}.gif", frames, fps=fps)
imageio.mimsave(filename, frames, fps=fps)
10 changes: 5 additions & 5 deletions maze_runner/maze.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import copy
import csv
import logging
import os
from time import sleep
Expand Down Expand Up @@ -36,11 +35,11 @@ def __init__(
self.finish_positions = []

# Start loading Maze file
with open(maze_file_path) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=",")
with open(maze_file_path) as file:
lines = file.readlines()
line_count = 0

for y_pos, cells in enumerate(csv_reader):
for y_pos, cells in enumerate(lines):
# Validate maze width
if self.maze_width is None:
self.maze_width = len(cells)
Expand Down Expand Up @@ -82,14 +81,15 @@ def __init__(

self.steps_taken = 0

def generate_animation(self, header: str, fps: int = 1):
def generate_animation(self, header: str, fps: int = 1, filename: str = None):
create_gif(
header,
self.maze,
self.positions,
self.start_position,
self.finish_positions,
fps,
filename,
)

def print_maze_status(self, clean_console: bool = True) -> None:
Expand Down
39 changes: 39 additions & 0 deletions maze_runner/maze_creator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import random


class MazeGenerator:
# Movimentos possíveis
MOVES = [(0, 2), (2, 0), (0, -2), (-2, 0)]

@staticmethod
def generate_maze_structure(width: int = 21, height: int = 21):
# Inicializa o labirinto com paredes
maze = [["#" for _ in range(width)] for _ in range(height)]

# Função recursiva para criar o labirinto
def carve_passages_from(x, y):
directions = MazeGenerator.MOVES[:]
random.shuffle(directions)
for dx, dy in directions:
nx, ny = x + dx, y + dy
if 0 <= nx < width and 0 <= ny < height and maze[ny][nx] == "#":
if 1 <= nx < width - 1 and 1 <= ny < height - 1:
maze[ny][nx] = " "
maze[ny - dy // 2][nx - dx // 2] = " "
carve_passages_from(nx, ny)

# Define o ponto de partida
start_x, start_y = (1, 1)
maze[start_y][start_x] = "S"
carve_passages_from(start_x, start_y)

# TODO add multi exits
# exits = [(width - 2, height - 2)]
maze[height - 2][width - 2] = "F"

return maze

@staticmethod
def save_maze_to_file(maze, filename):
with open(filename, "w", newline="") as file:
file.writelines([f"{''.join(line)}\n" for line in maze])

0 comments on commit 4013971

Please sign in to comment.