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

Edited 4 Years Ago by sym366

Attachments
23 50
**************************************************
*******                              ******** ****
******* ************** ************* ******** ****
******* ************** ***         * ***      ****
P       ************** **  ******  * *** **** ****
******* ***         ** *  *******        **** ****
******* *** ******* ** *  ******************* ****
******* *** ******* **    ******************* ****
******* *** ******* ************************* ****
******* *** **                      ********* ****
***     *** ** **** **** ******************** ****
*** ********** **** ****               ****** ****
*** ********** **** ************************* ****
*** ********** **** ************************* ****
***            **** ************ ************ ****
******** ********** ************ ************ ****
******** ********** ************      ******* ****
********      ***** ************ **** ******* ****
*******************              **** ******* ****
************************************* ******* ****
************************************* ************
*************************************            T
**************************************************
Enter a file name for the maze: **************************************************
*******                              ******** ****
******* ************** ************* ******** ****
******* ************** ***         * ***      ****
P       ************** **  ******  * *** **** ****
******* ***         ** *  *******        **** ****
******* *** ******* ** *  ******************* ****
******* *** ******* **    ******************* ****
******* *** ******* ************************* ****
******* *** **                      ********* ****
***     *** ** **** **** ******************** ****
*** ********** **** ****               ****** ****
*** ********** **** ************************* ****
*** ********** **** ************************* ****
***            **** ************ ************ ****
******** ********** ************ ************ ****
******** ********** ************      ******* ****
********      ***** ************ **** ******* ****
*******************              **** ******* ****
************************************* ******* ****
************************************* ************
*************************************            T
**************************************************

Maze solved: 
**************************************************
*******                              ******** ****
******* ************** ************* ******** ****
*******$************** ***         * ***      ****
$$$$$$$$************** **  ******  * *** **** ****
*******$***         ** *  *******        **** ****
*******$*** ******* ** *  ******************* ****
*******$*** ******* **    ******************* ****
*******$*** *******$************************* ****
*******$*** **$$$$$$................********* ****
***$$$$$*** **$****$****.******************** ****
***$**********$****$****...............****** ****
***$**********$****$************************* ****
***$**********$****$************************* ****
***$$$$$$$$$$$$****$************ ************ ****
********$**********$************$************ ****
******** **********$************$$$$$$******* ****
********      *****$************$****$******* ****
*******************$$$$$$$$$$$$$$****$******* ****
*************************************$******* ****
*************************************$************
*************************************$$$$$$$$$$$$T
**************************************************

(21, 49) (21, 48) (21, 47) (21, 46) (21, 45) (21, 44) (21, 43) (21, 42) (21, 41) (21, 40) (21, 39) (21, 38) (21, 37) (20, 37) (19, 37) (18, 37) (17, 37) (16, 37) (16, 36) (16, 35) (16, 34) (16, 33) (15, 32) (16, 32) (17, 32) (18, 32) (18, 31) (18, 30) (18, 29) (18, 28) (18, 27) (18, 26) (18, 25) (18, 24) (18, 23) (18, 22) (18, 21) (18, 20) (18, 19) (17, 19) (16, 19) (15, 19) (14, 19) (13, 19) (12, 19) (11, 19) (10, 19) (8, 19) (9, 19) (9, 18) (9, 17) (9, 16) (9, 15) (9, 14) (10, 14) (11, 14) (12, 14) (13, 14) (14, 14) (14, 13) (14, 12) (14, 11) (14, 10) (14, 9) (15, 8) (14, 8) (14, 7) (14, 6) (14, 5) (14, 4) (14, 3) (13, 3) (12, 3) (11, 3) (10, 3) (10, 4) (10, 5) (10, 6) (10, 7) (9, 7) (8, 7) (7, 7) (6, 7) (5, 7) (3, 7) (4, 7) (4, 6) (4, 5) (4, 4) (4, 3) (4, 2) (4, 1) (4, 0)

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).
This question has already been answered. Start a new discussion instead.