fix some stuff
This commit is contained in:
parent
7e61c61116
commit
77afd5cc15
@ -8,7 +8,7 @@ from tilesFactory import TilesFactory
|
|||||||
def generate_field() -> List[List[Tile]]:
|
def generate_field() -> List[List[Tile]]:
|
||||||
tiles_factory = TilesFactory()
|
tiles_factory = TilesFactory()
|
||||||
tiles_list = tiles_factory.get_tiles_list()
|
tiles_list = tiles_factory.get_tiles_list()
|
||||||
return [choices(tiles_list, weights=[10, 10, 4, 4, 4, 4, 4, 4, 4, 4, 4,2,2], k=12) for _ in range(12)]
|
return [choices(tiles_list, weights=[10, 10, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2], k=10) for _ in range(10)]
|
||||||
|
|
||||||
|
|
||||||
class Environment:
|
class Environment:
|
||||||
|
28
src/main.py
28
src/main.py
@ -64,21 +64,21 @@ def main():
|
|||||||
elif event.type == pg.KEYDOWN:
|
elif event.type == pg.KEYDOWN:
|
||||||
if event.key == pg.K_t:
|
if event.key == pg.K_t:
|
||||||
print('Starting to clear the sector')
|
print('Starting to clear the sector')
|
||||||
while env.mine_count:
|
#while env.mine_count:
|
||||||
print('-'*20)
|
print('-'*20)
|
||||||
path, actions = breadth_first_search(env.field, agent.x, agent.y, agent.direction)
|
#path, actions = breadth_first_search(env.field, agent.x, agent.y, agent.direction)
|
||||||
#xd: Tuple[int,int] = Tuple[agent.x, agent.y]
|
#xd: Tuple[int,int] = Tuple[agent.x, agent.y]
|
||||||
# path, actions = a_star(env.field, agent.x, agent.y, agent.direction, nearest_bomb((agent.x, agent.y), env.field))
|
path, actions = a_star(env.field, agent.x, agent.y, agent.direction, nearest_bomb((agent.x, agent.y), env.field))
|
||||||
if not path and not env.field[agent.y][agent.x].mine:
|
if not path and not env.field[agent.y][agent.x].mine:
|
||||||
print('Unable to find path, rocks are in the way')
|
print('Unable to find path, rocks are in the way')
|
||||||
break
|
break
|
||||||
print(f'Path{path}')
|
print(f'Path{path}')
|
||||||
print(f'Actions:{actions}')
|
print(f'Actions:{actions}')
|
||||||
for action in actions:
|
for action in actions:
|
||||||
pg.fastevent.post(pg.event.Event(pg.KEYDOWN, {'key': action}))
|
pg.fastevent.post(pg.event.Event(pg.KEYDOWN, {'key': action}))
|
||||||
|
|
||||||
pg.fastevent.post(pg.event.Event(pg.KEYDOWN, {'key': pg.K_SPACE}))
|
pg.fastevent.post(pg.event.Event(pg.KEYDOWN, {'key': pg.K_SPACE}))
|
||||||
handle_keys(env, agent, game_ui, factory)
|
handle_keys(env, agent, game_ui, factory)
|
||||||
print('Sector clear')
|
print('Sector clear')
|
||||||
else:
|
else:
|
||||||
pg.fastevent.post(event)
|
pg.fastevent.post(event)
|
||||||
|
@ -3,11 +3,13 @@ from operator import itemgetter
|
|||||||
|
|
||||||
from src.node import Node
|
from src.node import Node
|
||||||
from src.tile import Tile
|
from src.tile import Tile
|
||||||
|
from ap_mine import APMine
|
||||||
|
from at_mine import ATMine
|
||||||
from .helpers import successor, get_path_actions, calculate_priority
|
from .helpers import successor, get_path_actions, calculate_priority
|
||||||
from ..mine import Mine
|
|
||||||
|
|
||||||
|
|
||||||
def a_star(field: List[List[Tile]], start_x: int, start_y: int, start_direction: int, goal: Tuple[int, int]):
|
def a_star(field: List[List[Tile]], start_x: int, start_y: int, start_direction: int, goal: Tuple[int, int]):
|
||||||
|
print(goal)
|
||||||
explored = []
|
explored = []
|
||||||
node_queue: List[Tuple[int, Node]] = [(field[start_x][start_y].weight, Node(start_x, start_y, start_direction))]
|
node_queue: List[Tuple[int, Node]] = [(field[start_x][start_y].weight, Node(start_x, start_y, start_direction))]
|
||||||
# do kolejki dodawać krotke (priorytet, obiekt)
|
# do kolejki dodawać krotke (priorytet, obiekt)
|
||||||
@ -16,7 +18,7 @@ def a_star(field: List[List[Tile]], start_x: int, start_y: int, start_direction:
|
|||||||
priority, node = node_queue.pop(0)
|
priority, node = node_queue.pop(0)
|
||||||
# przy get wyciąga element z najniższą wartością priorytetu
|
# przy get wyciąga element z najniższą wartością priorytetu
|
||||||
|
|
||||||
if field[node.y][node.x] == goal:
|
if (node.x, node.y) == goal:
|
||||||
return get_path_actions(node)
|
return get_path_actions(node)
|
||||||
|
|
||||||
explored.append(node)
|
explored.append(node)
|
||||||
@ -29,7 +31,6 @@ def a_star(field: List[List[Tile]], start_x: int, start_y: int, start_direction:
|
|||||||
elif (index := [
|
elif (index := [
|
||||||
index for index, queue_tuple in enumerate(node_queue) if neighbour_priority > queue_tuple[0] and neighbour_node == queue_tuple[1]
|
index for index, queue_tuple in enumerate(node_queue) if neighbour_priority > queue_tuple[0] and neighbour_node == queue_tuple[1]
|
||||||
]):
|
]):
|
||||||
print(index)
|
|
||||||
node_queue[index[0]] = (neighbour_priority, neighbour_node)
|
node_queue[index[0]] = (neighbour_priority, neighbour_node)
|
||||||
node_queue = sorted(node_queue, key=itemgetter(0))
|
node_queue = sorted(node_queue, key=itemgetter(0))
|
||||||
|
|
||||||
@ -42,13 +43,13 @@ def heuristic(a: Tuple[int, int], b: Tuple[int, int]):
|
|||||||
return abs(x1 - x2) + abs(y1 - y2)
|
return abs(x1 - x2) + abs(y1 - y2)
|
||||||
|
|
||||||
|
|
||||||
def nearest_bomb(a: Tuple[int, int],field: List[List[Tile]]):
|
def nearest_bomb(a: Tuple[int, int], field: List[List[Tile]]):
|
||||||
min = 20
|
min = 20
|
||||||
min_x = 0
|
min_x = 0
|
||||||
min_y = 0
|
min_y = 0
|
||||||
for x in range(10):
|
for x in range(10):
|
||||||
for y in range(10):
|
for y in range(10):
|
||||||
if issubclass(Mine,field[x][y].mine.__class__):
|
if isinstance(field[y][x].mine, (APMine, ATMine)):
|
||||||
distance = abs(a[0] - x) + abs(a[1] - y)
|
distance = abs(a[0] - x) + abs(a[1] - y)
|
||||||
if distance < min:
|
if distance < min:
|
||||||
min = distance
|
min = distance
|
||||||
|
@ -10,9 +10,9 @@ class TilesFactory:
|
|||||||
img = IMAGES[number]
|
img = IMAGES[number]
|
||||||
if img.mine_type:
|
if img.mine_type:
|
||||||
module = import_module(f'{img.mine_type.lower()}_mine')
|
module = import_module(f'{img.mine_type.lower()}_mine')
|
||||||
return Tile(number, 1, getattr(module, f'{img.mine_type}Mine'))
|
return Tile(number, 1, getattr(module, f'{img.mine_type}Mine')())
|
||||||
else:
|
else:
|
||||||
return Tile(number, 1, None) if img.name.find('grass') else Tile(number,3, None)
|
return Tile(number, 1, None) if img.name.find('grass') else Tile(number, 3, None)
|
||||||
|
|
||||||
def get_tiles_list(self) -> List[Tile]:
|
def get_tiles_list(self) -> List[Tile]:
|
||||||
return [self.create_tile(i) for i in range(13)]
|
return [self.create_tile(i) for i in range(13)]
|
||||||
|
Loading…
Reference in New Issue
Block a user