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)) explored.append((state.xpos, state.ypos, state.orientation))
for value in element : for value in element :
val = (value.xpos, value.ypos, value.orientation) 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) fringe.append(value)
return False return False

View File

@ -1,3 +1,5 @@
FIELDWIDTH = 50
class GarbageTank: class GarbageTank:
def __init__(self, volume_capacity, mass_capacity): def __init__(self, volume_capacity, mass_capacity):
self.vcapacity = volume_capacity #m^3 self.vcapacity = volume_capacity #m^3
@ -8,12 +10,27 @@ class Engine:
self.power = power #HP self.power = power #HP
class GarbageTruck: 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.dump_location = dump_location
self.tank = GarbageTank(15, 18000) self.tank = GarbageTank(15, 18000)
self.engine = Engine(400) self.engine = Engine(400)
self.fuel = fuel_capacity self.fuel = fuel_capacity
self.xpos = xpos self.rect = rect
self.ypos = ypos
self.orientation = orientation 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 bfs import bfs
from state import State from state import State
import time import time
from garbage_truck import GarbageTruck
pygame.init() pygame.init()
WIDTH, HEIGHT = 800, 800 WIDTH, HEIGHT = 800, 800
@ -19,6 +20,8 @@ SAND = pygame.transform.scale(SAND_IMG, (50, 50))
COBBLE_IMG = pygame.image.load("cobble.jpeg") COBBLE_IMG = pygame.image.load("cobble.jpeg")
COBBLE = pygame.transform.scale(COBBLE_IMG, (50, 50)) COBBLE = pygame.transform.scale(COBBLE_IMG, (50, 50))
FPS = 10 FPS = 10
FIELDCOUNT = 16
FIELDWIDTH = 50
class Agent: class Agent:
def __init__(self, rect, direction): def __init__(self, rect, direction):
@ -59,7 +62,7 @@ def draw_window(agent, fields):
def main(): def main():
clock = pygame.time.Clock() clock = pygame.time.Clock()
run = True 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() fields = randomize_map()
while run: while run:
clock.tick(FPS) clock.tick(FPS)
@ -68,23 +71,16 @@ def main():
run = False run = False
#keys_pressed = pygame.key.get_pressed() #keys_pressed = pygame.key.get_pressed()
draw_window(agent, fields) 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: for interm in steps:
if interm.action == 'LEFT': if interm.action == 'LEFT':
agent.direction = (agent.direction - 1) % 4 agent.turn_left()
if interm.action == 'RIGHT': if interm.action == 'RIGHT':
agent.direction = (agent.direction + 1) % 4 agent.turn_right()
if interm.action == 'FORWARD': if interm.action == 'FORWARD':
if agent.direction == 0: agent.forward()
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
draw_window(agent, fields) draw_window(agent, fields)
time.sleep(1) time.sleep(0.5)
while True: while True:
pass pass

13
succ.py
View File

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