Dodano BFS
This commit is contained in:
parent
fa8cb46852
commit
5c50c54e46
@ -5,11 +5,10 @@ from DataModels.Dump import Dump
|
||||
from config import GRID_WIDTH, GRID_HEIGHT, DELAY
|
||||
from utilities import movement, check_moves
|
||||
from Traversal.DFS import DFS
|
||||
from Traversal.BFS import BFS
|
||||
import pygame
|
||||
|
||||
class GC(Cell):
|
||||
moves_made = 0
|
||||
|
||||
def __init__(self, x, y, max_rubbish, yellow=0, green=0, blue=0):
|
||||
Cell.__init__(self, x, y, max_rubbish, yellow, green, blue)
|
||||
self.moves = []
|
||||
@ -38,17 +37,24 @@ class GC(Cell):
|
||||
|
||||
def get_moves_made(self):
|
||||
return self.moves_made
|
||||
def find_houses(self,enviromnent, house_count,dump_count):
|
||||
|
||||
def find_houses(self,enviromnent, house_count,dump_count, mode):
|
||||
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]],"House")
|
||||
if mode == "DFS":
|
||||
[x,y],result = DFS(enviromnent,avalible_moves,[[x,y]],House)
|
||||
elif mode == "BFS":
|
||||
[x,y],result = BFS(enviromnent,avalible_moves,[[x,y]],House)
|
||||
self.moves.extend(result)
|
||||
for dump in range(dump_count):
|
||||
avalible_moves = check_moves(enviromnent, x,y)
|
||||
[x,y],result = DFS(enviromnent,avalible_moves,[[x,y]],"Dump")
|
||||
if mode == "DFS":
|
||||
[x,y],result = DFS(enviromnent,avalible_moves,[[x,y]],Dump)
|
||||
elif mode == "BFS":
|
||||
[x,y],result = BFS(enviromnent,avalible_moves,[[x,y]],Dump)
|
||||
self.moves.extend(result)
|
||||
self.moves.reverse()
|
||||
|
||||
|
52
Traversal/BFS.py
Normal file
52
Traversal/BFS.py
Normal file
@ -0,0 +1,52 @@
|
||||
from utilities import movement,check_moves
|
||||
from DataModels.House import House
|
||||
from DataModels.Dump import Dump
|
||||
from DataModels.Container import Container
|
||||
from config import GRID_WIDTH, GRID_HEIGHT
|
||||
from collections import deque
|
||||
|
||||
def BFS(grid, available_movement, gc_moveset, mode):
|
||||
queue = deque()
|
||||
visited_nodes=[]
|
||||
for x in range(GRID_WIDTH):
|
||||
visited_nodes.append([])
|
||||
for y in range(GRID_HEIGHT):
|
||||
visited_nodes[-1].append(0)
|
||||
visited_nodes[gc_moveset[-1][0]][gc_moveset[-1][1]] = 1
|
||||
queue.append([available_movement, gc_moveset, visited_nodes])
|
||||
|
||||
|
||||
while queue:
|
||||
possible_goals = []
|
||||
state = queue.popleft()
|
||||
avalible_movement_state = state[0]
|
||||
gc_moveset_state = state[1]
|
||||
visited_nodes_state = state[2]
|
||||
a = gc_moveset_state[-1][0]
|
||||
b = gc_moveset_state[-1][1]
|
||||
possible_goals.append([a+1,b])
|
||||
possible_goals.append([a-1,b])
|
||||
possible_goals.append([a,b+1])
|
||||
possible_goals.append([a,b-1])
|
||||
object_in_area = False
|
||||
for location in possible_goals:
|
||||
if GRID_WIDTH>location[0]>=0 and GRID_HEIGHT>location[1]>=0:
|
||||
cell = grid[location[0]][location[1]]
|
||||
if(type(cell) == mode and cell.unvisited):
|
||||
cell.unvisited = False
|
||||
object_in_area = True
|
||||
break
|
||||
|
||||
x,y = gc_moveset_state[-1]
|
||||
if(object_in_area):
|
||||
gc_moveset_state.append("pick_garbage")
|
||||
return ([x,y], gc_moveset_state)
|
||||
|
||||
for direction in avalible_movement_state:
|
||||
x_next, y_next = movement(grid,x,y)[0][direction]
|
||||
if visited_nodes_state[x_next][y_next]==0:
|
||||
available_movement_next = check_moves(grid, x_next,y_next,direction)
|
||||
gc_moveset_next = gc_moveset_state.copy()
|
||||
gc_moveset_next.append([x_next,y_next])
|
||||
visited_nodes_state[x_next][y_next]=1
|
||||
queue.append([available_movement_next, gc_moveset_next,visited_nodes_state])
|
@ -5,6 +5,7 @@ from DataModels.Container import Container
|
||||
from config import GRID_WIDTH, GRID_HEIGHT
|
||||
|
||||
def DFS(grid, available_movement, gc_moveset, mode,depth=0):
|
||||
|
||||
possible_goals = []
|
||||
a = gc_moveset[-1][0]
|
||||
b = gc_moveset[-1][1]
|
||||
@ -16,24 +17,18 @@ def DFS(grid, available_movement, gc_moveset, mode,depth=0):
|
||||
for location in possible_goals:
|
||||
if GRID_WIDTH>location[0]>=0 and GRID_HEIGHT>location[1]>=0:
|
||||
cell = grid[location[0]][location[1]]
|
||||
if mode == "House":
|
||||
if(type(cell) == House and cell.container.is_full and cell.unvisited):
|
||||
cell.unvisited = False
|
||||
object_in_area = True
|
||||
break
|
||||
elif mode == "Dump":
|
||||
if(type(cell) == Dump and cell.unvisited):
|
||||
cell.unvisited = False
|
||||
object_in_area = True
|
||||
break
|
||||
if(type(cell) == mode and cell.unvisited):
|
||||
cell.unvisited = False
|
||||
object_in_area = True
|
||||
break
|
||||
|
||||
x,y = gc_moveset[-1]
|
||||
if(object_in_area):
|
||||
xy = gc_moveset[-1]
|
||||
gc_moveset.append("pick_garbage")
|
||||
return (xy, gc_moveset)
|
||||
return ([x,y], gc_moveset)
|
||||
|
||||
if len(available_movement) == 0 or depth>30:
|
||||
return
|
||||
x,y = gc_moveset[-1]
|
||||
for direction in available_movement:
|
||||
x_next, y_next = movement(grid,x,y)[0][direction]
|
||||
available_movement_next = check_moves(grid, x_next,y_next,direction)
|
||||
|
4
main.py
4
main.py
@ -99,7 +99,9 @@ while True:
|
||||
elif event.key == pygame.K_SPACE:
|
||||
gc.collect(map_objects)
|
||||
elif event.key == pygame.K_0:
|
||||
gc.find_houses(map_objects,house_count,dump_count)
|
||||
gc.find_houses(map_objects,house_count,dump_count, "DFS")
|
||||
elif event.key == pygame.K_8:
|
||||
gc.find_houses(map_objects,house_count,dump_count, "BFS")
|
||||
|
||||
gc.make_actions_from_list(map_objects)
|
||||
pygame_sprites.update()
|
||||
|
Loading…
Reference in New Issue
Block a user