This is a design data structure and movement for a tilt maze. For example, this one.
My major idea is learned from abrarisme's smart idea of using two boolean matrix to represent wall. Please refer to this link for abrarisme's smart idea (Data structure and movement for tilt maze).
I need to (1) design the data structure to represent maze and (2) also implement movement function for 4 directions.
Since it is new code and I create this new post. Appreciate for code review advice on point (1) and point (2). Thanks!
An example of right matrix and related maze,
__ __ __ __
|__ |__ | [[true, false, true, false]
| | | | [false, false, true, false],
| | __| | => [false, true, false, false],
| | __| [true, false, true, false],
|__|__|__ __| [false, false, true, false]]
right = [[True, False, True, False],
[False, False, True, False],
[False, True, False, False],
[True, False, True, False],
[False, False, True, False]]
top = [[False, False, False, False],
[False, True, False, True],
[True, True, True, True],
[True, True, False, True],
[True, True, True, False]]
def move_right (start_x, start_y):
while start_y < len(right)-1 and right[start_x][start_y] == True:
start_y+=1
return (start_x, start_y)
def move_left(start_x, start_y):
while start_y >= 1 and right[start_x][start_y-1] == True:
start_y -= 1
return (start_x, start_y)
def move_top(start_x, start_y):
while start_x > 0 and top[start_x][start_y] == True:
start_x-=1
return (start_x, start_y)
def move_down(start_x, start_y):
while start_x < len(top) - 1 and top[start_x+1][start_y] == True:
start_x+=1
return (start_x, start_y)
if __name__ == "__main__":
(x,y) = move_right(0,0)
print(x,y) # output, (0, 1)
(x,y) = move_down(x,y)
print (x,y) # output, (4, 1)
(x, y) = move_left(x, y)
print (x,y) # output, (4, 1)
(x, y) = move_top(x, y)
print (x,y) # output, (0, 1)