1
\$\begingroup\$

I have implemented a maze with

  • a function addCoordinate where x and y denote the x and y coord of grid and block type: zero means open and 1 means wall.
  • a printMaze function which prints the maze with * for wall and empty space for open spaces.

My implementation seems to be working fine but could there be a better way of doing this:

class Maze:
    board, max_x, max_y, list = [],2,2,[]

    def __init__(self):
        """
        Constructor - You may modify this, but please do not add any extra parameters
        """

    def addCoordinate(self, x, y, blockType):
        """
        Add information about a coordinate on the maze grid
        x is the x coordinate
        y is the y coordinate
        blockType should be 0 (for an open space) of 1 (for a wall)
        """
        if x > self.max_x:
            self.max_x = x
        if y > self.max_y:
            self.max_y = y
        if self.max_y >= len(self.board) or self.max_x >= len(self.board[0]):
            newboard = [[1 for a in range(self.max_x + 1)] for b in range(self.max_y + 1)]
            for i in range(len(self.board)):
                for j in range(len(self.board[i])):
                    newboard[i][j] = self.board[i][j]
            self.board = newboard
        self.board[y][x] = blockType
    def printMaze(self):
        """
        Print out an ascii representation of the maze.
        A * indicates a wall and a empty space indicates an open space in the maze
        """

        for i in range(self.max_x + 1):
            for j in range(self.max_y + 1):
                if self.board[i][j] == 0:
                    print " ",
                else:
                    print "*",
            print ""


def mazeTest():
    """
    This sets the open space coordinates for the example
    maze in the assignment.
    The remainder of coordinates within the max bounds of these specified coordinates
    are assumed to be walls
    """
    myMaze = Maze()
    myMaze.addCoordinate(1, 0, 0)
    myMaze.addCoordinate(1, 1, 0)
    myMaze.addCoordinate(7, 1, 0)
    myMaze.addCoordinate(1, 2, 0)
    myMaze.addCoordinate(2, 2, 0)
    myMaze.addCoordinate(3, 2, 0)
    myMaze.addCoordinate(4, 2, 0)
    myMaze.addCoordinate(6, 2, 0)
    myMaze.addCoordinate(7, 2, 0)
    myMaze.addCoordinate(4, 3, 0)
    myMaze.addCoordinate(7, 3, 0)
    myMaze.addCoordinate(4, 4, 0)
    myMaze.addCoordinate(7, 4, 0)
    myMaze.addCoordinate(3, 5, 0)
    myMaze.addCoordinate(4, 5, 0)
    myMaze.addCoordinate(7, 5, 0)
    myMaze.addCoordinate(1, 6, 0)
    myMaze.addCoordinate(2, 6, 0)
    myMaze.addCoordinate(3, 6, 0)
    myMaze.addCoordinate(4, 6, 0)
    myMaze.addCoordinate(6, 6, 0)
    myMaze.addCoordinate(7, 6, 0)
    myMaze.addCoordinate(5, 7, 0)

    myMaze.printMaze()

mazeTest()

Output:

*   * * * * * * 
*   * * * * *   
*         *     
* * * *   * *   
* * * *   * *   
* * *     * *   
*         *     
* * * * *   * * 
\$\endgroup\$
  • \$\begingroup\$ Is this really how you want each maze to be specified — one element at a time? \$\endgroup\$ – 200_success Nov 23 '17 at 18:25
  • \$\begingroup\$ yes,that's what i could think of \$\endgroup\$ – Dkk Nov 23 '17 at 18:28
  • \$\begingroup\$ Is there any other way in which we could specify the maze \$\endgroup\$ – Dkk Nov 23 '17 at 19:25
  • \$\begingroup\$ You could generate the maze more procedural, see Wikipedia \$\endgroup\$ – Ludisposed Nov 24 '17 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.