adding states

This commit is contained in:
barmal4 2021-04-26 20:24:52 +02:00
parent 641b7091f7
commit 560e945d5a
10 changed files with 53 additions and 13 deletions

View File

@ -10,9 +10,13 @@ UP = 'UP'
DOWN = 'DOWN' DOWN = 'DOWN'
LEFT = 'LEFT' LEFT = 'LEFT'
RIGHT = 'RIGHT' RIGHT = 'RIGHT'
DEFUSE = 'DEFUSE'
REST = 'REST'
DECOY = 'Decoy' DECOY = 'Decoy'
ATOMIC_BOMB = 'Atomic Bomb' ATOMIC_BOMB = 'Atomic Bomb'
CLAYMORE = 'Claymore' CLAYMORE = 'Claymore'
CHEMICAL_BOMB = 'Chemical Bomb' CHEMICAL_BOMB = 'Chemical Bomb'
LAND_MINE = 'Land Mine' LAND_MINE = 'Land Mine'

View File

@ -1,23 +1,24 @@
from queue import Queue from queue import Queue
from Engine.PathFinder import PathFinder from Engine.Point import Point
class BfsPathFinder(PathFinder): from Engine.State import State
from Constants import RIGHT, LEFT, DOWN, UP, DEFUSE, REST
class BfsPathFinder:
def __init__(self, board): def __init__(self, board):
super().__init__(board)
self.board = board self.board = board
self.goal = None self.goal = None
def findBomb(self,start): def findBomb(self,startState):
frontier = Queue() frontier = Queue()
frontier.put(start) frontier.put(startState)
cameFrom = dict() cameFrom = dict()
cameFrom[start] = None cameFrom[startState] = None
while not frontier.empty(): while not frontier.empty():
current = frontier.get() current = frontier.get()
if self.checkGoal(current): if self.checkGoal(current):
return self.constructPath(cameFrom,start) return self.constructPath(cameFrom,startState)
for next in self.getNeighbour(current): for next in self.getNeighbour(current):
if next not in cameFrom: if next not in cameFrom:
@ -25,19 +26,39 @@ class BfsPathFinder(PathFinder):
cameFrom[next] = current cameFrom[next] = current
return [] return []
def constructPath(self,cameFrom,start): def constructPath(self,cameFrom,startState):
current = cameFrom[self.goal] current = cameFrom[self.goal]
path = [] path = []
path.append(self.goal) path.append(self.goal)
while current != start: while current.getAction != startState:
path.append(current) path.append(current)
current = cameFrom[current] current = cameFrom[current]
path.append(start) path.append(startState)
path.reverse() path.reverse()
return path 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): def checkGoal(self, current):
if current in self.board.bombMap: if current.getPoint() in self.board.bombMap:
self.goal = current self.goal = current
return True return True
return False return False

View File

@ -10,6 +10,7 @@ from Engine.Point import Point
from Engine.Stone import Stone from Engine.Stone import Stone
from Engine.PathFinder import PathFinder from Engine.PathFinder import PathFinder
from Engine.BfsPathFinder import BfsPathFinder from Engine.BfsPathFinder import BfsPathFinder
from Engine.State import State
class Game: class Game:
@ -95,7 +96,7 @@ class Game:
return BombFactory.create(LAND_MINE) return BombFactory.create(LAND_MINE)
def findBomb(self): def findBomb(self):
return BfsPathFinder(self.board).findBomb(self.agent.getPoint()) return BfsPathFinder(self.board).findBomb(State('REST'))
def finalState(self): def finalState(self):
if len(self.board.bombMap) == 0: if len(self.board.bombMap) == 0:

View File

@ -50,7 +50,7 @@ class PathFinder:
point2 = Point(current.getX(), current.getY() + 1) point2 = Point(current.getX(), current.getY() + 1)
point3 = Point(current.getX(), current.getY() - 1) point3 = Point(current.getX(), current.getY() - 1)
point4 = Point(current.getX() - 1, current.getY()) point4 = Point(current.getX() - 1, current.getY())
if self.checkField(current,point1): if self.checkField(current,current):
neighbourlist.append(point1) neighbourlist.append(point1)
if self.checkField(current, point2): if self.checkField(current, point2):
neighbourlist.append(point2) neighbourlist.append(point2)

14
Engine/State.py Normal file
View File

@ -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

Binary file not shown.

Binary file not shown.