Jakub Skop
Jakub Skop

Reputation: 111

Have I implented Prim's Algorithm correctly? - Maze generation

I am fairly new to Python, and recently started creating a game, that I hope would entirely take place in the terminal. One of the aspects of the game was creating a random maze, so I decided to use a randomized form of Prim's Algorithm, as described here - https://en.wikipedia.org/wiki/Maze_generation_algorithm

The code works, but the maze feels to "easy", as though there are too many connections between the passageways. I don't know whether this is because I did it wrong, or it's just a feature of the algorithm. Either way, I would appreciate someone explaining how to fix this; keep in mind I'm still a beginner.

import random

def printout(*args):
  for arg in args:
    print(*arg, sep='')

Map = [["░" for k in range(40)] for l in range(20)]

def Prim(Map):
  Wall_List = []
  position = [0,20]
  Map[0][20]= " "

  for i in range(420):

    if Map[(position[0]+2)%20][position[1]] == "░":
      Wall_List.append([(position[0]+2)%20,position[1]])
    if Map[(position[0]-2)%20][position[1]] == "░":
      Wall_List.append([(position[0]-2)%20,position[1]])
    if Map[position[0]][(position[1]+2)%40] == "░":
      Wall_List.append([position[0],(position[1]+2)%40])
    if Map[position[0]][(position[1]-2)%40] == "░":
      Wall_List.append([position[0],(position[1]-2)%40])

    cellposition = random.choice(Wall_List)
    joiningwalls = []

    if Map[(cellposition[0]+2)%20][cellposition[1]] == " ":
      joiningwalls.append([(cellposition[0]+1)%20,cellposition[1]])
    if Map[(cellposition[0]-2)%20][cellposition[1]] == " ":
      joiningwalls.append([(cellposition[0]-1)%20,cellposition[1]])
    if Map[cellposition[0]][(cellposition[1]+2)%40] == " ":
      joiningwalls.append([cellposition[0],(cellposition[1]+1)%40])
    if Map[cellposition[0]][(cellposition[1]-2)%40] == " ":
      joiningwalls.append([cellposition[0],(cellposition[1]-1)%40])

    joiningcell = random.choice(joiningwalls)
    Map[joiningcell[0]][joiningcell[1]] = " "
    Map[cellposition[0]][cellposition[1]] = " "

    position = cellposition
    Wall_List.remove(cellposition)

  return Map

Map = Prim(Map)
printout(*Map)

Upvotes: 1

Views: 175

Answers (0)

Related Questions