Merge pull request 'ruch_traktora_po_bfs' (#14) from ruch_traktora_po_bfs into snake_move
Reviewed-on: #14
This commit is contained in:
commit
066894d85c
8
App.py
8
App.py
@ -21,7 +21,7 @@ pole.draw_grid() #musi byc tutaj wywołane ponieważ inicjalizuje sloty do slown
|
|||||||
ui=Ui.Ui(screen)
|
ui=Ui.Ui(screen)
|
||||||
#Tractor creation
|
#Tractor creation
|
||||||
traktor_slot = pole.get_slot_from_cord((0, 0))
|
traktor_slot = pole.get_slot_from_cord((0, 0))
|
||||||
traktor = Tractor.Tractor(traktor_slot, screen, Osprzet.plug,clock)
|
traktor = Tractor.Tractor(traktor_slot, screen, Osprzet.opryskiwacz,clock)
|
||||||
|
|
||||||
|
|
||||||
def init_demo(): #Demo purpose
|
def init_demo(): #Demo purpose
|
||||||
@ -37,8 +37,12 @@ def init_demo(): #Demo purpose
|
|||||||
ui.render_text_to_console(string_to_print="Przejazd inicjalizujacy- traktor sprawdza poziom nawodnienia")
|
ui.render_text_to_console(string_to_print="Przejazd inicjalizujacy- traktor sprawdza poziom nawodnienia")
|
||||||
traktor.initial_move(pole)
|
traktor.initial_move(pole)
|
||||||
traktor.reset_pos(pole)
|
traktor.reset_pos(pole)
|
||||||
|
bfsRoot = Tractor.BFS({"x": 0, "y": 0, "direction": "E", "hydradeIndex": traktor.slot_hydrate_dict})
|
||||||
|
bfsRoot.reverse()
|
||||||
|
# ui.render_text_to_console(string_to_print="traktor porusza się ścieżką bfs")
|
||||||
|
traktor.move_by_root(bfsRoot, pole, [traktor.irrigateSlot])
|
||||||
start_flag=False
|
start_flag=False
|
||||||
demo_move()
|
# demo_move()
|
||||||
old_info=get_info(old_info)
|
old_info=get_info(old_info)
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
|
4
Pole.py
4
Pole.py
@ -55,8 +55,8 @@ class Pole:
|
|||||||
def check_collision(self,mouse_x,mouse_y):
|
def check_collision(self,mouse_x,mouse_y):
|
||||||
mouse_x=math.floor(mouse_x/dCon.CUBE_SIZE)
|
mouse_x=math.floor(mouse_x/dCon.CUBE_SIZE)
|
||||||
mouse_y=math.floor(mouse_y/dCon.CUBE_SIZE)
|
mouse_y=math.floor(mouse_y/dCon.CUBE_SIZE)
|
||||||
if(mouse_x<20):
|
if(mouse_x<dCon.NUM_X):
|
||||||
if(mouse_y<12):
|
if(mouse_y<dCon.NUM_Y):
|
||||||
collided=self.get_slot_from_cord((mouse_x,mouse_y))
|
collided=self.get_slot_from_cord((mouse_x,mouse_y))
|
||||||
return collided.print_status()
|
return collided.print_status()
|
||||||
return ""
|
return ""
|
2
Slot.py
2
Slot.py
@ -51,4 +51,6 @@ class Slot:
|
|||||||
|
|
||||||
def print_status(self):
|
def print_status(self):
|
||||||
return f"wspolrzedne: (X:{self.x_axis} Y:{self.y_axis}) "+self.plant.report_status()
|
return f"wspolrzedne: (X:{self.x_axis} Y:{self.y_axis}) "+self.plant.report_status()
|
||||||
|
def irrigatePlant(self):
|
||||||
|
self.plant.stan.nawodnienie = 100
|
||||||
|
|
34
Tractor.py
34
Tractor.py
@ -72,7 +72,12 @@ def BFS(istate):
|
|||||||
elem = fringe.pop(0)
|
elem = fringe.pop(0)
|
||||||
|
|
||||||
if goalTest(elem.state["hydradeIndex"]):
|
if goalTest(elem.state["hydradeIndex"]):
|
||||||
return elem #TODO ciąg akcji zbudowany z wykorzystaniem pól parent i action
|
x = elem
|
||||||
|
tab = []
|
||||||
|
while x.parent != None:
|
||||||
|
tab.append(x.action)
|
||||||
|
x = x.parent
|
||||||
|
return tab
|
||||||
|
|
||||||
explored.append(elem)
|
explored.append(elem)
|
||||||
|
|
||||||
@ -181,23 +186,40 @@ class Tractor:
|
|||||||
self.do_move_if_valid(pole,(0,0))
|
self.do_move_if_valid(pole,(0,0))
|
||||||
|
|
||||||
def initial_move(self,pole):
|
def initial_move(self,pole):
|
||||||
for y in range (0,12):
|
for y in range (0,dCon.NUM_Y):
|
||||||
if(y%2==0):
|
if(y%2==0):
|
||||||
for x in range(0,20):
|
for x in range(0,dCon.NUM_X):
|
||||||
self.snake_move(pole,x,y)
|
self.snake_move(pole,x,y)
|
||||||
else:
|
else:
|
||||||
for x in range(20,0,-1):
|
for x in range(dCon.NUM_X,-1,-1):
|
||||||
self.snake_move(pole,x,y)
|
self.snake_move(pole,x,y)
|
||||||
|
|
||||||
|
|
||||||
def snake_move(self,pole,x,y):
|
def snake_move(self,pole,x,y):
|
||||||
next_slot_coordinates=(x,y)
|
next_slot_coordinates=(x,y)
|
||||||
if(self.do_move_if_valid(pole,next_slot_coordinates)):
|
if(self.do_move_if_valid(pole,next_slot_coordinates)):
|
||||||
self.slot_hydrate_dict[(x,y)]= pole.get_slot_from_cord((x,y)).get_hydrate_stats() #Budowanie slownika slotow z poziomem nawodnienia dla traktorka
|
if pole.get_slot_from_cord((x,y)).get_hydrate_stats() < 60:
|
||||||
|
hydrateIndex = 0
|
||||||
|
else:
|
||||||
|
hydrateIndex = 1
|
||||||
|
self.slot_hydrate_dict[(x,y)]= hydrateIndex #Budowanie slownika slotow z poziomem nawodnienia dla traktorka
|
||||||
self.clock.tick(10)
|
self.clock.tick(10)
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
quit()
|
quit()
|
||||||
|
def move_by_root(self, root, pole, actions = None):
|
||||||
|
for move in root:
|
||||||
|
self.slot.redraw_image()
|
||||||
|
if move == 'forward':
|
||||||
|
self.move_forward(pole)
|
||||||
|
if move == 'right':
|
||||||
|
self.turn_right()
|
||||||
|
if move == 'left':
|
||||||
|
self.turn_left()
|
||||||
|
for a in actions:
|
||||||
|
a()
|
||||||
|
|
||||||
|
self.clock.tick(3)
|
||||||
|
|
||||||
#to tak zrobiłam już na później, może się przyda
|
#to tak zrobiłam już na później, może się przyda
|
||||||
def change_osprzet(self, new_osprzet):
|
def change_osprzet(self, new_osprzet):
|
||||||
@ -213,4 +235,6 @@ class Tractor:
|
|||||||
print("- Typ:", akcja.typ)
|
print("- Typ:", akcja.typ)
|
||||||
else:
|
else:
|
||||||
print("Brak akcji przypisanych do tego sprzętu.")
|
print("Brak akcji przypisanych do tego sprzętu.")
|
||||||
|
def irrigateSlot(self):
|
||||||
|
self.slot.irrigatePlant()
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
CUBE_SIZE = 64
|
CUBE_SIZE = 64
|
||||||
NUM_X = 20
|
NUM_X = 5
|
||||||
NUM_Y = 12
|
NUM_Y = 3
|
||||||
|
|
||||||
#returns true if tractor can move to specified slot
|
#returns true if tractor can move to specified slot
|
||||||
def isValidMove(x, y):
|
def isValidMove(x, y):
|
||||||
|
Loading…
Reference in New Issue
Block a user