diff --git a/Engine/Agent.py b/Engine/Agent.py index 89636749..73e7cda9 100644 --- a/Engine/Agent.py +++ b/Engine/Agent.py @@ -24,11 +24,11 @@ class Agent(object): if self.point.getX() < orientation.getX(): return 0 elif self.point.getY() > orientation.getY(): - return 270 + return 90 elif self.point.getX() > orientation.getX(): return 180 elif self.point.getY() < orientation.getY(): - return 90 + return 270 return 0 # def ifNotOnEdge(self, destination): diff --git a/Engine/BfsPathFinder.py b/Engine/BfsPathFinder.py new file mode 100644 index 00000000..2b603506 --- /dev/null +++ b/Engine/BfsPathFinder.py @@ -0,0 +1,43 @@ +from queue import Queue +from Engine.PathFinder import PathFinder +class BfsPathFinder(PathFinder): + + def __init__(self, board): + super().__init__(board) + self.board = board + self.goal = None + def findBomb(self,start): + + frontier = Queue() + frontier.put(start) + cameFrom = dict() + cameFrom[start] = None + + while not frontier.empty(): + current = frontier.get() + + if self.checkGoal(current): + return self.constructPath(cameFrom,start) + + for next in self.getNeighbour(current): + if next not in cameFrom: + frontier.put(next) + cameFrom[next] = current + return [] + + def constructPath(self,cameFrom,start): + current = cameFrom[self.goal] + path = [] + path.append(self.goal) + while current != start: + path.append(current) + current = cameFrom[current] + path.append(start) + path.reverse() + return path + + def checkGoal(self, current): + if current in self.board.bombMap: + self.goal = current + return True + return False diff --git a/Engine/Game.py b/Engine/Game.py index 9991a4ae..bc893f45 100644 --- a/Engine/Game.py +++ b/Engine/Game.py @@ -9,6 +9,7 @@ from Engine.BombFactory import BombFactory from Engine.Point import Point from Engine.Stone import Stone from Engine.PathFinder import PathFinder +from Engine.BfsPathFinder import BfsPathFinder class Game: @@ -23,43 +24,45 @@ class Game: self.agent = Agent(Point(0, 0)) self.turn = GREEN self.goingDown = True + self.path = [] def update(self): - self.agent.defuse(self.board.getBomb(self.agent.getPoint())) + self.defuseBomb() self.board.drawSquares(self.win) self.board.drawBombs() self.board.drawStones() self.board.drawAgent(self.win, self.agent) pygame.display.update() - def move(self): - point = self.agent.getPoint() - if self.goingDown: - if point.getY() + 1 < ROWS: - point.y += 1 - elif point.getX() + 1 < COLS: - point.x += 1 - self.goingDown = not self.goingDown - else: - if point.getY() - 1 >= 0: - point.y -= 1 - elif point.getX() + 1 < COLS: - point.x += 1 - self.goingDown = not self.goingDown - self.agent.rotateImage(point) - self.agent.point = point - #self.moveSequence() - - def moveSequence(self): - pathfinder = PathFinder(self.board) - for point in pathfinder.findPath(Point(0,0), Point(5,5)): - self.agent.point = point - self.update() + # def move(self): + # point = self.agent.getPoint() + # tmpPoint = Point(point.getX(), point.getY()) + # if self.goingDown: + # if point.getY() + 1 < ROWS: + # tmpPoint.y += 1 + # elif point.getX() + 1 < COLS: + # tmpPoint.x += 1 + # self.goingDown = not self.goingDown + # else: + # if point.getY() - 1 >= 0: + # tmpPoint.y -= 1 + # elif point.getX() + 1 < COLS: + # tmpPoint.x += 1 + # self.goingDown = not self.goingDown + # self.agent.rotateImage(tmpPoint) + # self.agent.point = tmpPoint + # #self.moveSequence() + # + # def moveSequence(self): + # pathfinder = PathFinder(self.board) + # for point in pathfinder.findPath(Point(0,0), Point(5,5)): + # self.agent.point = point + # self.update() def randomizeObject(self): i = 0 - while i < 5: + while i < 10: point = Point(random.randint(0, 9), random.randint(0, 9)) if(point.getX() == 0 and point.getY() == 0): continue; @@ -92,12 +95,26 @@ class Game: return BombFactory.create(LAND_MINE) def findBomb(self): - pass + return BfsPathFinder(self.board).findBomb(self.agent.getPoint()) def finalState(self): - j = 0 - for key in self.board.bombMap: - bomb = self.board.bombMap[key] - if not bomb.isDefused: - j += 1 - return j == 0 + if len(self.board.bombMap) == 0: + return True + return False + + def moveToNext(self): + point = self.path.pop(0) + self.agent.rotateImage(point) + self.agent.point = point + + def savePath(self,path): + self.path = path + + def getPath(self): + return self.path + + def defuseBomb(self): + point = self.agent.getPoint() + self.agent.defuse(self.board.getBomb(point)) + if point in self.board.bombMap: + self.board.bombMap.pop(point) diff --git a/Engine/PathFinder.py b/Engine/PathFinder.py index fc67ef5c..06d65bc8 100644 --- a/Engine/PathFinder.py +++ b/Engine/PathFinder.py @@ -50,17 +50,20 @@ class PathFinder: point2 = Point(current.getX(), current.getY() + 1) point3 = Point(current.getX(), current.getY() - 1) point4 = Point(current.getX() - 1, current.getY()) - if not (point1.getX() < 0 or point1.getX() > COLS or point1.getY() < 0 or point1.getY() > ROWS or point1.__eq__(current)) and point1 not in self.board.stoneMap: + if self.checkField(current,point1): neighbourlist.append(point1) - if not (point2.getX() < 0 or point2.getX() > COLS or point2.getY() < 0 or point2.getY() > ROWS or point2.__eq__(current)) and point2 not in self.board.stoneMap: + if self.checkField(current, point2): neighbourlist.append(point2) - if not (point3.getX() < 0 or point3.getX() > COLS or point3.getY() < 0 or point3.getY() > ROWS or point3.__eq__(current)) and point3 not in self.board.stoneMap: + if self.checkField(current,point3): neighbourlist.append(point3) - if not (point4.getX() < 0 or point4.getX() > COLS or point4.getY() < 0 or point4.getY() > ROWS or point4.__eq__(current)) and point4 not in self.board.stoneMap: + if self.checkField(current,point4): neighbourlist.append(point4) - return neighbourlist + def checkField(self,current, point): + if not (point.getX() < 0 or point.getX() > COLS - 1 or point.getY() < 0 or point.getY() > ROWS - 1 or point.__eq__(current)) and point not in self.board.stoneMap: + return True + return False def minKey(self, fscore, openlist): minkey = Point(0,0) diff --git a/Engine/__pycache__/Agent.cpython-39.pyc b/Engine/__pycache__/Agent.cpython-39.pyc index d23c95bf..b14e0bf5 100644 Binary files a/Engine/__pycache__/Agent.cpython-39.pyc and b/Engine/__pycache__/Agent.cpython-39.pyc differ diff --git a/Engine/__pycache__/BfsPathFinder.cpython-39.pyc b/Engine/__pycache__/BfsPathFinder.cpython-39.pyc new file mode 100644 index 00000000..f1ca42b6 Binary files /dev/null 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 80d40c23..3743bb33 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 acdbb3ce..876a7765 100644 Binary files a/Engine/__pycache__/PathFinder.cpython-39.pyc and b/Engine/__pycache__/PathFinder.cpython-39.pyc differ diff --git a/main.py b/main.py index bec90f75..ea83ec1f 100644 --- a/main.py +++ b/main.py @@ -20,13 +20,12 @@ def main(): run = False pygame.time.delay(200) - # while True: - # if game.finalState(): - # run = False - # break - # list = game.findBomb() - # for point in list: - game.move() + if game.finalState(): + break + if len(game.getPath()) == 0: + path = game.findBomb() + game.savePath(path) + game.moveToNext() game.update()