added pathfind

This commit is contained in:
Kraton99 2021-04-12 22:54:33 +02:00
parent d97daf6d55
commit eb87ec9f37
16 changed files with 139 additions and 20 deletions

View File

@ -4,7 +4,7 @@
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" /> <excludeFolder url="file://$MODULE_DIR$/venv" />
</content> </content>
<orderEntry type="jdk" jdkName="Python 3.9 (Projekt_AI-Automatyczny_saper)" jdkType="Python SDK" /> <orderEntry type="jdk" jdkName="Python 3.8 (2)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
</module> </module>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (Projekt_AI-Automatyczny_saper)" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (2)" project-jdk-type="Python SDK" />
</project> </project>

View File

@ -8,6 +8,7 @@ class Board:
def __init__(self, win): def __init__(self, win):
self.board = [] self.board = []
self.bombMap = {} self.bombMap = {}
self.stoneMap = {}
self.win = win self.win = win
def drawSquares(self, win): def drawSquares(self, win):
@ -33,6 +34,14 @@ class Board:
rect.center = (key.getX() * SQUARE_SIZE + SQUARE_SIZE / 2, key.getY() * SQUARE_SIZE + SQUARE_SIZE / 2) rect.center = (key.getX() * SQUARE_SIZE + SQUARE_SIZE / 2, key.getY() * SQUARE_SIZE + SQUARE_SIZE / 2)
self.win.blit(image, rect) self.win.blit(image, rect)
def drawStones(self):
for key in self.stoneMap:
image = pygame.image.load('Engine/stone.png')
image = pygame.transform.scale(image, (SQUARE_SIZE - 5, SQUARE_SIZE - 5))
rect = image.get_rect()
rect.center = (key.getX() * SQUARE_SIZE + SQUARE_SIZE / 2, key.getY() * SQUARE_SIZE + SQUARE_SIZE / 2)
self.win.blit(image, rect)
def getBomb(self, point): def getBomb(self, point):
if point in self.bombMap: if point in self.bombMap:
return self.bombMap[point] return self.bombMap[point]

View File

@ -8,6 +8,7 @@ from Engine.Agent import Agent
from Engine.BombFactory import BombFactory from Engine.BombFactory import BombFactory
from Engine.Point import Point from Engine.Point import Point
from Engine.Stone import Stone from Engine.Stone import Stone
from Engine.PathFinder import PathFinder
class Game: class Game:
@ -27,35 +28,55 @@ class Game:
self.agent.defuse(self.board.getBomb(self.agent.getPoint())) self.agent.defuse(self.board.getBomb(self.agent.getPoint()))
self.board.drawSquares(self.win) self.board.drawSquares(self.win)
self.board.drawBombs() self.board.drawBombs()
self.board.drawStones()
self.board.drawAgent(self.win, self.agent) self.board.drawAgent(self.win, self.agent)
pygame.display.update() pygame.display.update()
def move(self): def move(self):
point = self.agent.getPoint() # point = self.agent.getPoint()
if self.goingDown: # if self.goingDown:
if point.getY() + 1 < ROWS: # if point.getY() + 1 < ROWS:
point.y += 1 # point.y += 1
elif point.getX() + 1 < COLS: # elif point.getX() + 1 < COLS:
point.x += 1 # point.x += 1
self.goingDown = not self.goingDown # self.goingDown = not self.goingDown
else: # else:
if point.getY() - 1 >= 0: # if point.getY() - 1 >= 0:
point.y -= 1 # point.y -= 1
elif point.getX() + 1 < COLS: # elif point.getX() + 1 < COLS:
point.x += 1 # point.x += 1
self.goingDown = not self.goingDown # self.goingDown = not self.goingDown
# 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.agent.point = point
self.update()
def randomizeObject(self): def randomizeObject(self):
i = 0 i = 0
while i < 11: while i < 5:
point = Point(random.randint(0, 7), random.randint(0, 7)) point = Point(random.randint(0, 9), random.randint(0, 9))
if(point.getX() == 0 and point.getY() == 0): if(point.getX() == 0 and point.getY() == 0):
continue; continue;
if point not in self.board.bombMap: if point not in self.board.bombMap:
object = self.pickObject(random.randint(0, 4)) object = self.pickObject(random.randint(0, 4))
self.board.bombMap[point] = object self.board.bombMap[point] = object
i += 1 i += 1
r = 15
j = 0
while j < r:
point = Point(random.randint(0, 9), random.randint(0, 9))
if (point.getX() == 0 and point.getY() == 0):
continue;
if point not in self.board.bombMap and point not in self.board.stoneMap:
object = Stone()
self.board.stoneMap[point] = object
j += 1
def pickObject(self, rand): def pickObject(self, rand):
if rand == 0: if rand == 0:
@ -68,5 +89,11 @@ class Game:
return BombFactory.create(CLAYMORE) return BombFactory.create(CLAYMORE)
elif rand == 4: elif rand == 4:
return BombFactory.create(LAND_MINE) return BombFactory.create(LAND_MINE)
# elif(rand == 5):
# return Stone() 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

78
Engine/PathFinder.py Normal file
View File

@ -0,0 +1,78 @@
from Constants import ROWS, COLS
from Engine.Point import Point
class PathFinder:
def __init__(self, board):
self.board = board
self.openList = []
self.cameFrom = {}
self.gScore = {}
self.fScore = {}
self.current = Point(0,0)
def findPath(self,startPoint, endPoint):
self.openList.append(startPoint)
self.gScore[startPoint] = 0
self.fScore[startPoint] = startPoint.distance(endPoint)
while self.openList:
self.current = self.minKey(self.fScore, self.openList)
if self.current.__eq__(endPoint):
return self.reconstructPath()
self.openList.remove(self.current)
for point in self.getNeighbour(self.current):
tentativeGScore = self.gScore.get(self.current) + self.current.distance(point)
if tentativeGScore < self.gScore.get(point, 10000):
self.cameFrom[point] = self.current
self.gScore[point] = tentativeGScore
self.fScore[point] = point.distance(startPoint)
if(point not in self.openList):
self.openList.append(point)
return []
def reconstructPath(self):
totalPath = []
totalPath.append(self.current)
while self.current in self.cameFrom:
self.current = self.cameFrom[self.current]
totalPath.insert(0,self.current)
return totalPath
def getNeighbour(self, current):
neighbourlist = []
point1 = Point(current.getX() + 1, current.getY())
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:
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:
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:
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:
neighbourlist.append(point4)
return neighbourlist
def minKey(self, fscore, openlist):
minkey = Point(0,0)
minValue = float('inf')
for point in openlist:
value = fscore.get(point, 10000)
if value < minValue:
minValue = value
minkey = point
return minkey

View File

@ -9,6 +9,9 @@ class Point:
def getX(self): def getX(self):
return self.x return self.x
def distance(self,endpoint):
return abs(self.getX() + endpoint.getX()) + abs(self.getY() + endpoint.getY())
def __hash__(self): def __hash__(self):
"""Overrides the default implementation""" """Overrides the default implementation"""
return hash(tuple(sorted(self.__dict__.items()))) return hash(tuple(sorted(self.__dict__.items())))

Binary file not shown.

Binary file not shown.

View File

@ -20,6 +20,8 @@ def main():
run = False run = False
pygame.time.delay(200) pygame.time.delay(200)
if(game.finalState()):
run = False
game.move() game.move()
game.update() game.update()