diff --git a/Constants.py b/Constants.py index 6eb9e177..e79a06d5 100644 --- a/Constants.py +++ b/Constants.py @@ -10,9 +10,13 @@ UP = 'UP' DOWN = 'DOWN' LEFT = 'LEFT' RIGHT = 'RIGHT' +DEFUSE = 'DEFUSE' +REST = 'REST' DECOY = 'Decoy' ATOMIC_BOMB = 'Atomic Bomb' CLAYMORE = 'Claymore' CHEMICAL_BOMB = 'Chemical Bomb' LAND_MINE = 'Land Mine' + + diff --git a/Engine/BfsPathFinder.py b/Engine/BfsPathFinder.py index 2b603506..9741def2 100644 --- a/Engine/BfsPathFinder.py +++ b/Engine/BfsPathFinder.py @@ -1,23 +1,24 @@ from queue import Queue -from Engine.PathFinder import PathFinder -class BfsPathFinder(PathFinder): +from Engine.Point import Point +from Engine.State import State +from Constants import RIGHT, LEFT, DOWN, UP, DEFUSE, REST +class BfsPathFinder: def __init__(self, board): - super().__init__(board) self.board = board self.goal = None - def findBomb(self,start): + def findBomb(self,startState): frontier = Queue() - frontier.put(start) + frontier.put(startState) cameFrom = dict() - cameFrom[start] = None + cameFrom[startState] = None while not frontier.empty(): current = frontier.get() if self.checkGoal(current): - return self.constructPath(cameFrom,start) + return self.constructPath(cameFrom,startState) for next in self.getNeighbour(current): if next not in cameFrom: @@ -25,19 +26,39 @@ class BfsPathFinder(PathFinder): cameFrom[next] = current return [] - def constructPath(self,cameFrom,start): + def constructPath(self,cameFrom,startState): current = cameFrom[self.goal] path = [] path.append(self.goal) - while current != start: + while current.getAction != startState: path.append(current) current = cameFrom[current] - path.append(start) + path.append(startState) path.reverse() return path + def getNeighbour(self, current): + neighbourlist = [] + state1 = State(LEFT,Point(current.getX() + 1, current.getY())) + state2 = State(RIGHT,Point(current.getX(), current.getY() + 1)) + state3 = State(UP,Point(current.getX(), current.getY() - 1)) + state4 = State(DOWN,Point(current.getX() - 1, current.getY())) + if self.checkField(current.getPoint(),state1.getPoint()): + neighbourlist.append(state1.getPoint()) + if self.checkField(current.getPoint(), state2.getPoint()): + neighbourlist.append(state2.getPoint()) + if self.checkField(current.getPoint(),state3.getPoint()): + neighbourlist.append(state3) + if self.checkField(current.getPoint(),state4.getPoint()): + neighbourlist.append(state4) + return neighbourlist + def checkGoal(self, current): - if current in self.board.bombMap: + if current.getPoint() in self.board.bombMap: self.goal = current return True return False + + + + diff --git a/Engine/Game.py b/Engine/Game.py index bc893f45..ffa63432 100644 --- a/Engine/Game.py +++ b/Engine/Game.py @@ -10,6 +10,7 @@ from Engine.Point import Point from Engine.Stone import Stone from Engine.PathFinder import PathFinder from Engine.BfsPathFinder import BfsPathFinder +from Engine.State import State class Game: @@ -95,7 +96,7 @@ class Game: return BombFactory.create(LAND_MINE) def findBomb(self): - return BfsPathFinder(self.board).findBomb(self.agent.getPoint()) + return BfsPathFinder(self.board).findBomb(State('REST')) def finalState(self): if len(self.board.bombMap) == 0: diff --git a/Engine/PathFinder.py b/Engine/PathFinder.py index 06d65bc8..bc0b618e 100644 --- a/Engine/PathFinder.py +++ b/Engine/PathFinder.py @@ -50,7 +50,7 @@ class PathFinder: point2 = Point(current.getX(), current.getY() + 1) point3 = Point(current.getX(), current.getY() - 1) point4 = Point(current.getX() - 1, current.getY()) - if self.checkField(current,point1): + if self.checkField(current,current): neighbourlist.append(point1) if self.checkField(current, point2): neighbourlist.append(point2) diff --git a/Engine/State.py b/Engine/State.py new file mode 100644 index 00000000..a6998aac --- /dev/null +++ b/Engine/State.py @@ -0,0 +1,14 @@ +from Constants import RIGHT, LEFT, DEFUSE, REST +class State: + + def __init__(self, action, point): + self.action = action + self.point = point + + def getPoint(self): + return self.point + + def getAction(self): + return self.action + + diff --git a/Engine/__pycache__/BfsPathFinder.cpython-39.pyc b/Engine/__pycache__/BfsPathFinder.cpython-39.pyc index 5c5a53e3..d3abaed5 100644 Binary files a/Engine/__pycache__/BfsPathFinder.cpython-39.pyc and b/Engine/__pycache__/BfsPathFinder.cpython-39.pyc differ diff --git a/Engine/__pycache__/Game.cpython-39.pyc b/Engine/__pycache__/Game.cpython-39.pyc index 0ffd955a..1ba1685a 100644 Binary files a/Engine/__pycache__/Game.cpython-39.pyc and b/Engine/__pycache__/Game.cpython-39.pyc differ diff --git a/Engine/__pycache__/PathFinder.cpython-39.pyc b/Engine/__pycache__/PathFinder.cpython-39.pyc index 565e51ee..a2cc8d9d 100644 Binary files a/Engine/__pycache__/PathFinder.cpython-39.pyc and b/Engine/__pycache__/PathFinder.cpython-39.pyc differ diff --git a/Engine/__pycache__/State.cpython-39.pyc b/Engine/__pycache__/State.cpython-39.pyc new file mode 100644 index 00000000..fabd08bb Binary files /dev/null and b/Engine/__pycache__/State.cpython-39.pyc differ diff --git a/__pycache__/Constants.cpython-39.pyc b/__pycache__/Constants.cpython-39.pyc index 01b5e49b..60f21a22 100644 Binary files a/__pycache__/Constants.cpython-39.pyc and b/__pycache__/Constants.cpython-39.pyc differ