a star
This commit is contained in:
parent
8e7384cf7a
commit
1d717acefa
BIN
container/__pycache__/a_star.cpython-37.pyc
Normal file
BIN
container/__pycache__/a_star.cpython-37.pyc
Normal file
Binary file not shown.
BIN
container/__pycache__/agent.cpython-37.pyc
Normal file
BIN
container/__pycache__/agent.cpython-37.pyc
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
container/__pycache__/mushroom.cpython-37.pyc
Normal file
BIN
container/__pycache__/mushroom.cpython-37.pyc
Normal file
Binary file not shown.
BIN
container/__pycache__/node.cpython-37.pyc
Normal file
BIN
container/__pycache__/node.cpython-37.pyc
Normal file
Binary file not shown.
Binary file not shown.
BIN
container/__pycache__/tree.cpython-37.pyc
Normal file
BIN
container/__pycache__/tree.cpython-37.pyc
Normal file
Binary file not shown.
87
container/a_star.py
Normal file
87
container/a_star.py
Normal file
@ -0,0 +1,87 @@
|
||||
from container import board
|
||||
from container.node import Node
|
||||
from container.mushroom import Mushroom
|
||||
from container.tree import Tree
|
||||
from operator import attrgetter
|
||||
|
||||
|
||||
def a_star(board, fringe, explored, istate, successor, get_cost, goaltest):
|
||||
|
||||
agent_x = istate[0]
|
||||
agent_y = istate[1]
|
||||
agent_angle = istate[2]
|
||||
fringe: List[Node] = [Node(agent_x, agent_y, agent_angle)]
|
||||
|
||||
while fringe:
|
||||
|
||||
if not fringe:
|
||||
return False, False
|
||||
|
||||
node = min(fringe, key=lambda x: x.cost)
|
||||
fringe.remove(node)
|
||||
|
||||
if (node.x, node.y) == goaltest:
|
||||
actions = []
|
||||
while node.parent != None:
|
||||
actions.append(node.action)
|
||||
node = node.parent
|
||||
actions.reverse()
|
||||
return actions
|
||||
|
||||
explored.append(node)
|
||||
|
||||
for action, x, y, angle in successor(board, node.x, node.y, node.angle):
|
||||
next_node = Node(x, y, angle)
|
||||
next_node.parent = node
|
||||
next_node.action = action
|
||||
next_node.cost = get_cost(next_node, goaltest,board) + heuristic_function(node, goaltest)
|
||||
|
||||
if next_node not in fringe and next_node not in explored:
|
||||
fringe.append(next_node)
|
||||
else:
|
||||
for checked_node in fringe:
|
||||
if(checked_node == next_node and (next_node.cost) < (checked_node.cost)):
|
||||
fringe[fringe.index(checked_node)] = next_node
|
||||
|
||||
|
||||
|
||||
def successor(board,x,y,angle):
|
||||
|
||||
result = []
|
||||
|
||||
result.append(("rotate_left", x, y, (angle + 1) % 4))
|
||||
result.append(("rotate_right", x, y, (angle - 1) % 4))
|
||||
|
||||
if angle == 1:
|
||||
if x > 0:
|
||||
result.append(("move", x-1, y, angle))
|
||||
elif angle == 0:
|
||||
if y > 0:
|
||||
result.append(("move", x, y-1, angle))
|
||||
elif angle == 3:
|
||||
if x < board.col - 1:
|
||||
result.append(("move", x+1, y, angle))
|
||||
elif angle == 2:
|
||||
if y < board.row - 1:
|
||||
result.append(("move", x, y+1, angle))
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def get_cost(node, goal: Mushroom, board):
|
||||
|
||||
if (node.parent.x, node.parent.y) == (node.x, node.y):
|
||||
sum = 1
|
||||
elif(isinstance((board.board[node.x][node.y]),Tree)):
|
||||
sum = 1000
|
||||
elif(isinstance((board.board[node.x][node.y]),Mushroom) and (board.board[node.x][node.y].poison) == True):
|
||||
sum = 1000
|
||||
else:
|
||||
sum = 2
|
||||
|
||||
return sum + node.parent.cost
|
||||
|
||||
|
||||
def heuristic_function(node, goal):
|
||||
return abs(node.x - goal[0]) + abs(node.y - goal[1])
|
||||
|
@ -5,6 +5,7 @@ from container.constans import *
|
||||
from container.tree import Tree
|
||||
from container.mushroom import Mushroom
|
||||
from container.agent import Agent
|
||||
from container.a_star import a_star,successor, get_cost
|
||||
|
||||
class Board:
|
||||
def __init__(self, x = 0, y = 0, width = WIDTH, height = HEIGHT, row = ROWS, col = COLUMNS):
|
||||
@ -21,7 +22,7 @@ class Board:
|
||||
|
||||
self.agent = Agent(self.free_spaces.pop(), self.square_size)
|
||||
|
||||
print(f"Board {col}x{row} with agent on ({self.agent.col},{self.agent.row})")
|
||||
print(f"Board {col}x{row} with agent on ({self.agent.col},{self.agent.row}) {self.agent.angle}")
|
||||
|
||||
self.surface = pygame.Surface((self.width, self.height))
|
||||
|
||||
@ -41,6 +42,9 @@ class Board:
|
||||
|
||||
self.grow_next = 0
|
||||
|
||||
self.path = []
|
||||
self.actions = []
|
||||
|
||||
def update_free_spaces(self):
|
||||
self.free_spaces = []
|
||||
|
||||
@ -92,6 +96,15 @@ class Board:
|
||||
def update_agent(self):
|
||||
self.agent.update_animation()
|
||||
|
||||
|
||||
if self.actions and self.agent.moving == self.agent.rotating == 0:
|
||||
action = self.actions.pop(0)
|
||||
|
||||
if action == "move": self.agent.move()
|
||||
elif action == "rotate_left": self.agent.rotate(1)
|
||||
else: self.agent.rotate(-1)
|
||||
|
||||
|
||||
#zebranie grzyba
|
||||
if self.board[self.agent.col][self.agent.row] != False and self.board[self.agent.col][self.agent.row].name == "Mushroom" and self.board[self.agent.col][self.agent.row].grow == 100:
|
||||
self.agent.points += self.board[self.agent.col][self.agent.row].points
|
||||
@ -122,8 +135,22 @@ class Board:
|
||||
x_y = self.free_spaces.pop()
|
||||
self.board[x_y[0]][x_y[1]] = Mushroom(x_y, random.randint(0, 2), True, 0)
|
||||
|
||||
|
||||
|
||||
def a_starxd(self):
|
||||
print(self.agent.angle)
|
||||
fringe = []
|
||||
explored = []
|
||||
self.actions = a_star(self,
|
||||
fringe,
|
||||
explored,
|
||||
(self.agent.col,self.agent.row, self.agent.angle),
|
||||
successor,
|
||||
get_cost,
|
||||
(bfs((self.agent.col,self.agent.row,self.agent.angle),successor,self))
|
||||
)
|
||||
|
||||
print(self.actions)
|
||||
|
||||
return self.actions
|
||||
|
||||
|
||||
|
||||
|
15
container/node.py
Normal file
15
container/node.py
Normal file
@ -0,0 +1,15 @@
|
||||
|
||||
class Node:
|
||||
def __init__(self, x, y, angle = None, parent = None, action = 0, cost=0):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.angle = angle
|
||||
self.parent = parent
|
||||
self.action = action
|
||||
self.cost = cost
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, Node) and (self.x == other.x and self.y == other.y and self.angle == other.angle):
|
||||
return True
|
||||
else:
|
||||
return False
|
25
main.py
25
main.py
@ -2,7 +2,9 @@ import pygame
|
||||
from container.constans import WIDTH, HEIGHT, ROWS, COLUMNS, GREEN
|
||||
from container.board import Board
|
||||
|
||||
FPS = 30
|
||||
|
||||
FPS = 18
|
||||
|
||||
|
||||
#creating game window
|
||||
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
|
||||
@ -34,19 +36,36 @@ def main():
|
||||
if key_input[pygame.K_LEFT]: board.agent.rotate(1)
|
||||
if key_input[pygame.K_UP]: board.agent.move()
|
||||
if key_input[pygame.K_RIGHT]: board.agent.rotate(-1)
|
||||
|
||||
|
||||
|
||||
#drawing map and detective
|
||||
WIN.fill(GREEN)
|
||||
board.update_agent()
|
||||
board.update_agent() #update moves and collecting mushrooms
|
||||
board.grow_mushrooms()
|
||||
|
||||
board.draw_squares(WIN)
|
||||
board.draw_pieces(WIN)
|
||||
board.draw_agent(WIN)
|
||||
board.draw_info(WIN)
|
||||
|
||||
|
||||
pygame.display.update()
|
||||
|
||||
|
||||
if key_input[pygame.K_SPACE]:
|
||||
board.a_starxd()
|
||||
|
||||
if(board.agent.rotating==0 and board.agent.moving==0):
|
||||
board.a_starxd()
|
||||
|
||||
|
||||
|
||||
|
||||
pygame.quit()
|
||||
|
||||
main()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user