-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfs.py
90 lines (72 loc) · 2.74 KB
/
bfs.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
# Complete this class for all parts of the project
from pacman_module.game import Agent
from pacman_module.pacman import Directions
from pacman_module.util import PriorityQueue
class PacmanAgent(Agent):
def __init__(self, args):
"""
Arguments:
----------
- `args`: Namespace of arguments from command-line prompt.
"""
self.args = args
self.path = None
def get_action(self, state):
"""
Given a pacman game state, returns a legal move.
Arguments:
----------
- `state`: the current game state. See FAQ and class
`pacman.GameState`.
Return:
-------
- A legal move as defined in `game.Directions`.
"""
# return Directions.STOP
legals = state.getLegalActions()
legals.remove(Directions.STOP)
if self.path == None:
self.path = self.bfs(state)
moveTo = self.path[0]
self.path.pop(0)
return moveTo
def get_legals(self, s, currentPos):
movements = []
a, b = currentPos
walls = s.getWalls()
# print(walls[a][b])
if not walls[a + 1][b]: # for East move
movements.append((a + 1, b))
if not walls[a - 1][b]: # for West move
movements.append((a - 1, b))
if not walls[a][b + 1]: # for North move
movements.append((a, b + 1))
if not walls[a][b - 1]: # for South move
movements.append((a, b - 1))
return movements
def bfs(self, currentPos):
visited = []
fringe = PriorityQueue()
fringe.push((currentPos, []), 0)
while not fringe.isEmpty():
node = fringe.pop()
print("node -> ", node)
depth, successorPos, path = node[0], node[1][0], node[1][1]
# print()
# print(path)
# print(actualPos.generatePacmanSuccessors())
successorPacmanState = (successorPos.getPacmanPosition(), successorPos.getFood())
if successorPacmanState not in visited:
visited.append(successorPacmanState)
for successor, direction in successorPos.generatePacmanSuccessors():
# print(direction)
goal = True
for directions in successor.getFood():
for isGoalState in directions:
if isGoalState:
goal = False
if goal:
return path + [direction]
else:
depth = len(path)
fringe.push((successor, path + [direction]), depth) # depth -1 for dfs| depth for bfs | depth + 1 for ucs