-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
218 lines (183 loc) · 9.33 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import copy
import re
from agentController import AgentController
from customParser import read_map, infer_information
import os
from datetime import datetime
def find_path(start,old,check_style,offset,direction,map_game,gameController):
gameController.updatePerceiveAgent(start,map_game,old,check_style,direction)
if gameController.checkAgentDie(start,check_style):
path_find, point_path,fire,list_direction,grab,map_list, check = gameController.getReturnParameter()
return path_find, point_path,fire,list_direction,grab,map_list, check
path_can_go = gameController.getPathCanGo(start,check_style,offset,direction,old)
if path_can_go[-1] == "StopUnsure":
return path_can_go
if path_can_go[-1] == "Stop":
return path_can_go
path_can_go_now = path_can_go[0]
for step in path_can_go_now:
path_find, point_path,fire,list_direction,grab,map_list, check = find_path((step[0], step[1]),start,step[2],step[3],step[4],step[5],gameController)
if check == "Stop":
return path_find,point_path,fire,list_direction,grab,map_list, "Stop"
if check == "StopUnsure":
return path_find,point_path,fire,list_direction,grab,map_list, "StopUnsure"
def getParameterUI(path_name,i):
map_game = read_map(path_name)
if map_game is None:
return None
world_map , start = infer_information(map_game)
N = len(world_map)
start_position = start
old_position = (-1,-1)
check_style = 0
offset = None
direction = "right"
map_game_copy = copy.deepcopy(world_map)
gameController = AgentController(N,world_map,i)
path_list, point_list,shoot_list,direction_list,grab_list,map_list, _ = find_path(start_position,old_position,check_style, offset,direction,map_game_copy,gameController)
final_position = path_list[-1]
if final_position == (N-1,0):
point_list[-1] -= 10
temp_n = len(path_list)
temp_past_list = []
temp_point_list = []
temp_shoot_list = []
temp_direction_list = []
temp_grab_list = []
temp_map_list = []
action_list = []
for i in range(temp_n):
if shoot_list[i][0] != 1:
if i == 0:
action_list.append("Start")
else:
action_list.append("Forward")
temp_past_list.append(path_list[i])
temp_point_list.append(point_list[i])
temp_shoot_list.append((0,0))
temp_grab_list.append(0)
temp_map_list.append(copy.deepcopy(map_list[i]))
temp_direction_list.append(direction_list[i])
if shoot_list[i][0] == 1:
action_list.append("Shoot")
temp_past_list.append(path_list[i])
temp_point_list.append(point_list[i])
temp_shoot_list.append(shoot_list[i])
temp_grab_list.append(0)
temp_map_list.append(copy.deepcopy(map_list[i]))
temp_direction_list.append(direction_list[i])
if grab_list[i] == 1:
action_list.append("Grab")
temp_past_list.append(path_list[i])
temp_point_list[-1] -= 1000
temp_point_list.append(point_list[i])
temp_shoot_list.append((0,0))
temp_grab_list.append(grab_list[i])
temp_direction_list.append(direction_list[i])
if i < temp_n:
for update_gold in range(i,temp_n):
x, y = path_list[i]
txt = copy.deepcopy(map_list[update_gold][x][y])
if "G" in txt:
temp = txt[0:txt.index("G")]+txt[txt.index("G")+1:]
map_list[update_gold][x][y]=temp
if map_list[update_gold][x][y] == "":
map_list[update_gold][x][y]='-'
temp_map_list.append(copy.deepcopy(map_list[i]))
if i < temp_n -1:
if direction_list[i] != direction_list[i+1]:
if direction_list[i] == "left" and direction_list[i+1] == "right":
action_list.append(f"Turn {direction_list[i+1]}")
temp_past_list.append(path_list[i])
temp_point_list.append(point_list[i])
temp_shoot_list.append((0,0))
temp_grab_list.append(0)
temp_map_list.append(map_list[i])
temp_direction_list.append("up")
if direction_list[i] == "right" and direction_list[i+1] == "left":
action_list.append(f"Turn {direction_list[i+1]}")
temp_past_list.append(path_list[i])
temp_point_list.append(point_list[i])
temp_shoot_list.append((0,0))
temp_grab_list.append(0)
temp_map_list.append(map_list[i])
temp_direction_list.append("up")
if direction_list[i] == "up" and direction_list[i+1] == "down":
action_list.append(f"Turn left")
temp_past_list.append(path_list[i])
temp_point_list.append(point_list[i])
temp_shoot_list.append((0,0))
temp_grab_list.append(0)
temp_map_list.append(map_list[i])
temp_direction_list.append("left")
if direction_list[i] == "down" and direction_list[i+1] == "up":
action_list.append(f"Turn left")
temp_past_list.append(path_list[i])
temp_point_list.append(point_list[i])
temp_shoot_list.append((0,0))
temp_grab_list.append(0)
temp_map_list.append(map_list[i])
temp_direction_list.append("right")
if direction_list[i] == "left" and direction_list[i+1] == "up":
action_list.append(f"Turn right")
elif direction_list[i] == "right" and direction_list[i+1] == "up":
action_list.append(f"Turn left")
elif direction_list[i] == "left" and direction_list[i+1] == "down":
action_list.append(f"Turn left")
elif direction_list[i] == "right" and direction_list[i+1] == "down":
action_list.append(f"Turn right")
elif direction_list[i] == "up" and direction_list[i+1] == "right":
action_list.append(f"Turn right")
elif direction_list[i] == "down" and direction_list[i+1] == "left":
action_list.append(f"Turn right")
elif direction_list[i+1] == "up" or direction_list[i+1] == "down":
action_list.append(f"Turn left")
else:
action_list.append(f"Turn {direction_list[i+1]}")
temp_past_list.append(path_list[i])
temp_point_list.append(point_list[i])
temp_shoot_list.append((0,0))
temp_grab_list.append(0)
temp_map_list.append(map_list[i])
temp_direction_list.append(direction_list[i+1])
if i == temp_n-1:
if path_list[i] == (N-1,0):
action_list.append("Climb")
temp_point_list.append(point_list[i]+10)
else:
action_list.append("Unsure Stop")
temp_point_list.append(point_list[i])
temp_past_list.append(path_list[i])
temp_shoot_list.append((0,0))
temp_grab_list.append(0)
temp_map_list.append(copy.deepcopy(map_list[i]))
temp_direction_list.append(direction_list[i])
return temp_past_list, temp_point_list,temp_shoot_list,temp_direction_list,temp_grab_list,temp_map_list,action_list
def getParameter2(path_name):
map_game = read_map(path_name)
if map_game is None:
return None
N = len(map_game)
best_past_list, best_point_list,best_shoot_list,best_direction_list,best_grab_list,best_map_list,best_action_list = getParameterUI(path_name,0)
for i in range(1,N*N+1):
try:
temp_past_list, temp_point_list,temp_shoot_list,temp_direction_list,temp_grab_list,temp_map_list,action_list = getParameterUI(path_name,i)
except:
continue
if best_point_list[-1] < temp_point_list[-1]:
best_past_list, best_point_list,best_shoot_list,best_direction_list,best_grab_list,best_map_list,best_action_list = temp_past_list, temp_point_list,temp_shoot_list,temp_direction_list,temp_grab_list,temp_map_list,action_list
result_directory = "result"
if not os.path.exists(result_directory):
os.makedirs(result_directory)
print(f'The directory "{result_directory}" has been created.')
pattern = r'\d+'
namefile = "result"
matches = re.findall(pattern, path_name)
if matches:
namefile = namefile + matches[-1]+ ".txt"
file_path = os.path.join(result_directory, namefile)
with open(file_path, 'w+') as file:
file.write(f"{best_point_list[-1]}\n")
for action in best_action_list:
file.write(f"{action}\n")
return best_past_list, best_point_list,best_shoot_list,best_direction_list,best_grab_list,best_map_list,best_action_list