Search code examples
python-3.xpath-findingmaze

Getting a matrix for a Maze; Python


I have created a successful path finding algorithm on Python. It works on any sized matrices like this:

maze=[[' ',' ','X','X',' ','S'],
      [' ',' ',' ',' ','X',' '],
      ['F','X',' ',' ',' ',' '],
      [' ',' ',' ',' ','X',' '],
      [' ','X',' ',' ',' ',' ']]

The S is the starting point, the X marks walls and the F is the finish point.

Where can I get a maze in the same format as this?


Solution

  • You can create a text file named as input.txt and read from that test file in list.

    input.txt

      XX S
        X 
    FX    
        X 
     X    
    

    read from file into list like this:

    with open('C:/input.txt') as f:
        maze = f.read().splitlines()
    

    where maze is your list. It will be same as you shown above.

    And then for verification you can print like this (which shows you the contents of input.txt):

    for sublist in maze:
        print(''.join(sublist))