Python Maze Generator. Depth-First Search

Поделиться
HTML-код
  • Опубликовано: 29 авг 2024

Комментарии • 9

  • @nerdobject5351
    @nerdobject5351 Год назад +1

    Really cool. I had this exact project in one of my senior computer science courses. I used Java and it’s fun to see my solution wasn’t that different from yours!

  • @0ri0nexe
    @0ri0nexe 11 месяцев назад

    Ty, I searched how to do it and the easier way i found

  • @mrdixioner
    @mrdixioner 4 месяца назад

    Спасибо огромное! Всё заработало с первого раза!!!

  • @pluto4-g4s
    @pluto4-g4s Год назад +1

    Thank you, this video helped a lot. How did you make the maze generate instantly? This is my only problem, it takes a long time to generate bigger mazes and your github code makes them instantly. I don't really understand it while trying to read it. The visualation is cool but takes too long
    Also, can you explain the list comprehension you used for the creation of the grid? I tried implementing it without comprehension using nested for loops but it messes up the check index function. Can you translate the list comprehension into a standard for loop? I'm finding it hard to read

  • @The-redstone-channel
    @The-redstone-channel Месяц назад

    I'm wondering how do I check if the player is at a certain cell?

  • @daone1908
    @daone1908 2 месяца назад

    thats a lot of code
    can someone copy and paste it in the comments ple

  • @lionpersia
    @lionpersia Год назад +1

    Hey Coder Space, great video as always. I have a question: what is the name of the music in the background of this video? I find it soothing! Thank you!

    • @CoderSpaceChannel
      @CoderSpaceChannel  Год назад

      thanks but unfortunately this is still unknown music and shazam does not help

  • @daone1908
    @daone1908 2 месяца назад

    bruh im stuck i when to ai to get it helped me but it just game me more problems ple oneone help
    import pygame
    from random import choice
    RES = WIDTH, HEIGHT = 1202, 902
    TILE = 100
    cols, rows = WIDTH // TILE, HEIGHT // TILE
    pygame.init()
    sc = pygame.display.set_mode(RES)
    clock = pygame.time.Clock()
    class Cell:
    def __init__(self, x, y):
    self.x, self.y = x, y
    self.walls = {'top': True, 'right': True, 'bottom': True, 'left': True}
    self.visited = False
    def draw_current_cell(self):
    x, y = self.x * TILE, self.y * TILE
    pygame.draw.rect(sc, pygame.Color('saddlebrown'), (x + 2, y + 2, TILE + 2, TILE - 2))
    def draw(self):
    x, y = self.x * TILE, self.y * TILE
    if self.visited:
    pygame.draw.rect(sc, pygame.Color('black'), (x, y, TILE, TILE))
    if self.walls['top']:
    pygame.draw.line(sc, pygame.Color('darkorange'), (x, y), (x + TILE, y), 2)
    if self.walls['right']:
    pygame.draw.line(sc, pygame.Color('darkorange'), (x + TILE, y), (x + TILE, y + TILE), 2)
    if self.walls['bottom']:
    pygame.draw.line(sc, pygame.Color('darkorange'), (x + TILE, y + TILE),(x + TILE, y), 2)
    if self.walls['left']:
    pygame.draw.line(sc, pygame.Color('darkorange'), (x + TILE, y),(x, y), 2)
    def check_cell(self, x, y):
    def find_index(x, y): return x + y * cols
    # Add 'def' keyword and indent
    if x < 0 or x > cols - 1 or y < 0 or y > rows - 1:
    return False
    return grid_cells[find_index(x, y)]
    def check_cell(self, x, y):
    def find_index(x, y): # Add 'def' keyword and indent
    return x + y * cols
    if x < 0 or x > cols - 1 or y < 0 or y > rows - 1:
    return False
    return grid_cells[find_index(x, y)]
    def check_neighbors(self):
    neighbors = []
    top = self.check_cell(self.x, self.y - 1)
    right = self.check_cell(self.x + 1, self.y)
    bottom = self.check_cell(self.x, self.y + 1)
    left = self.check_cell(self.x - 1, self.y)
    if top and not top.visited:
    neighbors.append(top)
    if right and not right.visited:
    neighbors.append(right)
    if bottom and not bottom.visited:
    neighbors.append(bottom)
    if left and not left.visited:
    neighbors.append(left)
    return choice(neighbors) if neighbors else False
    grid_cells = [Cell(col, row) for row in range(rows) for col in range(cols)]
    current_cell = grid_cells[0]
    stack = []
    while True:
    sc.fill(pygame.Color('darkslategray'))
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    exit()
    [cell.draw() for cell in grid_cells]
    current_cell.visited = True
    current_cell.draw_current_cell()
    next_cell = current_cell.check_neighbors()
    if next_cell:
    next_cell.visited = True
    current_cell = next_cell
    pygame.display.flip()
    clock.tick(30)