I upload my test_maze.txt so you can see my problem. I have some 3 unwanted $ that went up and 1 unwanted $ that went down on my path on the maze from P to T. I think the problem is in line 20 in the def main(): of my maze.py I need help fixing that if loop.

"""
File: maze.py
"""
from grid import Grid
from stack import ArrayStack

def main():
    maze = getMazeFromFile()
    tempstack = ArrayStack()
    print maze
    (startRow, startCol) = findStartPos(maze)
    success = getOut(startRow, startCol, maze)
    if success != None:
        print "Maze solved: "
        (col,row) = success.pop()
        tempstack.push((col,row))
        (pcol,prow) = (col,row)
        while not success.isEmpty():
            (col,row) = success.pop()
            if (pcol,prow): 
                tempstack.push((col,row))
                maze[col][row] = "$"
        print maze
        print "Path: " + tempstack

    else:
        print "No path out of this maze"

def getMazeFromFile():
    name = raw_input("Enter a file name for the maze: ")
    fileObj = open("maze.txt", 'r')
    firstLine = map(int, fileObj.readline().strip().split())
    rows = firstLine[0]
    columns = firstLine[1]
    maze = Grid(rows, columns, "*")
    for row in xrange(rows):
        line = fileObj.readline().strip()
        column = 0
        for ch in line:
            maze[row][column] = ch
            column += 1
    return maze

def findStartPos(maze):
    for row in xrange(maze.getHeight()):
        for column in xrange(maze.getWidth()):
            if maze[row][column] == 'P':
                return (row, column)
    return (-1, -1)

def getOut(row, column, maze):
    stack = ArrayStack()
    stack.push((row, column))

    while not stack.isEmpty():
        (row, column) = stack.peek()
        if maze[row][column] == 'T':
            return stack
        elif maze[row][column] != '.':
            maze[row][column] = '.'
            counter = 0
            if row != 0 and not maze[row - 1][column] in ('*', '.'):
                stack.push((row - 1, column))
                counter += 1
            if row + 1 != maze.getHeight() and not maze[row + 1][column] in ('*','.'):
                stack.push((row + 1, column))
                counter += 1
            if column + 1 != maze.getWidth() and not maze[row][column + 1] in ('*','.'):
                stack.push((row, column + 1))
                counter += 1
            if column != 0 and not maze[row][column - 1] in ('*','.'):
                stack.push((row, column - 1))
                counter += 1
        if counter == 0:
            stack.pop()
    return None
main()

These are the other file's codes in order to run my maze.py

"""
File: stack.py
"""
import math 
from arrays import Array
class ArrayStack(object):
    """Represents a stack using Array."""
    def __init__(self):
        self._items = Array(0, None)
        self._top = -1

    def __len__(self):
        """The number of items in the stack."""
        return self._top + 1

    def __str__(self):
        """The string representation of the stack."""
        s = ""
        for i in xrange(self._top + 1):
            s += str(self._items[i]) + " "
        return s

    def isEmpty(self):
        return self._top == -1

    def peek(self):
        """Precondition: stack is not empty."""
        return self._items[self._top]

    def push(self,newindex):
        """Add new item to top of stack."""
        self._top += 1
        self._items[self._top] = newindex
    def pop(self):
        tmp = self._items[self._top]
        self._top -= 1
        return tmp

"""
File: arrays.py
"""
import math 
class Array(object):
    def __init__(self,capacity,fillValue = None):
        self._items = list()
        for count in xrange(capacity):
            self._items.append(fillValue)
    def __len__(self):
        return len(self._items)

    def __str__(self):
        s = ""
        for i in self._items:
            s += "%1s" %(str(i))
        return s

    def __iter__(self):
        return iter(self._items)

    def __getitem__(self,index):
        return self._items[index]

    def __setitem__(self,index,newItem):
        if(index == len(self._items)):
            self._items.append(newItem)
        else:
            self._items[index] = newItem

"""
File: grid
"""
from arrays import Array

class Grid(Array):
    def __init__(self, rows, columns, fillValue = None):
        self._data = Array(rows)
        for row in xrange(rows):
            self._data[row] = Array(columns, fillValue)
    def getHeight(self):
        return len(self._data)
    def getWidth(self):
        return len(self._data[0])
    def __getitem__(self, index):
        return self._data[index]
    def __str__(self):
        result = ""
        for row in xrange(self.getHeight()):
            for col in xrange(self.getWidth()):
                result += str(self._data[row][col]) + ""
            result += "\n"
        return result

I do not know yet, but you are asking the file name, but are not using it.

Oh those are seperate files that I need to run my maze.py. So when I run my maze.py, I'll get a maze and its solved maze. The problem is in my solved maze. I have 3 unwanted $ that went up and 1 unwanted $ that went down on my path on the maze from P to T.

Enter a file name for the maze:  
**************************************************
*******                              ******** ****
******* ************** ************* ******** ****
******* ************** ***         * ***      ****
P       ************** **  ******  * *** **** ****
******* ***         ** *  *******        **** ****
******* *** ******* ** *  ******************* ****
******* *** ******* **    ******************* ****
******* *** ******* ************************* ****
******* *** **                      ********* ****
***     *** ** **** **** ******************** ****
*** ********** **** ****               ****** ****
*** ********** **** ************************* ****
*** ********** **** ************************* ****
***            **** ************ ************ ****
******** ********** ************ ************ ****
******** ********** ************      ******* ****
********      ***** ************ **** ******* ****
*******************              **** ******* ****
************************************* ******* ****
************************************* ************
*************************************            T
**************************************************

Maze solved: 
 **************************************************
*******                              ******** ****
******* ************** ************* ******** ****
*******$************** ***         * ***      ****
$$$$$$$$************** **  ******  * *** **** ****
*******$***         ** *  *******        **** ****
*******$*** ******* ** *  ******************* ****
*******$*** ******* **    ******************* ****
*******$*** *******$************************* ****
*******$*** **$$$$$$................********* ****
***$$$$$*** **$****$****.******************** ****
***$**********$****$****...............****** ****
***$**********$****$************************* ****
***$**********$****$************************* ****
***$$$$$$$$$$$$****$************ ************ ****
********$**********$************$************ ****
******** **********$************$$$$$$******* ****
********      *****$************$****$******* ****
*******************$$$$$$$$$$$$$$****$******* ****
*************************************$******* ****
*************************************$************
*************************************$$$$$$$$$$$$T
**************************************************
****    *******
****5**********
P1234789*******
****6**********
****$**********
****$T*********
***************

Your Stack is filled / emptied like this:

1) 1, 2, 3, 4
2) 1, 2, 3, 4, 5, 6, 7
3) 1, 2, 3, 4, 5, 6, 7, 8
4) 1, 2, 3, 4, 5, 6, 7, 8, 9
5) 1, 2, 3, 4, 5, 6, 7, 8
6) 1, 2, 3, 4, 5, 6, 7
7) 1, 2, 3, 4, 5, 6 ---<--- and from this state we find the path to "T",

but cell with "5" in it is still in the stack (but should be popped up).
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.