From b10bfbbbebd5f238eb404dfc52ecc5a3e25fd9c9 Mon Sep 17 00:00:00 2001 From: matixezor Date: Tue, 27 Apr 2021 21:34:59 +0200 Subject: [PATCH 1/2] remove nearest bomb --- src/environment.py | 2 +- src/main.py | 4 +-- src/search_algoritms/a_star.py | 18 -------------- src/search_algoritms/helpers.py | 44 ++++++++++++++++----------------- 4 files changed, 25 insertions(+), 43 deletions(-) diff --git a/src/environment.py b/src/environment.py index 041334e..d0d0a0b 100644 --- a/src/environment.py +++ b/src/environment.py @@ -13,5 +13,5 @@ def generate_field() -> List[List[Tile]]: class Environment: def __init__(self, field: List[List[Tile]] = None): - self.field = generate_field() + self.field = field if field else generate_field() self.mine_count = sum(sum(1 for tile in row if tile.mine) for row in self.field) diff --git a/src/main.py b/src/main.py index 17c7f39..5f07f85 100644 --- a/src/main.py +++ b/src/main.py @@ -2,7 +2,7 @@ import pygame as pg from agent import Agent from game_ui import GameUi -from const import ICON, IMAGES +from const import ICON, IMAGES, DEBUG_FIELD from environment import Environment from search_algoritms.BFS import breadth_first_search, breadth_first_search_for_a_star from src.search_algoritms.a_star import a_star @@ -41,7 +41,7 @@ def main(): pg.display.set_icon(pg.image.load(ICON)) pg.fastevent.init() - env = Environment() + env = Environment(DEBUG_FIELD) agent = Agent() game_ui = GameUi(agent, env) game_ui.update() diff --git a/src/search_algoritms/a_star.py b/src/search_algoritms/a_star.py index f71201b..ebba564 100644 --- a/src/search_algoritms/a_star.py +++ b/src/search_algoritms/a_star.py @@ -1,7 +1,5 @@ from typing import List, Tuple -from ap_mine import APMine -from at_mine import ATMine from src.node import Node from src.tile import Tile from .helpers import successor, get_path_actions_a_star, calculate_priority @@ -13,7 +11,6 @@ def a_star(field: List[List[Tile]], start_x: int, start_y: int, start_direction: while node_queue: node = node_queue.pop(0) - # przy get wyciąga element z najniższą wartością priorytetu if (node.x, node.y) == goal: return get_path_actions_a_star(node, field) @@ -33,18 +30,3 @@ def a_star(field: List[List[Tile]], start_x: int, start_y: int, start_direction: node_queue.sort() return False, False - - -def nearest_bomb(a: Tuple[int, int], field: List[List[Tile]]): - min = 20 - min_x = 0 - min_y = 0 - for x in range(10): - for y in range(10): - if isinstance(field[y][x].mine, (APMine, ATMine)): - distance = abs(a[0] - x) + abs(a[1] - y) - if distance < min: - min = distance - min_x = x - min_y = y - return min_x, min_y diff --git a/src/search_algoritms/helpers.py b/src/search_algoritms/helpers.py index bf8390d..cd85841 100644 --- a/src/search_algoritms/helpers.py +++ b/src/search_algoritms/helpers.py @@ -6,6 +6,16 @@ from src.node import Node from src.tile import Tile +def get_path_actions(node: Node): + path = [] + actions = [] + while node.parent: + path.append((node.x, node.y)) + actions.append(node.action) + node = node.parent + return path[::-1], actions[::-1] + + def successor(field: List[List[Tile]], x: int, y: int, direction: int): """ kody klawiszy: @@ -28,28 +38,6 @@ def successor(field: List[List[Tile]], x: int, y: int, direction: int): return neighbours -def get_path_actions(node: Node, field: List[List[Tile]]): - path = [] - actions = [] - while node.parent: - path.append((node.x, node.y)) - actions.append(node.action) - node = node.parent - return path[::-1], actions[::-1] - -def get_path_actions_a_star(node: Node, field: List[List[Tile]]): - path = [] - actions = [] - while node.parent: - path.append((node.x, node.y)) - print(field[node.y][node.x], field[node.y][node.x].weight, node.x, node.y, field[node.y][node.x].mine) - if field[node.y][node.x].mine: - actions.append(32) - actions.append(node.action) - node = node.parent - return path[::-1], actions[::-1] - - def cost(field: List[List[Tile]], node): return 1 if (node.parent.x, node.parent.y) == (node.x, node.y) else field[node.y][node.x].weight @@ -60,3 +48,15 @@ def calculate_priority(field: List[List[Tile]], node, goal_test: Tuple[int, int def heuristic(a, b): return abs(a.x - b[0]) + abs(a.y - b[1]) + + +def get_path_actions_a_star(node: Node, field: List[List[Tile]]): + path = [] + actions = [] + while node.parent: + path.append((node.x, node.y)) + if field[node.y][node.x].mine: + actions.append(32) + actions.append(node.action) + node = node.parent + return path[::-1], actions[::-1] From 04bbaedb4c796bb6598fba2ccf0716ca024bb621 Mon Sep 17 00:00:00 2001 From: matixezor Date: Sun, 16 May 2021 11:59:06 +0200 Subject: [PATCH 2/2] code refactor --- src/main.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main.py b/src/main.py index 5f07f85..ceaab63 100644 --- a/src/main.py +++ b/src/main.py @@ -63,11 +63,11 @@ def main(): if event.key == pg.K_t: print('Starting to clear the sector') while env.mine_count: - print('-'*20) - #path, actions = breadth_first_search(env.field, agent.x, agent.y, agent.direction) - path, actions = a_star (env.field, agent.x, agent.y, agent.direction, - breadth_first_search_for_a_star (env.field, agent.x, agent.y, - agent.direction)) + print('-' * 20) + # path, actions = breadth_first_search(env.field, agent.x, agent.y, agent.direction) + path, actions = a_star(env.field, agent.x, agent.y, agent.direction, + breadth_first_search_for_a_star(env.field, agent.x, agent.y, + agent.direction)) if not path and not env.field[agent.y][agent.x].mine: print('Unable to find path, rocks are in the way') break