adding states
This commit is contained in:
parent
641b7091f7
commit
560e945d5a
@ -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'
|
||||
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -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:
|
||||
|
@ -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)
|
||||
|
14
Engine/State.py
Normal file
14
Engine/State.py
Normal 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.
Binary file not shown.
BIN
Engine/__pycache__/State.cpython-39.pyc
Normal file
BIN
Engine/__pycache__/State.cpython-39.pyc
Normal file
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user