forked from mackorone/breadth-first-search
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
100 lines (85 loc) · 2.78 KB
/
Copy pathMain.py
File metadata and controls
100 lines (85 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import API
import collections
from Direction import Direction
from Maze import Maze
from Mouse import Mouse
def main():
maze = Maze(API.mazeWidth(), API.mazeHeight())
mouse = Mouse(0, 0, Direction.NORTH)
while not maze.inCenter(mouse.getPosition()):
updateWalls(maze, mouse)
moveOneCell(maze, mouse)
def updateWalls(maze, mouse):
position = mouse.getPosition()
direction = mouse.getDirection()
if API.wallFront():
maze.setWall(position, direction)
if API.wallLeft():
maze.setWall(position, direction.turnLeft())
if API.wallRight():
maze.setWall(position, direction.turnRight())
def moveOneCell(maze, mouse):
# Compute the next direction
currentX, currentY = mouse.getPosition()
nextX, nextY = getNextCell(maze, mouse)
if nextX < currentX:
nextDirection = Direction.WEST
if nextY < currentY:
nextDirection = Direction.SOUTH
if nextX > currentX:
nextDirection = Direction.EAST
if nextY > currentY:
nextDirection = Direction.NORTH
# Turn and move to the next cell
currentDirection = mouse.getDirection()
if currentDirection.turnLeft() == nextDirection:
mouse.turnLeft()
elif currentDirection.turnRight() == nextDirection:
mouse.turnRight()
elif currentDirection != nextDirection:
mouse.turnAround()
mouse.moveForward()
def getNextCell(maze, mouse):
initial = mouse.getPosition()
center = None
ancestors = {}
queue = collections.deque([initial])
while queue:
current = queue.popleft()
for direction in Direction:
neighbor = getNeighbor(current, direction)
# If the neighbor is out of bounds, skip
if not maze.contains(neighbor):
continue
# If the neighbor is blocked by wall, skip
if maze.getWall(current, direction):
continue
# If the neighbor is already discovered, skip
if neighbor in ancestors:
continue
# Add the neighbor to queue and update ancestors
queue.append(neighbor)
ancestors[neighbor] = current
if maze.inCenter(neighbor):
center = neighbor
# If a center cell was found, stop searching
if center:
break
# Walk backwards from the center
position = center
while ancestors[position] != initial:
position = ancestors[position]
return position
def getNeighbor(current, direction):
x, y = current
if direction == Direction.NORTH:
y += 1
if direction == Direction.EAST:
x += 1
if direction == Direction.SOUTH:
y -= 1
if direction == Direction.WEST:
x -= 1
return (x, y)
if __name__ == "__main__":
main()