added constants, created agent methods

This commit is contained in:
Mateusz Szlachetka 2023-04-21 14:31:17 +02:00
parent fc588a4ad5
commit 15abf755b9
4 changed files with 38 additions and 24 deletions

2
bfs.py
View File

@ -17,6 +17,6 @@ def bfs(istate, goalx, goaly):
explored.append((state.xpos, state.ypos, state.orientation))
for value in element :
val = (value.xpos, value.ypos, value.orientation)
if val not in explored:
if val not in explored and value not in fringe:
fringe.append(value)
return False

View File

@ -1,3 +1,5 @@
FIELDWIDTH = 50
class GarbageTank:
def __init__(self, volume_capacity, mass_capacity):
self.vcapacity = volume_capacity #m^3
@ -8,12 +10,27 @@ class Engine:
self.power = power #HP
class GarbageTruck:
def __init__(self, dump_location, fuel_capacity, xpos, ypos, orientation):
def __init__(self, dump_location, fuel_capacity, rect, orientation):
self.dump_location = dump_location
self.tank = GarbageTank(15, 18000)
self.engine = Engine(400)
self.fuel = fuel_capacity
self.xpos = xpos
self.ypos = ypos
self.rect = rect
self.orientation = orientation
self.houses = [] #lista domów do odwiedzenia
self.houses = [] #lista domów do odwiedzenia
def turn_left(self):
self.orientation = (self.orientation - 1) % 4
def turn_right(self):
self.orientation = (self.orientation + 1) % 4
def forward(self):
if self.orientation == 0:
self.rect.x += FIELDWIDTH
elif self.orientation == 1:
self.rect.y += FIELDWIDTH
elif self.orientation == 2:
self.rect.x -= FIELDWIDTH
else:
self.rect.y -= FIELDWIDTH

22
main.py
View File

@ -3,6 +3,7 @@ import random
from bfs import bfs
from state import State
import time
from garbage_truck import GarbageTruck
pygame.init()
WIDTH, HEIGHT = 800, 800
@ -19,6 +20,8 @@ SAND = pygame.transform.scale(SAND_IMG, (50, 50))
COBBLE_IMG = pygame.image.load("cobble.jpeg")
COBBLE = pygame.transform.scale(COBBLE_IMG, (50, 50))
FPS = 10
FIELDCOUNT = 16
FIELDWIDTH = 50
class Agent:
def __init__(self, rect, direction):
@ -59,7 +62,7 @@ def draw_window(agent, fields):
def main():
clock = pygame.time.Clock()
run = True
agent = Agent(pygame.Rect(0, 0, 50, 50), 0) # tworzenie pola dla agenta
agent = GarbageTruck(0, 0, pygame.Rect(0, 0, 50, 50), 0) # tworzenie pola dla agenta
fields = randomize_map()
while run:
clock.tick(FPS)
@ -68,23 +71,16 @@ def main():
run = False
#keys_pressed = pygame.key.get_pressed()
draw_window(agent, fields)
steps = bfs(State(None, None, 0, 0, 'E'), 250, 700)
steps = bfs(State(None, None, 0, 0, 'E'), 450, 600)
for interm in steps:
if interm.action == 'LEFT':
agent.direction = (agent.direction - 1) % 4
agent.turn_left()
if interm.action == 'RIGHT':
agent.direction = (agent.direction + 1) % 4
agent.turn_right()
if interm.action == 'FORWARD':
if agent.direction == 0:
agent.rect.x += 50
elif agent.direction == 1:
agent.rect.y += 50
elif agent.direction == 2:
agent.rect.x -= 50
else:
agent.rect.y -= 50
agent.forward()
draw_window(agent, fields)
time.sleep(1)
time.sleep(0.5)
while True:
pass

13
succ.py
View File

@ -1,4 +1,5 @@
from state import State
FIELDWIDTH, FIELDCOUNT = 50, 16
def succ(st: State):
successors = []
@ -7,23 +8,23 @@ def succ(st: State):
successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'W'))
successors.append(State(st, 'RIGHT', st.xpos, st.ypos, 'E'))
if st.ypos > 0:
successors.append(State(st, 'FORWARD', st.xpos, st.ypos - 50, 'N'))
successors.append(State(st, 'FORWARD', st.xpos, st.ypos - FIELDWIDTH , 'N'))
if st.orientation == 'S':
successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'E'))
successors.append(State(st,'RIGHT', st.xpos, st.ypos, 'W'))
if st.ypos < 750:
successors.append(State(st, 'FORWARD', st.xpos, st.ypos + 50, 'S'))
if st.ypos < FIELDWIDTH * (FIELDCOUNT - 1):
successors.append(State(st, 'FORWARD', st.xpos, st.ypos + FIELDWIDTH , 'S'))
if st.orientation == 'W':
successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'S'))
successors.append(State(st,'RIGHT', st.xpos, st.ypos, 'N'))
if st.xpos > 0:
successors.append(State(st, 'FORWARD', st.xpos - 50, st.ypos, 'W'))
successors.append(State(st, 'FORWARD', st.xpos - FIELDWIDTH , st.ypos, 'W'))
if st.orientation == 'E':
successors.append(State(st, 'LEFT', st.xpos, st.ypos, 'N'))
successors.append(State(st, 'RIGHT', st.xpos, st.ypos, 'S'))
if st.xpos < 750:
successors.append(State(st, 'FORWARD', st.xpos + 50, st.ypos, 'E'))
if st.xpos < FIELDWIDTH * (FIELDCOUNT - 1):
successors.append(State(st, 'FORWARD', st.xpos + FIELDWIDTH , st.ypos, 'E'))
return successors