forked from chuyangliu/snake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.py
171 lines (135 loc) · 5.2 KB
/
path.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# pylint: disable=C0111,E1101
import sys
import random
from collections import deque
from snake.base import Direc, PointType
from snake.solver.base import BaseSolver
class _TableCell:
def __init__(self):
self.reset()
def __str__(self):
return "{ dist: %d parent: %s visit: %d }" % \
(self.dist, str(self.parent), self.visit)
__repr__ = __str__
def reset(self):
# Shortest path
self.parent = None
self.dist = sys.maxsize
# Longest path
self.visit = False
class PathSolver(BaseSolver):
def __init__(self, snake):
super().__init__(snake)
self.__table = [[_TableCell() for _ in range(snake.map.num_cols)]
for _ in range(snake.map.num_rows)]
@property
def table(self):
return self.__table
def shortest_path_to_food(self):
return self.path_to(self.map.food, "shortest")
def longest_path_to_tail(self):
return self.path_to(self.snake.tail(), "longest")
def path_to(self, des, path_type):
ori_type = self.map.point(des).type
self.map.point(des).type = PointType.EMPTY
if path_type == "shortest":
path = self.shortest_path_to(des)
elif path_type == "longest":
path = self.longest_path_to(des)
self.map.point(des).type = ori_type # Restore origin type
return path
def shortest_path_to(self, des):
"""Find the shortest path from the snake's head to the destination.
Args:
des (snake.base.pos.Pos): The destination position on the map.
Returns:
A collections.deque of snake.base.direc.Direc indicating the path directions.
"""
self.__reset_table()
head = self.snake.head()
self.__table[head.x][head.y].dist = 0
queue = deque()
queue.append(head)
while queue:
cur = queue.popleft()
if cur == des:
return self.__build_path(head, des)
# Arrange the order of traverse to make the path as straight as possible
if cur == head:
first_direc = self.snake.direc
else:
first_direc = self.__table[cur.x][cur.y].parent.direc_to(cur)
adjs = cur.all_adj()
random.shuffle(adjs)
for i, pos in enumerate(adjs):
if first_direc == cur.direc_to(pos):
adjs[0], adjs[i] = adjs[i], adjs[0]
break
# Traverse adjacent positions
for pos in adjs:
if self.__is_valid(pos):
adj_cell = self.__table[pos.x][pos.y]
if adj_cell.dist == sys.maxsize:
adj_cell.parent = cur
adj_cell.dist = self.__table[cur.x][cur.y].dist + 1
queue.append(pos)
return deque()
def longest_path_to(self, des):
"""Find the longest path from the snake's head to the destination.
Args:
des (snake.base.pos.Pos): The destination position on the map.
Returns:
A collections.deque of snake.base.direc.Direc indicating the path directions.
"""
path = self.shortest_path_to(des)
if not path:
return deque()
self.__reset_table()
cur = head = self.snake.head()
# Set all positions on the shortest path to 'visited'
self.__table[cur.x][cur.y].visit = True
for direc in path:
cur = cur.adj(direc)
self.__table[cur.x][cur.y].visit = True
# Extend the path between each pair of the positions
idx, cur = 0, head
while True:
cur_direc = path[idx]
nxt = cur.adj(cur_direc)
if cur_direc == Direc.LEFT or cur_direc == Direc.RIGHT:
tests = [Direc.UP, Direc.DOWN]
elif cur_direc == Direc.UP or cur_direc == Direc.DOWN:
tests = [Direc.LEFT, Direc.RIGHT]
extended = False
for test_direc in tests:
cur_test = cur.adj(test_direc)
nxt_test = nxt.adj(test_direc)
if self.__is_valid(cur_test) and self.__is_valid(nxt_test):
self.__table[cur_test.x][cur_test.y].visit = True
self.__table[nxt_test.x][nxt_test.y].visit = True
path.insert(idx, test_direc)
path.insert(idx + 2, Direc.opposite(test_direc))
extended = True
break
if not extended:
cur = nxt
idx += 1
if idx >= len(path):
break
return path
def __reset_table(self):
for row in self.__table:
for col in row:
col.reset()
def __build_path(self, src, des):
path = deque()
tmp = des
while tmp != src:
parent = self.__table[tmp.x][tmp.y].parent
path.appendleft(parent.direc_to(tmp))
tmp = parent
return path
def __is_valid(self, pos):
return self.map.is_safe(pos) and not self.__table[pos.x][pos.y].visit