remove nearest bomb

This commit is contained in:
matixezor 2021-04-27 21:34:59 +02:00
parent 6a08195fe0
commit b10bfbbbeb
4 changed files with 25 additions and 43 deletions

View File

@ -13,5 +13,5 @@ def generate_field() -> List[List[Tile]]:
class Environment: class Environment:
def __init__(self, field: List[List[Tile]] = None): 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) self.mine_count = sum(sum(1 for tile in row if tile.mine) for row in self.field)

View File

@ -2,7 +2,7 @@ import pygame as pg
from agent import Agent from agent import Agent
from game_ui import GameUi from game_ui import GameUi
from const import ICON, IMAGES from const import ICON, IMAGES, DEBUG_FIELD
from environment import Environment from environment import Environment
from search_algoritms.BFS import breadth_first_search, breadth_first_search_for_a_star from search_algoritms.BFS import breadth_first_search, breadth_first_search_for_a_star
from src.search_algoritms.a_star import 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.display.set_icon(pg.image.load(ICON))
pg.fastevent.init() pg.fastevent.init()
env = Environment() env = Environment(DEBUG_FIELD)
agent = Agent() agent = Agent()
game_ui = GameUi(agent, env) game_ui = GameUi(agent, env)
game_ui.update() game_ui.update()

View File

@ -1,7 +1,5 @@
from typing import List, Tuple from typing import List, Tuple
from ap_mine import APMine
from at_mine import ATMine
from src.node import Node from src.node import Node
from src.tile import Tile from src.tile import Tile
from .helpers import successor, get_path_actions_a_star, calculate_priority 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: while node_queue:
node = node_queue.pop(0) node = node_queue.pop(0)
# przy get wyciąga element z najniższą wartością priorytetu
if (node.x, node.y) == goal: if (node.x, node.y) == goal:
return get_path_actions_a_star(node, field) 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() node_queue.sort()
return False, False 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

View File

@ -6,6 +6,16 @@ from src.node import Node
from src.tile import Tile 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): def successor(field: List[List[Tile]], x: int, y: int, direction: int):
""" """
kody klawiszy: kody klawiszy:
@ -28,28 +38,6 @@ def successor(field: List[List[Tile]], x: int, y: int, direction: int):
return neighbours 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): 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 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): def heuristic(a, b):
return abs(a.x - b[0]) + abs(a.y - b[1]) 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]