-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
101 lines (88 loc) · 3.74 KB
/
solver.py
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
101
import matplotlib.pyplot as plt
def check_path(maze):
solver = Solver(maze)
width = solver.maze.shape[0]
length = solver.maze.shape[1]
while True:
solver.make_one_move()
solver.show_maze()
if solver.is_game_stuck:
if solver.current_position == (width-1,length-1):
return True
else:
return False
if solver.current_position == (0,0) and not solver.available_start_directions:
return False
if solver.current_position == (width-1,length-1):
return True
class Solver():
def __init__(self, maze, start_position = (0,0), end_position = None, direction = 'S'):
self.maze = maze
self.current_position = start_position
self.direction = direction
if not end_position:
self.end_position = (self.maze.shape[0]-1, self.maze.shape[1]-1)
self.is_game_stuck = False
self.available_start_directions = ['W','S']
def make_one_move(self):
temp_checked = []
if self.current_position == (0,0):
self.direction = self.available_start_directions[0]
self.available_start_directions.remove(self.direction)
else:
start_checking_in_direction = {'W':'E', 'N':'S', 'E':'W', 'S':'N'}
self.direction = start_checking_in_direction[self.direction]
while True:
self.set_new_direction()
new_position = self.find_new_position()
if self.is_move_valid(new_position) and not self.maze[new_position]:
self.current_position = new_position
break
elif 'W' in temp_checked and 'E' in temp_checked and 'S' in temp_checked and 'N' in temp_checked:
self.is_game_stuck = True
break
else:
temp_checked.append(self.direction)
def find_new_position(self):
if self.direction == 'N':
new_position = (self.current_position[0]-1, self.current_position[1])
elif self.direction == 'E':
new_position = (self.current_position[0], self.current_position[1]+1)
elif self.direction == 'S':
new_position = (self.current_position[0]+1, self.current_position[1])
elif self.direction == 'W':
new_position = (self.current_position[0], self.current_position[1]-1)
else:
print('The direction should be a single letter chosen from: N,E,S,W.')
return new_position
def is_move_valid(self,position):
horizontal_size = len(self.maze[0])
vertical_size = len(self.maze)
if (position[1] == -1 or position[1] == horizontal_size
or position[0] == -1 or position[0] == vertical_size):
return False
else:
return True
def set_new_direction(self):
directions_clockwise = {'W':'S', 'N':'W', 'E':'N', 'S':'E'}
self.direction = directions_clockwise[self.direction]
def show_maze(self):
if self.direction == 'N':
marker = "^"
elif self.direction == 'S':
marker = "v"
elif self.direction == 'E':
marker = ">"
elif self.direction == 'W':
marker = "<"
fig = plt.subplots(figsize = [2,2])
plt.imshow(self.maze, cmap = 'bone_r')
plt.scatter(self.current_position[1],self.current_position[0],marker=marker,c='red',s=100)
plt.show()
# def string_to_array(input_str):
# maze_width = input_str.find('\n')
# maze_length = input_str.count('\n') + 1
# input_str = input_str.replace('W','1,')
# input_str = input_str.replace('.','0,')
# maze_array = np.fromstring(input_str, sep=',').reshape(maze_width,maze_length)
# return maze_array