-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutilsMinihackSearch.py
More file actions
70 lines (60 loc) · 2.07 KB
/
utilsMinihackSearch.py
File metadata and controls
70 lines (60 loc) · 2.07 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
import numpy as np
import math
from typing import Tuple, List
def get_player_location(game_map: np.ndarray, symbol : str = "@") -> Tuple[int, int]:
x, y = np.where(game_map == ord(symbol))
return (x[0], y[0])
def get_target_location(game_map: np.ndarray, symbol : str = ">") -> Tuple[int, int]:
x, y = np.where(game_map == ord(symbol))
return (x[0], y[0])
def is_wall(position_element: int) -> bool:
obstacles = "|- "
return chr(position_element) in obstacles
def get_valid_moves(game_map: np.ndarray, current_position: Tuple[int, int]) -> List[Tuple[int, int]]:
x_limit, y_limit = game_map.shape
valid = []
x, y = current_position
# North
if y - 1 > 0 and not is_wall(game_map[x, y-1]):
valid.append((x, y-1))
# East
if x + 1 < x_limit and not is_wall(game_map[x+1, y]):
valid.append((x+1, y))
# South
if y + 1 < y_limit and not is_wall(game_map[x, y+1]):
valid.append((x, y+1))
# West
if x - 1 > 0 and not is_wall(game_map[x-1, y]):
valid.append((x-1, y))
return valid
def actions_from_path(start: Tuple[int, int], path: List[Tuple[int, int]]) -> List[int]:
action_map = {
"N": 0,
"E": 1,
"S": 2,
"W": 3
}
actions = []
x_s, y_s = start
for (x, y) in path:
if x_s == x:
if y_s > y:
actions.append(action_map["W"])
else: actions.append(action_map["E"])
elif y_s == y:
if x_s > x:
actions.append(action_map["N"])
else: actions.append(action_map["S"])
else:
raise Exception("x and y can't change at the same time. oblique moves not allowed!")
x_s = x
y_s = y
return actions
def euclidean_distance(point1: Tuple[int, int], point2: Tuple[int, int]) -> float:
x1, y1 = point1
x2, y2 = point2
return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
def manhattan_distance(point1: Tuple[int, int], point2: Tuple[int, int]) -> int:
x1, y1 = point1
x2, y2 = point2
return abs(x1 - x2) + abs(y1 - y2)