diff --git a/container/__pycache__/a_star.cpython-37.pyc b/container/__pycache__/a_star.cpython-37.pyc new file mode 100644 index 0000000..5a53134 Binary files /dev/null and b/container/__pycache__/a_star.cpython-37.pyc differ diff --git a/container/__pycache__/agent.cpython-37.pyc b/container/__pycache__/agent.cpython-37.pyc new file mode 100644 index 0000000..34ecd5c Binary files /dev/null and b/container/__pycache__/agent.cpython-37.pyc differ diff --git a/container/__pycache__/board.cpython-37.pyc b/container/__pycache__/board.cpython-37.pyc index db0cdc2..9700378 100644 Binary files a/container/__pycache__/board.cpython-37.pyc and b/container/__pycache__/board.cpython-37.pyc differ diff --git a/container/__pycache__/constans.cpython-37.pyc b/container/__pycache__/constans.cpython-37.pyc index 0549a88..45761f7 100644 Binary files a/container/__pycache__/constans.cpython-37.pyc and b/container/__pycache__/constans.cpython-37.pyc differ diff --git a/container/__pycache__/mushroom.cpython-37.pyc b/container/__pycache__/mushroom.cpython-37.pyc new file mode 100644 index 0000000..4199da3 Binary files /dev/null and b/container/__pycache__/mushroom.cpython-37.pyc differ diff --git a/container/__pycache__/node.cpython-37.pyc b/container/__pycache__/node.cpython-37.pyc new file mode 100644 index 0000000..b33928f Binary files /dev/null and b/container/__pycache__/node.cpython-37.pyc differ diff --git a/container/__pycache__/piece.cpython-37.pyc b/container/__pycache__/piece.cpython-37.pyc index dc89133..bc840f0 100644 Binary files a/container/__pycache__/piece.cpython-37.pyc and b/container/__pycache__/piece.cpython-37.pyc differ diff --git a/container/__pycache__/tree.cpython-37.pyc b/container/__pycache__/tree.cpython-37.pyc new file mode 100644 index 0000000..8d28d36 Binary files /dev/null and b/container/__pycache__/tree.cpython-37.pyc differ diff --git a/container/a_star.py b/container/a_star.py new file mode 100644 index 0000000..95f88c2 --- /dev/null +++ b/container/a_star.py @@ -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]) + diff --git a/container/board.py b/container/board.py index 63993bb..a6bb551 100644 --- a/container/board.py +++ b/container/board.py @@ -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 diff --git a/container/node.py b/container/node.py new file mode 100644 index 0000000..a2ed0a8 --- /dev/null +++ b/container/node.py @@ -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 \ No newline at end of file diff --git a/main.py b/main.py index 1e66afd..882e5a0 100644 --- a/main.py +++ b/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() + + + + +