0

I am making a game and the board is a 2D Array, I have a procedure to print the maze and each time it prints it it should show an 'X' where the player is standing. The problem is it keeps an 'X' where the player was standing:

-1 X -1 -1 
-1 2 3 -1 
-1 -1 4 5 
-1 6 7 -1  
 9 8 -1 a  
 b -1 -1 -1  
 c d -1 -1 
-1 e f -1  
 1 -1 h g

2 X -1 -1 
-1 X 3 -1 
-1 -1 4 5 
-1 6 7 -1 
9 8 -1 a 
b -1 -1 -1 
c d -1 -1 
-1 e f -1 
1 -1 h g 

There are two 'X's when there should only be one but I don't know why.

def printMaze(maze, x, y):
    oldRoom = maze[y][x]
    u = x - 1
    v = y - 1
    maze[v][u] = oldRoom
    maze[y][x] = 'X'
    for i in range(9):
        for z in range(4):
            print str(maze[i][z]).rjust(2),
        print ''
2
  • This doesn't answer your question, but may improve readability for people looking at it and trying to figure out a solution. If you change your print line to be print str(maze[i][z]).rjust(2), the characters should all be aligned in the printed maze.
    – Blair
    Jun 19, 2015 at 19:26
  • @Brien That's perfect thanks, I was gonna look for a way to make it look better after I had it working.
    – Bruce W
    Jun 19, 2015 at 19:31

3 Answers 3

0

If x and y are the coordinates of the player, then the first thing you do is set oldRoom equal to where the player is. Isn't that where the X is currently?

Instead of shuffling around the values in your maze array, why not just check if the current location you're printing is where the player is, and print the X instead?

def printMaze(maze, x, y):
    for i in range(9):
        for z in range(4):
            if i == x and z == y:
                print 'X',
            else:
                print maze[i][z],
        print ''
0
0

In your example, you move from (0,1) to (1,1) but moving the element at (1,1) to (0,0). So you have to give printMaze the new and the old position.

def printMaze(maze, x, y, old_x, old_y):
    maze[old_y][old_x] = maze[y][x]
    maze[y][x] = 'X'
    for row in maze:
        for cell in row:
            print cell,
        print ''
1
  • But old_x(u) and old_y(v) are local variables. They only appear within that procedure. I now understand what's wrong though it will just take a bit to figure out how to solve it.
    – Bruce W
    Jun 19, 2015 at 19:35
-1

The problem is you must store the last pos where the "X" was, and then use this last pos to set the u and v variables.

old_x = old_y = None

def printMaze(maze, x, y):
    global old_x, old_y
    if old_x and old_y:
        maze[old_y][old_x] = maze[y][x]
    old_x, old_y = x, y
    maze[y][x] = 'X'
    for i in range(9):
        for z in range(4):
            print maze[i][z],
        print ''
2
  • this gives an UnboundLocalError.
    – Daniel
    Jun 19, 2015 at 19:48
  • I forgot the global statement (updated). Compared to the accepted answer, i proposed this answer because I thought it was part of your game to move the old value where the X was before, like some puzzles! Sorry for late response
    – yolenoyer
    Jun 20, 2015 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.