added moving by bfs
This commit is contained in:
parent
1b2a4a376b
commit
a67d160f9a
5
main.py
5
main.py
@ -19,8 +19,7 @@ def main():
|
||||
screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))
|
||||
pygame.display.set_caption('TRAKTOHOLIK')
|
||||
|
||||
search = BFSSearcher().search((8, 1), (6, 2), Constants.UP)
|
||||
print(search)
|
||||
path = BFSSearcher().search((8, 1), (6, 2), Constants.UP)
|
||||
|
||||
run = True
|
||||
while run:
|
||||
@ -33,7 +32,7 @@ def main():
|
||||
if event.type == pygame.QUIT:
|
||||
run = False
|
||||
|
||||
tractor.update()
|
||||
tractor.update(path)
|
||||
tractor.check_collision(obstacles)
|
||||
pygame.display.update()
|
||||
pygame.quit()
|
||||
|
BIN
src/__pycache__/constants.cpython-39.pyc
Normal file
BIN
src/__pycache__/constants.cpython-39.pyc
Normal file
Binary file not shown.
BIN
src/__pycache__/settings.cpython-39.pyc
Normal file
BIN
src/__pycache__/settings.cpython-39.pyc
Normal file
Binary file not shown.
BIN
src/__pycache__/tile.cpython-39.pyc
Normal file
BIN
src/__pycache__/tile.cpython-39.pyc
Normal file
Binary file not shown.
BIN
src/__pycache__/tractor.cpython-39.pyc
Normal file
BIN
src/__pycache__/tractor.cpython-39.pyc
Normal file
Binary file not shown.
BIN
src/__pycache__/world.cpython-39.pyc
Normal file
BIN
src/__pycache__/world.cpython-39.pyc
Normal file
Binary file not shown.
@ -1,7 +1,7 @@
|
||||
import pygame
|
||||
import math
|
||||
from pygame.sprite import Sprite
|
||||
|
||||
from constants import Constants as C
|
||||
|
||||
class Tractor(Sprite):
|
||||
""" Class to represent our agent """
|
||||
@ -88,16 +88,17 @@ class Tractor(Sprite):
|
||||
self.rect.y += self.curr_direction[1] * self.settings.tile_size # jak wejdzie na kolizje to cofamy ruch
|
||||
# w przyszlosci mozna zmienic
|
||||
|
||||
def update(self):
|
||||
key = pygame.key.get_pressed()
|
||||
def update(self, path):
|
||||
|
||||
if key[pygame.K_d] and self.rect.x:
|
||||
self.turn_right()
|
||||
elif key[pygame.K_a]:
|
||||
self.turn_left()
|
||||
elif key[pygame.K_w]:
|
||||
self.move()
|
||||
# print(self.rect)
|
||||
while(path):
|
||||
action = path.pop(0)
|
||||
if action == C.ROTATE_RIGHT and self.rect.x:
|
||||
self.turn_right()
|
||||
elif action == C.ROTATE_LEFT:
|
||||
self.turn_left()
|
||||
elif action == C.MOVE:
|
||||
self.move()
|
||||
# print(self.rect)
|
||||
|
||||
def draw(self, screen):
|
||||
screen.blit(self.image, self.rect)
|
||||
@ -120,4 +121,4 @@ class Tractor(Sprite):
|
||||
def fertilize(self, tile):
|
||||
if not tile.is_fertilized:
|
||||
tile.is_fertilized = True
|
||||
tile.fertilizer = tile.plant.fertilizer
|
||||
tile.fertilizer = tile.plant.fertilizer
|
||||
|
BIN
src/utils/__pycache__/bfs.cpython-39.pyc
Normal file
BIN
src/utils/__pycache__/bfs.cpython-39.pyc
Normal file
Binary file not shown.
@ -5,7 +5,6 @@ class Node:
|
||||
def __init__(self, current_x, current_y, agent_direction, action=None, parent=None):
|
||||
self.current_x = current_x
|
||||
self.current_y = current_y
|
||||
self.state = tuple([self.current_x, self.current_y])
|
||||
self.action = action
|
||||
self.parent = parent
|
||||
self.agent_direction = agent_direction
|
||||
@ -40,7 +39,7 @@ class Node:
|
||||
|
||||
neighbours.append((actions, (x - 1, y), C.LEFT))
|
||||
|
||||
if y < 9: # upper neighbour
|
||||
if y > 0: # upper neighbour
|
||||
if self.agent_direction == C.RIGHT:
|
||||
actions = [C.ROTATE_LEFT, C.MOVE]
|
||||
elif self.agent_direction == C.LEFT:
|
||||
@ -50,9 +49,9 @@ class Node:
|
||||
elif self.agent_direction == C.DOWN:
|
||||
actions = [C.ROTATE_LEFT, C.ROTATE_LEFT, C.MOVE]
|
||||
|
||||
neighbours.append((actions, (x, y + 1), C.UP))
|
||||
neighbours.append((actions, (x, y - 1), C.UP))
|
||||
|
||||
if y > 0: # down neighbour
|
||||
if y < 9: # down neighbour
|
||||
if self.agent_direction == C.RIGHT:
|
||||
actions = [C.ROTATE_RIGHT, C.MOVE]
|
||||
elif self.agent_direction == C.LEFT:
|
||||
@ -62,12 +61,10 @@ class Node:
|
||||
elif self.agent_direction == C.DOWN:
|
||||
actions = C.MOVE
|
||||
|
||||
neighbours.append((actions, (x, y - 1), C.DOWN))
|
||||
neighbours.append((actions, (x, y + 1), C.DOWN))
|
||||
|
||||
return neighbours
|
||||
|
||||
def __str__(self):
|
||||
return self.state + ' ' + self.parent
|
||||
|
||||
|
||||
class BFSSearcher:
|
||||
|
||||
@ -76,27 +73,30 @@ class BFSSearcher:
|
||||
self.explored_states = []
|
||||
self.path = []
|
||||
|
||||
def search(self, first_state: tuple, goal: tuple, agent_direction: list):
|
||||
def search(self, first_state: tuple, goal, agent_direction):
|
||||
self.fringe.append(Node(first_state[0], first_state[1], agent_direction))
|
||||
|
||||
while True:
|
||||
if not self.fringe:
|
||||
return False
|
||||
element: Node = self.fringe.pop(0)
|
||||
element = self.fringe.pop(0)
|
||||
|
||||
if element.state == goal:
|
||||
return self.__state_eq_goal_action(element)
|
||||
|
||||
self.explored_states.append(element)
|
||||
for action, state, direction in element.successor():
|
||||
|
||||
for action, state, direction in element.succesor():
|
||||
fringe_states = []
|
||||
explored_states = []
|
||||
for node in self.fringe:
|
||||
fringe_states.append(node.state)
|
||||
for node in self.explored_states:
|
||||
explored_states.append(node.state)
|
||||
if (state not in fringe_states) and (state not in explored_states):
|
||||
x = Node(state[0], state[1], direction, action, element)
|
||||
explored_states.append(node)
|
||||
if state not in fringe_states and state not in explored_states:
|
||||
x = Node(state[0], state[1], direction)
|
||||
x.parent = element
|
||||
x.action = action
|
||||
self.fringe.append(x)
|
||||
|
||||
def __state_eq_goal_action(self, current_element: Node):
|
||||
@ -107,7 +107,5 @@ class BFSSearcher:
|
||||
self.path.append(action)
|
||||
else:
|
||||
self.path.append(current_element.action)
|
||||
current_element = current_element.parent
|
||||
|
||||
self.path.reverse()
|
||||
return self.path
|
||||
return self.path.reverse()
|
||||
|
Loading…
Reference in New Issue
Block a user