Repair agent

This commit is contained in:
Darek Golomski 2021-04-12 16:02:10 +02:00
parent 27cf66d35d
commit 63645f3b24
6 changed files with 33 additions and 16 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 5.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.6 KiB

After

Width:  |  Height:  |  Size: 13 KiB

View File

@ -11,9 +11,12 @@ def successor(x: int, y: int, direction: int):
for i in range(4):
# TODO
# naprawić by zaczął od prawej i szedł zgodnie z zegarem
# prawo -> góra -> lewo -> dół
# chcemy: prawo -> dół -> lewo -> góra
neighbour_x = x + DIRECTIONS_X[i]
neighbour_y = y + DIRECTIONS_Y[i]
neighbour_side = DIRECTIONS_SIDE[i]
# print(neighbour_x, neighbour_y, neighbour_side)
if 0 <= neighbour_x <= 9 and 0 <= neighbour_y <= 9:
# TODO
@ -26,10 +29,10 @@ def successor(x: int, y: int, direction: int):
# 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
# 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, start_direction: int):
@ -43,12 +46,11 @@ def breadth_first_search(field: List[List[Tile]], start_x: int, start_y: int, st
if field[node.y][node.x].mine:
path = []
actions = []
print(explored)
while node.parent:
path.append((node.x, node.y))
actions.extend(node.actions)
actions.append(node.actions)
node = node.parent
return path, actions
return path[::-1], sum(actions[::-1], [])
for x, y, direction, actions in successor(node.x, node.y, node.direction):
if field[y][x].number in (2, 3):
@ -59,4 +61,4 @@ def breadth_first_search(field: List[List[Tile]], start_x: int, start_y: int, st
neighbour_node = Node(x, y, direction, node, actions)
node_queue.put(neighbour_node)
return False
return False, False

View File

@ -63,6 +63,14 @@ DEFAULT_FIELD = [
]
# TODO
# to poprawić
# -1 -> prawo, ruch
# 0 -> ruch
# 1 -> lewo, ruch
# -2 -> prawo, prawo, ruch
# 2 -> lewo, lewo, ruch
# -3 -> lewo, ruch
# 3 -> prawo, ruch
ACTIONS = {
-1: [pg.K_d, pg.K_w],
0: [pg.K_w],
@ -74,5 +82,5 @@ ACTIONS = {
}
# TODO
DIRECTIONS_X = (1, 0, -1, 0)
DIRECTIONS_Y = (0, -1, 0, 1)
DIRECTIONS_Y = (0, 1, 0, -1)
DIRECTIONS_SIDE = (1, 2, 3, 0)

View File

@ -14,7 +14,7 @@ class GameUi:
def move(self):
coord = 'x' if self.agent.direction in (1, 3) else 'y'
shift = -1 if self.agent.direction in (2, 3) else 1
shift = -1 if self.agent.direction in (0, 3) else 1
if coord == 'x':
if self.env.field[self.agent.y][self.agent.x+shift].number in (2, 3):

View File

@ -25,10 +25,10 @@ def main():
# TODO
# poprawić numeracje stron agenta by szło zgodnie z zegarem
# ruchy agenta:
# 1 - right
# 2 - up
# 3 - left
# 0 - down
# 1 - right -> 1
# 0 - up -> 0
# 3 - left -> 3
# 2 - down -> 2
while running:
for event in pg.event.get():
@ -42,11 +42,12 @@ def main():
elif (event.key == pg.K_w or event.key == pg.K_UP) \
and ((agent.direction == 1 and agent.x * 80 < 700)
or (agent.direction == 3 and agent.x * 80 > 5)
or (agent.direction == 2 and agent.y * 80 > 0)
or (agent.direction == 0 and agent.y * 80 < 700)):
or (agent.direction == 0 and agent.y * 80 > 0)
or (agent.direction == 2 and agent.y * 80 < 700)):
game_ui.move()
elif event.key == pg.K_SPACE:
if env.field[agent.y][agent.x].mine:
env.mine_count -= 1
env.field[agent.y][agent.x] = factory.create_tile(
IMAGES[env.field[agent.y][agent.x].number].parent
)
@ -55,9 +56,15 @@ def main():
# TODO
# petla while dopóki env.mine_count != 0
# uwzglednienie czy bfs zwraca False i wtedy break
# for i in range(1):
pg.time.delay(50)
path, actions = breadth_first_search(env.field, agent.x, agent.y, agent.direction)
print(path, actions)
if not path:
print("CHUJA")
break
for action in actions:
print(action)
pg.event.post(pg.event.Event(pg.KEYDOWN, {'key': action}))
pg.event.post(pg.event.Event(pg.KEYDOWN, {'key': pg.K_SPACE}))