bfs second try, adjacency matrix added, need to check if everything is correct

This commit is contained in:
trzmielewskiR 2022-04-07 21:37:51 +02:00
parent f41cf7e02b
commit 3044a71592
3 changed files with 75 additions and 17 deletions

View File

@ -24,4 +24,4 @@ class BreadthSearchAlgorithm:
return next_path
visited.append(node)
return
return "Path found"

30
data.txt Normal file
View File

@ -0,0 +1,30 @@
11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
11,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,11,
11,0,0,0,0,0,0,0,0,0,35,0,0,0,0,0,0,0,0,0,2684354599,35,17,25,0,24,29,33,7,11,
11,7,0,0,23,0,17,14,12,0,13,0,16,13,0,24,0,42,13,25,0,45,22,29,0,44,0,0,7,11,
11,7,0,0,15,0,45,37,17,0,0,0,24,12,0,45,0,44,24,14,0,0,0,0,0,37,0,23,7,11,
11,7,0,0,13,0,0,0,41,25,0,20,16,41,22,35,0,29,37,21,0,17,42,0,0,25,0,0,7,11,
11,7,0,26,27,28,29,0,21,43,0,29,0,0,0,0,0,0,0,0,0,0,43,0,536870952,0,2684354599,0,7,11,
11,7,0,14,0,24,21,0,15,33,0,21,0,43,21,33,0,45,30,31,16,0,12,0,0,0,0,0,7,11,
11,7,0,42,0,22,20,0,0,0,0,44,0,20,40,16,0,43,0,33,13,0,45,0,17,0,43,0,7,11,
11,7,0,25,0,13,0,0,15,24,0,0,0,41,0,29,0,15,0,0,44,0,37,0,21,0,24,0,7,11,
11,7,0,0,0,0,0,33,29,0,17,12,0,21,0,0,0,17,43,0,13,29,41,0,24,0,13,0,7,11,
11,7,0,15,18,45,0,13,41,0,24,16,0,23,0,13,41,22,16,0,20,32,17,0,29,25,35,0,7,11,
11,7,0,25,35,32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,21,0,0,0,0,0,0,0,7,11,
11,7,0,0,34,19,0,44,15,0,2684354596,2684354596,0,18,24,0,17,35,0,41,25,0,41,0,13,0,44,0,7,11,
11,7,24,0,0,0,0,0,0,0,0,0,0,42,20,0,16,0,0,0,24,0,21,0,18,0,25,0,7,9,
11,7,22,0,33,13,16,43,21,20,29,15,0,41,24,0,29,13,0,16,43,0,18,24,42,0,42,23,7,9,
11,7,21,0,21,20,0,0,0,0,41,44,0,42,15,0,0,0,0,0,0,0,0,0,0,0,0,0,7,9,
11,7,24,0,41,24,0,3221225510,1610612774,0,24,21,0,13,40,20,0,17,32,15,44,0,26,27,28,0,18,25,7,11,
11,7,18,0,44,12,0,2684354598,38,0,17,22,0,41,0,0,0,0,0,24,20,0,21,35,33,0,12,24,7,11,
11,7,13,0,0,0,0,0,0,0,0,0,0,12,15,21,0,13,35,18,41,37,43,39,15,0,22,18,7,11,
11,7,15,0,43,15,0,33,17,41,0,29,18,33,0,0,0,0,0,0,0,0,0,0,0,0,0,24,7,11,
11,7,13,0,25,0,0,0,0,39,21,42,44,14,0,18,45,0,13,14,0,29,0,42,13,0,25,35,7,11,
11,7,24,0,14,18,23,24,0,13,0,0,0,0,0,21,42,0,41,15,2684354599,21,0,15,0,0,0,24,7,11,
11,7,18,0,0,0,0,0,0,3221225508,0,21,15,0,41,29,0,0,0,0,43,14,18,42,0,14,0,18,7,11,
11,7,14,40,43,0,43,14,0,0,0,29,17,18,22,24,0,17,23,0,13,30,31,25,0,33,0,32,7,11,
11,7,14,0,44,0,12,0,0,24,14,0,0,0,0,0,0,0,15,0,0,0,0,0,0,0,0,15,7,11,
11,7,25,0,29,0,14,13,25,14,43,0,15,13,0,44,33,0,25,0,13,44,0,43,15,44,0,42,7,11,
11,7,23,0,0,0,0,0,0,0,0,0,32,45,0,35,25,0,0,0,0,0,0,32,0,0,0,18,7,11,
11,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,11,
11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11

38
main.py
View File

@ -1,11 +1,15 @@
import pygame as pg
import sys
from os import path
import SearchBfs
from map import *
# from agent import trashmaster
# from house import House
from sprites import *
from settings import *
from SearchBfs import *
class Game():
@ -24,8 +28,8 @@ class Game():
self.map_img = self.map.make_map()
self.map_rect = self.map_img.get_rect()
self.player_img = pg.image.load(path.join(img_folder,PLAYER_IMG)).convert_alpha()
self.player_img = pg.transform.scale(self.player_img, (PLAYER_WIDTH,PLAYER_HEIGHT) )
self.player_img = pg.image.load(path.join(img_folder, PLAYER_IMG)).convert_alpha()
self.player_img = pg.transform.scale(self.player_img, (PLAYER_WIDTH, PLAYER_HEIGHT))
self.wall_img = pg.image.load(path.join(img_folder, WALL_IMG)).convert_alpha()
self.wall_img = pg.transform.scale(self.wall_img, (TILESIZE, TILESIZE))
@ -45,7 +49,7 @@ class Game():
self.camera = Camera(self.map.width, self.map.height)
self.draw_debug = False
#self.screen.blit(self.map_img, self.camera.apply_rect(self.map_rect))
# self.screen.blit(self.map_img, self.camera.apply_rect(self.map_rect))
def run(self):
# game loop - set self.playing = False to end the game
@ -72,7 +76,6 @@ class Game():
for y in range(0, HEIGHT, TILESIZE):
pg.draw.line(self.screen, LIGHTGREY, (0, y), (WIDTH, y))
# def draw(self, drawable_object, pos):
# # pos => (x, y)
# # drawable object must have .image field inside class
@ -112,6 +115,7 @@ class Game():
# #self.screen.fill(pygame.Color(self.BACKGROUND_COLOR))
# self.screen.blit(self.map_img, (0,0))
# def main():
# game = WalleGame()
# game.update_window()
@ -140,8 +144,32 @@ class Game():
# pygame.quit()
# if __name__ == '__main__':
# main()
def graph():
with open('data.txt', 'r') as f:
matrix = [[int(x) for x in line.split(',') if x != '\n'] for line in f.readlines()]
adj = {}
for yi, yvalue in enumerate(matrix):
for xi, xvalue in enumerate(matrix):
if xi - 1 >= 0 and matrix[yi][xi - 1] == 0:
adj[(xi, yi)] = adj.get((xi, yi), []) + [(xi - 1, yi)]
if xi + 1 < len(matrix[yi]) and matrix[yi][xi + 1] == 0:
adj[(xi, yi)] = adj.get((xi, yi), []) + [(xi + 1, yi)]
if yi - 1 >= 0 and matrix[yi - 1][xi] == 0:
adj[(xi, yi)] = adj.get((xi, yi), []) + [(xi, yi - 1)]
if yi + 1 < len(matrix) and matrix[yi + 1][xi] == 0:
adj[(xi, yi)] = adj.get((xi, yi), []) + [(xi, yi + 1)]
l = sorted(list(adj.items()), key=lambda x: (x[0]))
print(*l, sep='\n')
return l
start_node = (0, 2)
target_node = (0, 2)
# create the game object
g = Game()
g.show_start_screen()
@ -149,4 +177,4 @@ while True:
g.new()
g.run()
g.show_go_screen()
SearchBfs.BreadthSearchAlgorithm(graph, start_node, target_node)