From 3b6fc6a446993499345a461c36514e6985ea901e Mon Sep 17 00:00:00 2001 From: Anna Nowak Date: Tue, 23 Apr 2019 21:45:16 +0200 Subject: [PATCH] Naprawiono algorytm, dfs do poprawy --- DataModels/GC.py | 23 ++++++++--------------- DataModels/House.py | 6 +----- Traversal/DFS.py | 26 ++++++++++++++------------ main.py | 5 +++-- 4 files changed, 26 insertions(+), 34 deletions(-) diff --git a/DataModels/GC.py b/DataModels/GC.py index 66425bc..eb7abd2 100644 --- a/DataModels/GC.py +++ b/DataModels/GC.py @@ -33,21 +33,14 @@ class GC(Cell): item.return_trash(self) self.update_image() - def find_houses(self,enviromnent): - for row in enviromnent: - for cell in row: - goal = [] - if type(cell) == House and cell.container.is_full(): - goal.append([cell.x, cell.y]) - if len(self.moves) == 0: - x = self.x - y = self.y - else: - x,y = self.moves[-2] - avalible_moves = check_moves(enviromnent, x,y) - self.moves.extend(DFS(enviromnent,avalible_moves,[[x,y]],goal)) - self.moves.append("pick_garbage") - print(self.moves) + def find_houses(self,enviromnent, house_count): + x = self.x + y = self.y + result = [] + for home in range(house_count): + avalible_moves = check_moves(enviromnent, x,y) + [x,y],result = DFS(enviromnent,avalible_moves,[[x,y]]) + self.moves.extend(result) self.moves.reverse() diff --git a/DataModels/House.py b/DataModels/House.py index 6d9fe1c..316e768 100644 --- a/DataModels/House.py +++ b/DataModels/House.py @@ -4,13 +4,9 @@ from DataModels.Cell import Cell class House(Cell): def __init__(self, x, y, max_rubbish, yellow=0, green=0, blue=0): Cell.__init__(self, x, y, max_rubbish, yellow, green, blue) + self.unvisited = True def return_trash(self, collector): self.container.yellow, self.container.green, self.container.blue = collector.container.add( [self.container.yellow, self.container.green, self.container.blue]) self.update_image() - - def is_empty(self): - if(self.container.yellow == self.container.green == self.container.blue == 0): - return True - else: return False diff --git a/Traversal/DFS.py b/Traversal/DFS.py index 8b11192..450819e 100644 --- a/Traversal/DFS.py +++ b/Traversal/DFS.py @@ -1,8 +1,7 @@ from utilities import movement,check_moves from DataModels.House import House -def DFS(grid, avaliable_movement, gc_moveset,goal): - print("Moveset: "+str(gc_moveset)) - +from DataModels.Container import Container +def DFS(grid, available_movement, gc_moveset, depth=0): possible_goals = [] a = gc_moveset[-1][0] b = gc_moveset[-1][1] @@ -10,26 +9,29 @@ def DFS(grid, avaliable_movement, gc_moveset,goal): possible_goals.append([a-1,b]) possible_goals.append([a,b+1]) possible_goals.append([a,b-1]) - house_in_area = False for location in possible_goals: try: - if(type(grid[location[0]][location[1]]) == House): + if(type(grid[location[0]][location[1]]) == House and grid[location[0]][location[1]].container.is_full + and grid[location[0]][location[1]].unvisited): + grid[location[0]][location[1]].unvisited = False house_in_area = True + print("dupa") break except: continue - print(str(house_in_area) + " possible goals: " + str(possible_goals) + " gc_moveset: " + str(gc_moveset[-1])) - - if(house_in_area or len(avaliable_movement) == 0): + if(house_in_area or len(available_movement) == 0): print("Do zwrocenia: ",gc_moveset) - return gc_moveset + xy = gc_moveset[-1] + print(available_movement) + gc_moveset.append("pick_garbage") + return (xy, gc_moveset) x,y = gc_moveset[-1] - for direction in avaliable_movement: + for direction in available_movement: x_next, y_next = movement(grid,x,y)[0][direction] - avaliable_movement_next = check_moves(grid, x_next,y_next,direction) + available_movement_next = check_moves(grid, x_next,y_next,direction) gc_moveset_next = gc_moveset.copy() gc_moveset_next.append([x_next,y_next]) - return DFS(grid, avaliable_movement_next, gc_moveset_next,goal) + return DFS(grid, available_movement_next, gc_moveset_next) diff --git a/main.py b/main.py index 1c8313c..ed413ef 100755 --- a/main.py +++ b/main.py @@ -12,7 +12,7 @@ from DataModels.Road import Road from DataModels.GC import GC pygame_sprites = pygame.sprite.Group() - +house_count=0 FPS_CLOCK = pygame.time.Clock() GAME_WINDOW = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32) @@ -50,6 +50,7 @@ for y in map.readlines(): elif x is 'H': map_objects[x_coord][y_coord] = generate(x)( x_coord, y_coord, HOUSE_CAPACITY, yellow, green, blue) + house_count+=1 elif x is 'B': map_objects[x_coord][y_coord] = generate( x)(x_coord, y_coord, 100, "Dump_Blue") @@ -90,7 +91,7 @@ while True: elif event.key == pygame.K_SPACE: gc.collect(map_objects) elif event.key == pygame.K_0: - gc.find_houses(map_objects) + gc.find_houses(map_objects,house_count) gc.make_actions_from_list(map_objects) pygame_sprites.update()