-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSokobanPathfinding.py
103 lines (90 loc) · 2.96 KB
/
SokobanPathfinding.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
import sys
class SokobanPathfinding:
_MAP_INPUTS = {
"free_space": " ",
"wall": "#",
"goal": ".",
"player": "@",
"player_on_goal": "+",
"box": "$",
"box_on_goal": "*",
}
def __init__(self):
self._map = []
self._position = [0, 0]
self._answer = []
self._end = False
def read_map(self, file_name):
"""
Function that reads in a map from a .txt file
"""
with open(file_name) as f:
lines = f.readlines()
for line in lines:
line = line.replace("$", "#")
self._map.append(line[:-1])
def find_player(self):
for idx, i in enumerate(self._map):
if self._MAP_INPUTS["player"] in i:
self._position[0] = idx
self._position[1] = i.index(self._MAP_INPUTS["player"])
def print_map(self):
"""
Function that prints the inputted map.
"""
for row in self._map:
# print all values of each row in the map matrix
print(row)
def edit_map(self, position, value):
s = list(self._map[position[0]])
s[position[1]] = value
self._map[position[0]] = "".join(s)
def player_move(self, direction):
self.edit_map(self._position, self._MAP_INPUTS["free_space"])
next_position = self._position
if direction == "u":
next_position[0] -= 1
elif direction == "d":
next_position[0] += 1
elif direction == "l":
next_position[1] -= 1
elif direction == "r":
next_position[1] += 1
if self._map[next_position[0]][next_position[1]] == self._MAP_INPUTS["goal"]:
self._end = True
else:
self.edit_map(next_position, self._MAP_INPUTS["player"])
def next_map(self):
while self._end == False:
if (
self._map[self._position[0] - 1][self._position[1]]
!= self._MAP_INPUTS["wall"]
):
self.player_move("u")
self._answer.append("u")
elif (
self._map[self._position[0]][self._position[1] + 1]
!= self._MAP_INPUTS["wall"]
):
self.player_move("r")
self._answer.append("r")
elif (
self._map[self._position[0] + 1][self._position[1]]
!= self._MAP_INPUTS["wall"]
):
self.player_move("d")
self._answer.append("d")
elif (
self._map[self._position[0]][self._position[1] - 1]
!= self._MAP_INPUTS["wall"]
):
self.player_move("l")
self._answer.append("l")
self.print_map()
print(self._answer)
if __name__ == "__main__":
sb = SokobanPathfinding()
sb.read_map(sys.argv[1])
sb.print_map()
sb.find_player()
sb.next_map()