Repair agent
This commit is contained in:
parent
27cf66d35d
commit
63645f3b24
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 |
18
src/BFS.py
18
src/BFS.py
@ -11,9 +11,12 @@ def successor(x: int, y: int, direction: int):
|
|||||||
for i in range(4):
|
for i in range(4):
|
||||||
# TODO
|
# TODO
|
||||||
# naprawić by zaczął od prawej i szedł zgodnie z zegarem
|
# 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_x = x + DIRECTIONS_X[i]
|
||||||
neighbour_y = y + DIRECTIONS_Y[i]
|
neighbour_y = y + DIRECTIONS_Y[i]
|
||||||
neighbour_side = DIRECTIONS_SIDE[i]
|
neighbour_side = DIRECTIONS_SIDE[i]
|
||||||
|
# print(neighbour_x, neighbour_y, neighbour_side)
|
||||||
|
|
||||||
if 0 <= neighbour_x <= 9 and 0 <= neighbour_y <= 9:
|
if 0 <= neighbour_x <= 9 and 0 <= neighbour_y <= 9:
|
||||||
# TODO
|
# TODO
|
||||||
@ -26,10 +29,10 @@ def successor(x: int, y: int, direction: int):
|
|||||||
# d 100
|
# d 100
|
||||||
# w 119
|
# w 119
|
||||||
# 1 direction -> prawo
|
# 1 direction -> prawo
|
||||||
# neighbour side 1 -> po prawej 1-1 =0 -> W
|
# neighbour side 1 -> po prawej 1-1 = 0 -> W
|
||||||
# neighbour side 2 -> na gorze 2-1 =1 -> D + W
|
# neighbour side 2 -> na gorze 2-1 = 1 -> D + W
|
||||||
# neighbour side 3 -> po lewej 3-1 =2 -> D + 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 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):
|
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:
|
if field[node.y][node.x].mine:
|
||||||
path = []
|
path = []
|
||||||
actions = []
|
actions = []
|
||||||
print(explored)
|
|
||||||
while node.parent:
|
while node.parent:
|
||||||
path.append((node.x, node.y))
|
path.append((node.x, node.y))
|
||||||
actions.extend(node.actions)
|
actions.append(node.actions)
|
||||||
node = node.parent
|
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):
|
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):
|
||||||
@ -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)
|
neighbour_node = Node(x, y, direction, node, actions)
|
||||||
node_queue.put(neighbour_node)
|
node_queue.put(neighbour_node)
|
||||||
|
|
||||||
return False
|
return False, False
|
||||||
|
10
src/const.py
10
src/const.py
@ -63,6 +63,14 @@ DEFAULT_FIELD = [
|
|||||||
]
|
]
|
||||||
# TODO
|
# TODO
|
||||||
# to poprawić
|
# 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 = {
|
ACTIONS = {
|
||||||
-1: [pg.K_d, pg.K_w],
|
-1: [pg.K_d, pg.K_w],
|
||||||
0: [pg.K_w],
|
0: [pg.K_w],
|
||||||
@ -74,5 +82,5 @@ ACTIONS = {
|
|||||||
}
|
}
|
||||||
# TODO
|
# 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)
|
DIRECTIONS_SIDE = (1, 2, 3, 0)
|
||||||
|
@ -14,7 +14,7 @@ class GameUi:
|
|||||||
|
|
||||||
def move(self):
|
def move(self):
|
||||||
coord = 'x' if self.agent.direction in (1, 3) else 'y'
|
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 coord == 'x':
|
||||||
if self.env.field[self.agent.y][self.agent.x+shift].number in (2, 3):
|
if self.env.field[self.agent.y][self.agent.x+shift].number in (2, 3):
|
||||||
|
19
src/main.py
19
src/main.py
@ -25,10 +25,10 @@ def main():
|
|||||||
# TODO
|
# TODO
|
||||||
# poprawić numeracje stron agenta by szło zgodnie z zegarem
|
# poprawić numeracje stron agenta by szło zgodnie z zegarem
|
||||||
# ruchy agenta:
|
# ruchy agenta:
|
||||||
# 1 - right
|
# 1 - right -> 1
|
||||||
# 2 - up
|
# 0 - up -> 0
|
||||||
# 3 - left
|
# 3 - left -> 3
|
||||||
# 0 - down
|
# 2 - down -> 2
|
||||||
|
|
||||||
while running:
|
while running:
|
||||||
for event in pg.event.get():
|
for event in pg.event.get():
|
||||||
@ -42,11 +42,12 @@ def main():
|
|||||||
elif (event.key == pg.K_w or event.key == pg.K_UP) \
|
elif (event.key == pg.K_w or event.key == pg.K_UP) \
|
||||||
and ((agent.direction == 1 and agent.x * 80 < 700)
|
and ((agent.direction == 1 and agent.x * 80 < 700)
|
||||||
or (agent.direction == 3 and agent.x * 80 > 5)
|
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 > 0)
|
||||||
or (agent.direction == 0 and agent.y * 80 < 700)):
|
or (agent.direction == 2 and agent.y * 80 < 700)):
|
||||||
game_ui.move()
|
game_ui.move()
|
||||||
elif event.key == pg.K_SPACE:
|
elif event.key == pg.K_SPACE:
|
||||||
if env.field[agent.y][agent.x].mine:
|
if env.field[agent.y][agent.x].mine:
|
||||||
|
env.mine_count -= 1
|
||||||
env.field[agent.y][agent.x] = factory.create_tile(
|
env.field[agent.y][agent.x] = factory.create_tile(
|
||||||
IMAGES[env.field[agent.y][agent.x].number].parent
|
IMAGES[env.field[agent.y][agent.x].number].parent
|
||||||
)
|
)
|
||||||
@ -55,9 +56,15 @@ def main():
|
|||||||
# TODO
|
# TODO
|
||||||
# petla while dopóki env.mine_count != 0
|
# petla while dopóki env.mine_count != 0
|
||||||
# uwzglednienie czy bfs zwraca False i wtedy break
|
# 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)
|
path, actions = breadth_first_search(env.field, agent.x, agent.y, agent.direction)
|
||||||
print(path, actions)
|
print(path, actions)
|
||||||
|
if not path:
|
||||||
|
print("CHUJA")
|
||||||
|
break
|
||||||
for action in actions:
|
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': action}))
|
||||||
|
|
||||||
pg.event.post(pg.event.Event(pg.KEYDOWN, {'key': pg.K_SPACE}))
|
pg.event.post(pg.event.Event(pg.KEYDOWN, {'key': pg.K_SPACE}))
|
||||||
|
Loading…
Reference in New Issue
Block a user