From c1cdc1489d99633cff491a24f8981aea65002119 Mon Sep 17 00:00:00 2001 From: Kraton99 Date: Tue, 13 Apr 2021 21:27:51 +0200 Subject: [PATCH] bfs --- Engine/Agent.py | 4 +- Engine/BfsPathFinder.py | 43 ++++++++++ Engine/Game.py | 81 +++++++++++------- Engine/PathFinder.py | 13 +-- Engine/__pycache__/Agent.cpython-39.pyc | Bin 1418 -> 1418 bytes .../__pycache__/BfsPathFinder.cpython-39.pyc | Bin 0 -> 1426 bytes Engine/__pycache__/Game.cpython-39.pyc | Bin 3361 -> 3407 bytes Engine/__pycache__/PathFinder.cpython-39.pyc | Bin 2367 -> 2351 bytes main.py | 13 ++- 9 files changed, 108 insertions(+), 46 deletions(-) create mode 100644 Engine/BfsPathFinder.py create mode 100644 Engine/__pycache__/BfsPathFinder.cpython-39.pyc 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 d23c95bfed390e0b3369a75a41c138b3183384b3..b14e0bf5cae5301dda5b7ff093a0b6b2c3114639 100644 GIT binary patch delta 38 pcmeC;?&9W3_ delta 38 rcmeC;?&9W3qp2!HG diff --git a/Engine/__pycache__/BfsPathFinder.cpython-39.pyc b/Engine/__pycache__/BfsPathFinder.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f1ca42b6e2be1b87906a2a92db5e471e91216393 GIT binary patch literal 1426 zcmZWpOK;mo5Z)IlT9E>@DUvoRaxDn7IR!lwM$tU#pg=3QaC#wBurzli%jQeFOQSOA zQw-#9SV!OcJNg6m+EY)x_tcrCg-9-m9q!J~?#z7i4YTohNZ|S7pZ2#u1tEXoV*3Fd z`3{Ep5`-j@7Nn#Jr8s8=D|x~xc|#i7^ThhOlP*895Ct}dePdt%NstSjAT`j0J(IU$TyE(zguOka)*Cx=%pQ*TgjN6>D-SI&vL!VomjHn1yJwW=8WR_LA-SB}q> zb$j`V%Fo^ax^A4;m9e_bE!NYGV0)q#^ONJc)p4kivQQtJX`4hdT~~I2mCCbH9q77r zgS^#RRp6vjzgs;9^IqJ?gV78q__8jK_h4lngCH!VL%=g)5sV=nvHLUv&FFs!Z;s|0 zzT#=j4c8vwA7O%oIwhY0s&hn80MfAbxbTjhas9Q|aj&5rN6Y17uVX!W*KAFIm|}w* z*BGn|v!+p%bOWuPJQn&9Z0d(7K1DOp0BU#A)}O!*GT(d*f+pGhNV&M|a}ekR^_=M=Pj3^yn~6%UZSt&PQpwY_p=@@$I8AS`UOqA8vfx zwYe&aG~Ej09@-&UGo726y3c$NvqSzb|E{7V#UIa1D8w;sR+h6nE=T^VKSp+29Cm{cUb+cI_H3Q^J8JrKH;S^!@I$0gSgK5AHNh_s>+WJ zV%euRDVG=Yd6if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