-5

I am trying to create the exact maze in the diagram shown with python codes and I'm having a bit of a hiccup. I'm aware that one can use matplotlib with an array of 1's and 0's to plot it out. But still I don't get it.

Could someone help me out and guide me please. Thank you.

Ps: I'm still new to python but it doesn't matter how complex the code is, I'll try to understand it. Thanks a lot

enter image description here

SPYBUG96
  • 1,089
  • 5
  • 20
  • 38

1 Answers1

4

Following your maze, I translated to the equivalent structure found here:

maze.txt

+-+-+-+-+ +-+-+-+-+-+
|   |             | |
+ + + +-+-+-+ +-+ + +
| | |   |   | | |   |
+ +-+-+ +-+ + + + +-+
| |   |   | |   |   |
+ + + + + + + +-+ +-+
|   |   |   | |     |
+-+-+-+-+-+-+-+ +-+ +
|             |   | |
+ +-+-+-+-+ + +-+-+ +
|   |       |       |
+ + + +-+ +-+ +-+-+-+
| | |   |     |     |
+ +-+-+ + +-+ + +-+ +
| |     | | | |   | |
+-+ +-+ + + + +-+ + +
|   |   |   |   | | |
+ +-+ +-+-+-+-+ + + +
|   |       |     | |
+-+-+-+-+-+ +-+-+-+-+

And then modified a bit the code to make it more readable:

import matplotlib.pyplot as plt


maze = []
with open("maze.txt", 'r') as file:
    for line in file:
        line = line.rstrip()
        row = []
        for c in line:
            if c == ' ':
                row.append(1) # spaces are 1s
            else:
                row.append(0) # walls are 0s
        maze.append(row)

plt.pcolormesh(maze)
plt.axes().set_aspect('equal') #set the x and y axes to the same scale
plt.xticks([]) # remove the tick marks by setting to an empty list
plt.yticks([]) # remove the tick marks by setting to an empty list
plt.axes().invert_yaxis() #invert the y-axis so the first row of data is at the top
plt.show()

enter image description here