forked from s444399/AI
Upload files to ''
This commit is contained in:
parent
f17e5a650e
commit
4e18cd852b
72
AStar.py
Normal file
72
AStar.py
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
import numpy
|
||||||
|
from AStarState import AStarState
|
||||||
|
|
||||||
|
class AStar:
|
||||||
|
def returnPath(self, currentNode, grid):
|
||||||
|
path = []
|
||||||
|
noRows, noColumns = numpy.shape(grid)
|
||||||
|
result = [[-1 for i in range(noColumns)] for j in range(noRows)]
|
||||||
|
current = currentNode
|
||||||
|
while current is not None:
|
||||||
|
path.append(current.position)
|
||||||
|
current = current.parent
|
||||||
|
path = path[::-1]
|
||||||
|
startValue = 0
|
||||||
|
for i in range(len(path)):
|
||||||
|
result[path[i][0]][path[i][1]] = startValue
|
||||||
|
startValue+=1
|
||||||
|
return result
|
||||||
|
|
||||||
|
def search(self, start, end, grid, cost):
|
||||||
|
noRows, noColumns = numpy.shape(grid)
|
||||||
|
startNode = AStarState(None, tuple(start))
|
||||||
|
endNode = AStarState(None, tuple(end))
|
||||||
|
startNode.g = 0
|
||||||
|
startNode.h = 0
|
||||||
|
startNode.f = 0
|
||||||
|
endNode.g = 0
|
||||||
|
endNode.h = 0
|
||||||
|
endNode.f = 0
|
||||||
|
toVisit = []
|
||||||
|
visited = []
|
||||||
|
toVisit.append(startNode)
|
||||||
|
iterations = 0
|
||||||
|
max = (len(grid) // 2) ** 10
|
||||||
|
moves = [[-1, 0], [1, 0], [0, 1], [0, -1]]
|
||||||
|
while len(toVisit)>0:
|
||||||
|
iterations=iterations+1
|
||||||
|
current = toVisit[0]
|
||||||
|
currentIndeks = 0
|
||||||
|
for indeks, item in enumerate(toVisit):
|
||||||
|
if item.g<current.g:
|
||||||
|
current = item
|
||||||
|
currentIndeks = indeks
|
||||||
|
if iterations>max:
|
||||||
|
return self.returnPath(current, grid)
|
||||||
|
visited.append(current)
|
||||||
|
toVisit.pop(currentIndeks)
|
||||||
|
if current==endNode:
|
||||||
|
return self.returnPath(current, grid)
|
||||||
|
children = []
|
||||||
|
for new in moves:
|
||||||
|
positions = (current.position[0]+new[0], current.position[1]+new[1])
|
||||||
|
if (positions[0] > (noRows - 1) or
|
||||||
|
positions[0] < 0 or
|
||||||
|
positions[1] > (noColumns - 1) or
|
||||||
|
positions[1] < 0):
|
||||||
|
continue
|
||||||
|
if grid[positions[0]][positions[1]]!=0:
|
||||||
|
continue
|
||||||
|
children.append(AStarState(current, positions))
|
||||||
|
for child in children:
|
||||||
|
if len([visitedChild for visitedChild in visited if visitedChild==child])>0:
|
||||||
|
continue
|
||||||
|
if child.position[0]<=(len(grid)-4) and child.position[0]>=3 and child.position[1]>=4 and child.position[1]<=(len(grid[0])-1):
|
||||||
|
child.g = current.g + (10 * cost)
|
||||||
|
else:
|
||||||
|
child.g = current.g + cost
|
||||||
|
child.h = (((child.position[0]-endNode.position[0]) ** 2) + ((child.position[1]-endNode.position[1]) ** 2))
|
||||||
|
child.f = child.g + child.h
|
||||||
|
if len([i for i in toVisit if child==i and child.g>i.g])>0:
|
||||||
|
continue
|
||||||
|
toVisit.append(child)
|
12
AStarState.py
Normal file
12
AStarState.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
|
||||||
|
class AStarState:
|
||||||
|
def __init__(self, parent, position):
|
||||||
|
self.parent = parent
|
||||||
|
self.position = position
|
||||||
|
self.g = 0
|
||||||
|
self.h = 0
|
||||||
|
self.f = 0
|
||||||
|
def __eq__(self, other):
|
||||||
|
return self.position == other.position
|
||||||
|
def show(self):
|
||||||
|
return str("{("+str(self.position[0])+", "+str(self.position[1])+") "+str(self.g)+"}")
|
4
Evencik.py
Normal file
4
Evencik.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
class Evencik:
|
||||||
|
def __init__(self, key):
|
||||||
|
self.key = key
|
5
box.py
Normal file
5
box.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import pygame
|
||||||
|
|
||||||
|
class Box:
|
||||||
|
def __init__(self):
|
||||||
|
self.nazwa = "Nazwa"
|
16
boxOnTheFloor.py
Normal file
16
boxOnTheFloor.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import pygame
|
||||||
|
|
||||||
|
class BoxOnTheFloor:
|
||||||
|
def __init__(self, screen, cell, we, ns, box):
|
||||||
|
self.cell = cell
|
||||||
|
self.ns = ns
|
||||||
|
self.occupied = True
|
||||||
|
self.box = box
|
||||||
|
self.we = we
|
||||||
|
self.screen = screen
|
||||||
|
self.image = pygame.image.load(r'images/pa.png')
|
||||||
|
self.image = pygame.transform.scale(self.image, (cell, cell))
|
||||||
|
def get(self):
|
||||||
|
return self.box
|
||||||
|
def draw(self):
|
||||||
|
self.screen.blit(self.image, (self.cell*self.ns, self.cell*self.we))
|
Loading…
Reference in New Issue
Block a user