add actions, directions to node. add mine_count to env

This commit is contained in:
matixezor 2021-04-12 10:51:33 +02:00
parent a7fb859e32
commit 27cf66d35d
5 changed files with 55 additions and 20 deletions

View File

@ -1,50 +1,62 @@
import queue
from typing import Tuple, List
from typing import List
from node import Node
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 = []
for i in range(4):
# TODO
# naprawić by zaczął od prawej i szedł zgodnie z zegarem
neighbour_x = x + DIRECTIONS_X[i]
neighbour_y = y + DIRECTIONS_Y[i]
neighbour_side = DIRECTIONS_SIDE[i]
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
# 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]):
# TODO
# 1. Implementacja akcji (zapisywanie tego co robot musi zrobić by przejść z jednego node do drugiego)
# 2. Wypisanie drogi razem z akcjami
def breadth_first_search(field: List[List[Tile]], start_x: int, start_y: int, start_direction: int):
explored = [(start_x, start_y)]
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():
node = node_queue.get()
if (node.x, node.y) == goal:
if field[node.y][node.x].mine:
path = []
actions = []
print(explored)
while node.parent:
path.append((node.x, node.y))
actions.extend(node.actions)
node = node.parent
print(path[::-1])
return True
return path, actions
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):
continue
elif (x, y) in explored:
continue
explored.append((x, y))
neighbour_node = Node(x, y, node)
neighbour_node = Node(x, y, direction, node, actions)
node_queue.put(neighbour_node)
return False

View File

@ -61,7 +61,18 @@ DEFAULT_FIELD = [
[5, 2, 0, 1, 3, 1, 2, 1, 0, 0],
[2, 5, 1, 4, 5, 1, 0, 5, 4, 0],
]
ACTIONS = ()
# TODO
# 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_Y = (0, -1, 0, 1)
DIRECTIONS_SIDE = (1, 2, 3, 0)

View File

@ -14,3 +14,4 @@ def generate_field() -> List[List[Tile]]:
class Environment:
def __init__(self, field: List[List[Tile]] = None):
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)

View File

@ -22,6 +22,8 @@ def main():
running = True
# TODO
# poprawić numeracje stron agenta by szło zgodnie z zegarem
# ruchy agenta:
# 1 - right
# 2 - up
@ -50,7 +52,15 @@ def main():
)
game_ui.update()
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__":

View File

@ -1,11 +1,12 @@
from __future__ import annotations
from pygame import event
from typing import List
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.y = y
self.direction = direction
self.parent = parent
self.action = action
self.actions = actions