add actions, directions to node. add mine_count to env
This commit is contained in:
parent
a7fb859e32
commit
27cf66d35d
40
src/BFS.py
40
src/BFS.py
@ -1,50 +1,62 @@
|
|||||||
import queue
|
import queue
|
||||||
from typing import Tuple, List
|
from typing import List
|
||||||
|
|
||||||
from node import Node
|
from node import Node
|
||||||
from tile import Tile
|
from tile import Tile
|
||||||
from const import DIRECTIONS_X, DIRECTIONS_Y
|
from const import DIRECTIONS_X, DIRECTIONS_Y, DIRECTIONS_SIDE, ACTIONS
|
||||||
|
|
||||||
|
|
||||||
def successor(x: int, y: int):
|
def successor(x: int, y: int, direction: int):
|
||||||
neighbours = []
|
neighbours = []
|
||||||
for i in range(4):
|
for i in range(4):
|
||||||
|
# TODO
|
||||||
|
# naprawić by zaczął od prawej i szedł zgodnie z zegarem
|
||||||
neighbour_x = x + DIRECTIONS_X[i]
|
neighbour_x = x + DIRECTIONS_X[i]
|
||||||
neighbour_y = y + DIRECTIONS_Y[i]
|
neighbour_y = y + DIRECTIONS_Y[i]
|
||||||
|
neighbour_side = DIRECTIONS_SIDE[i]
|
||||||
|
|
||||||
if 0 <= neighbour_x <= 9 and 0 <= neighbour_y <= 9:
|
if 0 <= neighbour_x <= 9 and 0 <= neighbour_y <= 9:
|
||||||
neighbours.append((neighbour_x, neighbour_y))
|
# TODO
|
||||||
|
# wymyśleć sposób by dla każdej strony odnaleźć akcje w ACTIONS
|
||||||
|
neighbours.append((neighbour_x, neighbour_y, neighbour_side, ACTIONS[neighbour_side-direction]))
|
||||||
|
|
||||||
return neighbours
|
return neighbours
|
||||||
|
# kody klawiszy
|
||||||
|
# a 97
|
||||||
|
# d 100
|
||||||
|
# w 119
|
||||||
|
# 1 direction -> prawo
|
||||||
|
# neighbour side 1 -> po prawej 1-1 =0 -> W
|
||||||
|
# neighbour side 2 -> na gorze 2-1 =1 -> D + W
|
||||||
|
# neighbour side 3 -> po lewej 3-1 =2 -> D + D + W
|
||||||
|
# neighbour side 0 -> na dole 0-1 =-1 -> A + W
|
||||||
|
|
||||||
|
|
||||||
def breadth_first_search(field: List[List[Tile]], start_x: int, start_y: int, goal: Tuple[int, int]):
|
def breadth_first_search(field: List[List[Tile]], start_x: int, start_y: int, start_direction: int):
|
||||||
# TODO
|
|
||||||
# 1. Implementacja akcji (zapisywanie tego co robot musi zrobić by przejść z jednego node do drugiego)
|
|
||||||
# 2. Wypisanie drogi razem z akcjami
|
|
||||||
explored = [(start_x, start_y)]
|
explored = [(start_x, start_y)]
|
||||||
node_queue = queue.SimpleQueue()
|
node_queue = queue.SimpleQueue()
|
||||||
node_queue.put(Node(start_x, start_y))
|
node_queue.put(Node(start_x, start_y, start_direction))
|
||||||
|
|
||||||
while not node_queue.empty():
|
while not node_queue.empty():
|
||||||
node = node_queue.get()
|
node = node_queue.get()
|
||||||
|
|
||||||
if (node.x, node.y) == goal:
|
if field[node.y][node.x].mine:
|
||||||
path = []
|
path = []
|
||||||
|
actions = []
|
||||||
print(explored)
|
print(explored)
|
||||||
while node.parent:
|
while node.parent:
|
||||||
path.append((node.x, node.y))
|
path.append((node.x, node.y))
|
||||||
|
actions.extend(node.actions)
|
||||||
node = node.parent
|
node = node.parent
|
||||||
print(path[::-1])
|
return path, actions
|
||||||
return True
|
|
||||||
|
|
||||||
for x, y in successor(node.x, node.y):
|
for x, y, direction, actions in successor(node.x, node.y, node.direction):
|
||||||
if field[y][x].number in (2, 3):
|
if field[y][x].number in (2, 3):
|
||||||
continue
|
continue
|
||||||
elif (x, y) in explored:
|
elif (x, y) in explored:
|
||||||
continue
|
continue
|
||||||
explored.append((x, y))
|
explored.append((x, y))
|
||||||
neighbour_node = Node(x, y, node)
|
neighbour_node = Node(x, y, direction, node, actions)
|
||||||
node_queue.put(neighbour_node)
|
node_queue.put(neighbour_node)
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
15
src/const.py
15
src/const.py
@ -61,7 +61,18 @@ DEFAULT_FIELD = [
|
|||||||
[5, 2, 0, 1, 3, 1, 2, 1, 0, 0],
|
[5, 2, 0, 1, 3, 1, 2, 1, 0, 0],
|
||||||
[2, 5, 1, 4, 5, 1, 0, 5, 4, 0],
|
[2, 5, 1, 4, 5, 1, 0, 5, 4, 0],
|
||||||
]
|
]
|
||||||
|
# TODO
|
||||||
ACTIONS = ()
|
# to poprawić
|
||||||
|
ACTIONS = {
|
||||||
|
-1: [pg.K_d, pg.K_w],
|
||||||
|
0: [pg.K_w],
|
||||||
|
1: [pg.K_a, pg.K_w],
|
||||||
|
2: [pg.K_d, pg.K_d, pg.K_w],
|
||||||
|
-2: [pg.K_d, pg.K_d, pg.K_w],
|
||||||
|
-3: [pg.K_a, pg.K_w],
|
||||||
|
3: [pg.K_d, pg.K_w]
|
||||||
|
}
|
||||||
|
# TODO
|
||||||
DIRECTIONS_X = (1, 0, -1, 0)
|
DIRECTIONS_X = (1, 0, -1, 0)
|
||||||
DIRECTIONS_Y = (0, -1, 0, 1)
|
DIRECTIONS_Y = (0, -1, 0, 1)
|
||||||
|
DIRECTIONS_SIDE = (1, 2, 3, 0)
|
||||||
|
@ -14,3 +14,4 @@ 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 = field if field else 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)
|
||||||
|
12
src/main.py
12
src/main.py
@ -22,6 +22,8 @@ def main():
|
|||||||
|
|
||||||
running = True
|
running = True
|
||||||
|
|
||||||
|
# TODO
|
||||||
|
# poprawić numeracje stron agenta by szło zgodnie z zegarem
|
||||||
# ruchy agenta:
|
# ruchy agenta:
|
||||||
# 1 - right
|
# 1 - right
|
||||||
# 2 - up
|
# 2 - up
|
||||||
@ -50,7 +52,15 @@ def main():
|
|||||||
)
|
)
|
||||||
game_ui.update()
|
game_ui.update()
|
||||||
elif event.key == pg.K_t:
|
elif event.key == pg.K_t:
|
||||||
print(breadth_first_search(env.field, 0, 0, (0, 2)))
|
# TODO
|
||||||
|
# petla while dopóki env.mine_count != 0
|
||||||
|
# uwzglednienie czy bfs zwraca False i wtedy break
|
||||||
|
path, actions = breadth_first_search(env.field, agent.x, agent.y, agent.direction)
|
||||||
|
print(path, actions)
|
||||||
|
for action in actions:
|
||||||
|
pg.event.post(pg.event.Event(pg.KEYDOWN, {'key': action}))
|
||||||
|
|
||||||
|
pg.event.post(pg.event.Event(pg.KEYDOWN, {'key': pg.K_SPACE}))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from pygame import event
|
from pygame import event
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
|
||||||
class Node:
|
class Node:
|
||||||
def __init__(self, x: int, y: int, parent: Node = None, action: event = None):
|
def __init__(self, x: int, y: int, direction: int, parent: Node = None, actions: List[event] = None):
|
||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
|
self.direction = direction
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
self.action = action
|
self.actions = actions
|
||||||
|
Loading…
Reference in New Issue
Block a user