This is a design data structure and movement for a tilt maze. For example, this one.
My major idea is to divide into Ball
, Maze
, HorizontalWall
and VerticalWall
classes. Ball
is the core class which operates on Maze
, HorizontalWall
and VerticalWall
.
I feel my code is a bit inefficient since each time, I need to enumerate all vertical or horizontal walls, for move right/left/up/down. I'm not sure if there's a more efficient way to represent the data structure and movement implementation.
class VerticalWall: # something like |
def __init__(self, x, y1, y2):
self.x=x
self.y_start = y1
self.y_end = y2
class HorizontalWall: # soemthing like --
def __init__(self, x1, x2, y):
self.x_start = x1
self.x_end = x2
self.y = y
class Ball:
def __init__(self, top, left, maze):
self.top = top
self.left = left
self.maze = maze
def moveRight(self):
x_location=[]
y_location=[]
for i in maze.vertical_wall:
x_location.append(i.x)
y_location.append(i.y_start)
while True:
for i,x in enumerate(x_location):
if self.left + 1 == x and y_location[i] == self.top: # met with a vertical wall
return
if self.left+1==self.maze.width: # border
return
else:
self.left += 1
def moveLeft(self):
x_location=[]
y_location=[]
for i in maze.vertical_wall:
x_location.append(i.x)
y_location.append(i.y_start)
while True:
for i,x in enumerate(x_location):
if self.left == x and y_location[i] == self.top: # met with a vertical wall
return
if self.left == 0: # border
return
else:
self.left -= 1
def moveUp(self):
x_location=[]
y_location=[]
for i in maze.horizontal_wall:
x_location.append(i.x_start)
y_location.append(i.y)
while True:
for i,x in enumerate(x_location):
if self.left == x and y_location[i] == self.top: # met with a vertical wall
return
if self.top == 0: # border
return
else:
self.top -= 1
def moveDown(self):
x_location=[]
y_location=[]
for i in maze.horizontal_wall:
x_location.append(i.x_start)
y_location.append(i.y)
while True:
for i,x in enumerate(x_location):
if self.left == x and y_location[i] == self.top+1: # met with a vertical wall
return
if self.top + 1 == self.maze.height: # border
return
else:
self.top += 1
class Maze:
def __init__(self, height, width, target_x, target_y):
self.height = height
self.width = width
self.target_x=target_x
self.target_y=target_y
self.horizontal_wall=[]
self.vertical_wall=[]
if __name__ == "__main__":
# https://www.mathsisfun.com/games/tilt-maze.html
maze = Maze(5,5,4,4)
maze.horizontal_wall.append(HorizontalWall(1, 2, 1))
maze.horizontal_wall.append(HorizontalWall(4, 5, 2))
maze.horizontal_wall.append(HorizontalWall(2, 3, 3))
maze.horizontal_wall.append(HorizontalWall(1, 2, 4))
maze.horizontal_wall.append(HorizontalWall(4, 5, 4))
maze.vertical_wall.append(VerticalWall(3, 0, 1))
maze.vertical_wall.append(VerticalWall(1, 2, 3))
maze.vertical_wall.append(VerticalWall(3, 4, 5))
ball = Ball(0,0,maze)
print ('current location ', ball.top, ball.left)
ball.moveRight()
print ('current location ', ball.top, ball.left)
ball.moveDown()
print ('current location ', ball.top, ball.left)
ball.moveLeft()
print ('current location ', ball.top, ball.left)
ball.moveUp()
print ('current location ', ball.top, ball.left)