├── map.txt ├── main.py ├── Iterative-deepening-Search.py ├── Depth-Limited-Search.py ├── main_ef.py └── A_star-Search.py /map.txt: -------------------------------------------------------------------------------- 1 | ...........XX........................... 2 | ............X........................... 3 | ............X........................... 4 | ............X........................... 5 | ............X........................... 6 | ............X....X.X.X.................. 7 | ............X....X..XX.................. 8 | ............X..S.X.XX................... 9 | ............X....X...................... 10 | ............XXXXXXXXXXXX................ 11 | ........................................ 12 | ........................................ 13 | .....XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 14 | ........................................ 15 | ........................................ 16 | ................D....................... 17 | ........................................ 18 | ........................................ 19 | ........................................ 20 | ........................................ 21 | ........................................ 22 | ........................................ 23 | ........................................ 24 | ........................................ 25 | ........................................ 26 | ........................................ 27 | ........................................ 28 | ........................................ 29 | ........................................ 30 | ........................................ 31 | ........................................ 32 | ........................................ 33 | ........................................ 34 | ........................................ 35 | ........................................ 36 | ........................................ 37 | ........................................ 38 | ........................................ 39 | ........................................ 40 | ........................................ 41 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | import sys 3 | from collections import deque 4 | 5 | # Constants for the game 6 | TILE_SIZE = 20 7 | WINDOW_WIDTH = 800 8 | WINDOW_HEIGHT = 800 9 | 10 | # Colors 11 | WHITE = (255, 255, 255) 12 | BLACK = (0, 0, 0) 13 | GREEN = (0, 255, 0) 14 | RED = (255, 0, 0) 15 | 16 | def read_map(filename): 17 | with open(filename, 'r') as f: 18 | return [list(line.strip()) for line in f.readlines()] 19 | 20 | def find_neighbors(map, node): 21 | directions = [(0, -1), (0, 1), (-1, 0), (1, 0)] 22 | neighbors = [] 23 | for dx, dy in directions: 24 | nx, ny = node[0] + dx, node[1] + dy 25 | if 0 <= nx < len(map[0]) and 0 <= ny < len(map) and map[ny][nx] in ('.', 'D'): 26 | neighbors.append((nx, ny)) 27 | return neighbors 28 | 29 | def bfs(map, start, destination): 30 | queue = deque([start]) 31 | visited = {start: None} 32 | 33 | while queue: 34 | current = queue.popleft() 35 | if current == destination: 36 | break 37 | for neighbor in find_neighbors(map, current): 38 | if neighbor not in visited: 39 | queue.append(neighbor) 40 | visited[neighbor] = current 41 | 42 | path = [] 43 | while current is not None: 44 | path.append(current) 45 | current = visited[current] 46 | path.reverse() 47 | return path 48 | 49 | def main(): 50 | pygame.init() 51 | screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) 52 | pygame.display.set_caption('Map Explorer') 53 | 54 | map = read_map('map.txt') 55 | start = None 56 | destination = None 57 | 58 | for y, row in enumerate(map): 59 | for x, tile in enumerate(row): 60 | if tile == 'S': 61 | start = (x, y) 62 | elif tile == 'D': 63 | destination = (x, y) 64 | 65 | if start is None or destination is None: 66 | print("Map must have a start (S) and a destination (D)") 67 | sys.exit(1) 68 | 69 | path = bfs(map, start, destination) 70 | 71 | clock = pygame.time.Clock() 72 | index = 0 73 | 74 | while True: 75 | for event in pygame.event.get(): 76 | if event.type == pygame.QUIT: 77 | pygame.quit() 78 | sys.exit() 79 | 80 | screen.fill(WHITE) 81 | 82 | # Draw the map 83 | for y, row in enumerate(map): 84 | for x, tile in enumerate(row): 85 | rect = pygame.Rect(x*TILE_SIZE, y*TILE_SIZE, TILE_SIZE, TILE_SIZE) 86 | if tile == 'X': 87 | pygame.draw.rect(screen, BLACK, rect) 88 | elif tile == '.' or tile == 'S' or tile == 'D': 89 | pygame.draw.rect(screen, WHITE, rect, 1) 90 | 91 | # Draw the path 92 | for point in path: 93 | pygame.draw.rect(screen, GREEN, (point[0]*TILE_SIZE, point[1]*TILE_SIZE, TILE_SIZE, TILE_SIZE)) 94 | 95 | # Draw the character 96 | if index < len(path): 97 | char_pos = path[index] 98 | pygame.draw.rect(screen, RED, (char_pos[0]*TILE_SIZE, char_pos[1]*TILE_SIZE, TILE_SIZE, TILE_SIZE)) 99 | index += 1 100 | 101 | pygame.display.flip() 102 | clock.tick(5) # Move the character at 5 tiles per second 103 | 104 | if __name__ == '__main__': 105 | main() 106 | -------------------------------------------------------------------------------- /Iterative-deepening-Search.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | import sys 3 | 4 | # Constants for the game 5 | TILE_SIZE = 10 6 | WINDOW_WIDTH = 400 7 | WINDOW_HEIGHT = 400 8 | 9 | # Colors 10 | WHITE = (255, 255, 255) 11 | BLACK = (0, 0, 0) 12 | GREEN = (0, 255, 0) 13 | RED = (255, 0, 0) 14 | BLUE = (0, 0, 255) # Color for IDS exploration 15 | 16 | def read_map(filename): 17 | with open(filename, 'r') as f: 18 | return [list(line.strip()) for line in f.readlines()] 19 | 20 | def find_neighbors(map, node): 21 | directions = [(0, -1), (0, 1), (-1, 0), (1, 0)] 22 | neighbors = [] 23 | for dx, dy in directions: 24 | nx, ny = node[0] + dx, node[1] + dy 25 | if 0 <= nx < len(map[0]) and 0 <= ny < len(map) and map[ny][nx] in ('.', 'D'): 26 | neighbors.append((nx, ny)) 27 | return neighbors 28 | 29 | def dfs_limit(map, start, destination, depth_limit, explored): 30 | if start == destination: 31 | return [start] 32 | if depth_limit <= 0: 33 | return None 34 | 35 | explored.add(start) 36 | for neighbor in find_neighbors(map, start): 37 | if neighbor not in explored: 38 | path = dfs_limit(map, neighbor, destination, depth_limit - 1, explored) 39 | if path is not None: 40 | return [start] + path 41 | return None 42 | 43 | def iterative_deepening_search(map, start, destination): 44 | for depth_limit in range(1, len(map) * len(map[0])): 45 | explored = set() 46 | path = dfs_limit(map, start, destination, depth_limit, explored) 47 | if path is not None: 48 | return path 49 | 50 | def draw_map(screen, map, explored=None, path=None): 51 | explored = explored or set() 52 | path = path or [] 53 | for y, row in enumerate(map): 54 | for x, tile in enumerate(row): 55 | rect = pygame.Rect(x*TILE_SIZE, y*TILE_SIZE, TILE_SIZE, TILE_SIZE) 56 | if tile == 'X': 57 | pygame.draw.rect(screen, BLACK, rect) 58 | elif (x, y) in explored: 59 | pygame.draw.rect(screen, BLUE, rect) # Explored tiles 60 | elif (x, y) in path: 61 | pygame.draw.rect(screen, RED, rect) # Path tiles 62 | else: 63 | pygame.draw.rect(screen, WHITE, rect, 1) 64 | 65 | def main(): 66 | pygame.init() 67 | screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) 68 | pygame.display.set_caption('Map Explorer') 69 | 70 | map = read_map('map.txt') 71 | start = None 72 | destination = None 73 | 74 | for y, row in enumerate(map): 75 | for x, tile in enumerate(row): 76 | if tile == 'S': 77 | start = (x, y) 78 | elif tile == 'D': 79 | destination = (x, y) 80 | 81 | if start is None or destination is None: 82 | print("Map must have a start (S) and a destination (D)") 83 | sys.exit(1) 84 | 85 | path = iterative_deepening_search(map, start, destination) 86 | 87 | if path is None: 88 | print("No path found.") 89 | sys.exit(1) 90 | 91 | explored = set(path[:-1]) 92 | 93 | clock = pygame.time.Clock() 94 | 95 | running = True 96 | while running: 97 | for event in pygame.event.get(): 98 | if event.type == pygame.QUIT: 99 | running = False 100 | 101 | draw_map(screen, map, explored, path) 102 | pygame.display.flip() 103 | clock.tick(60) # Refresh rate 104 | 105 | pygame.quit() 106 | sys.exit() 107 | 108 | if __name__ == '__main__': 109 | main() 110 | -------------------------------------------------------------------------------- /Depth-Limited-Search.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | import sys 3 | 4 | # Constants for the game 5 | TILE_SIZE = 10 6 | WINDOW_WIDTH = 400 7 | WINDOW_HEIGHT = 400 8 | 9 | # Colors 10 | WHITE = (255, 255, 255) 11 | BLACK = (0, 0, 0) 12 | GREEN = (0, 255, 0) 13 | RED = (255, 0, 0) 14 | BLUE = (0, 0, 255) # Color for DLS exploration 15 | 16 | def read_map(filename): 17 | with open(filename, 'r') as f: 18 | return [list(line.strip()) for line in f.readlines()] 19 | 20 | def find_neighbors(map, node): 21 | directions = [(0, -1), (0, 1), (-1, 0), (1, 0)] 22 | neighbors = [] 23 | for dx, dy in directions: 24 | nx, ny = node[0] + dx, node[1] + dy 25 | if 0 <= nx < len(map[0]) and 0 <= ny < len(map) and map[ny][nx] in ('.', 'D'): 26 | neighbors.append((nx, ny)) 27 | return neighbors 28 | 29 | def dls(map, start, destination, limit, explored, on_explore): 30 | if start == destination: 31 | return [start] 32 | 33 | if limit <= 0: 34 | return None 35 | 36 | explored.add(start) 37 | on_explore(start) 38 | 39 | for neighbor in find_neighbors(map, start): 40 | if neighbor not in explored: 41 | result = dls(map, neighbor, destination, limit - 1, explored, on_explore) 42 | if result is not None: 43 | return [start] + result 44 | 45 | return None 46 | 47 | def draw_map(screen, map, explored=None): 48 | explored = explored or set() 49 | for y, row in enumerate(map): 50 | for x, tile in enumerate(row): 51 | rect = pygame.Rect(x*TILE_SIZE, y*TILE_SIZE, TILE_SIZE, TILE_SIZE) 52 | if tile == 'X': 53 | pygame.draw.rect(screen, BLACK, rect) 54 | elif (x, y) in explored: 55 | pygame.draw.rect(screen, BLUE, rect) # Explored tiles 56 | else: 57 | pygame.draw.rect(screen, WHITE, rect, 1) 58 | 59 | def main(): 60 | pygame.init() 61 | screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) 62 | pygame.display.set_caption('Map Explorer') 63 | 64 | map = read_map('map.txt') 65 | start = None 66 | destination = None 67 | 68 | for y, row in enumerate(map): 69 | for x, tile in enumerate(row): 70 | if tile == 'S': 71 | start = (x, y) 72 | elif tile == 'D': 73 | destination = (x, y) 74 | 75 | if start is None or destination is None: 76 | print("Map must have a start (S) and a destination (D)") 77 | sys.exit(1) 78 | 79 | explored = set() 80 | 81 | def on_explore(current): 82 | nonlocal explored 83 | explored.add(current) 84 | draw_map(screen, map, explored) 85 | pygame.display.flip() 86 | pygame.time.delay(100) # Delay to visualize DLS exploration 87 | 88 | limit = 10 # Depth limit 89 | path = dls(map, start, destination, limit, explored, on_explore) 90 | 91 | clock = pygame.time.Clock() 92 | 93 | running = True 94 | while running: 95 | for event in pygame.event.get(): 96 | if event.type == pygame.QUIT: 97 | running = False 98 | 99 | # Draw the final path 100 | draw_map(screen, map, explored) 101 | if path is not None: 102 | for point in path: 103 | pygame.draw.rect(screen, RED, (point[0]*TILE_SIZE, point[1]*TILE_SIZE, TILE_SIZE, TILE_SIZE)) 104 | 105 | pygame.display.flip() 106 | clock.tick(60) # Refresh rate 107 | 108 | pygame.quit() 109 | sys.exit() 110 | 111 | if __name__ == '__main__': 112 | main() 113 | -------------------------------------------------------------------------------- /main_ef.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | import sys 3 | from collections import deque 4 | 5 | # Constants for the game 6 | TILE_SIZE = 10 7 | WINDOW_WIDTH = 400 8 | WINDOW_HEIGHT = 400 9 | 10 | # Colors 11 | WHITE = (255, 255, 255) 12 | BLACK = (0, 0, 0) 13 | GREEN = (0, 255, 0) 14 | RED = (255, 0, 0) 15 | BLUE = (0, 0, 255) # Color for BFS exploration 16 | 17 | def read_map(filename): 18 | with open(filename, 'r') as f: 19 | return [list(line.strip()) for line in f.readlines()] 20 | 21 | def find_neighbors(map, node): 22 | directions = [(0, -1), (0, 1), (-1, 0), (1, 0)] 23 | neighbors = [] 24 | for dx, dy in directions: 25 | nx, ny = node[0] + dx, node[1] + dy 26 | if 0 <= nx < len(map[0]) and 0 <= ny < len(map) and map[ny][nx] in ('.', 'D'): 27 | neighbors.append((nx, ny)) 28 | return neighbors 29 | 30 | def bfs(map, start, destination, on_explore): 31 | queue = deque([start]) 32 | visited = {start: None} 33 | 34 | while queue: 35 | current = queue.popleft() 36 | on_explore(current, list(queue)) # Callback for visualization 37 | if current == destination: 38 | break 39 | for neighbor in find_neighbors(map, current): 40 | if neighbor not in visited: 41 | queue.append(neighbor) 42 | visited[neighbor] = current 43 | 44 | path = [] 45 | while current is not None: 46 | path.append(current) 47 | current = visited[current] 48 | path.reverse() 49 | return path 50 | 51 | def draw_map(screen, map, explored=None, frontier=None): 52 | explored = explored or set() 53 | frontier = frontier or [] 54 | for y, row in enumerate(map): 55 | for x, tile in enumerate(row): 56 | rect = pygame.Rect(x*TILE_SIZE, y*TILE_SIZE, TILE_SIZE, TILE_SIZE) 57 | if tile == 'X': 58 | pygame.draw.rect(screen, BLACK, rect) 59 | elif (x, y) in explored: 60 | pygame.draw.rect(screen, BLUE, rect) # Explored tiles 61 | elif (x, y) in frontier: 62 | pygame.draw.rect(screen, GREEN, rect) # Frontier tiles 63 | else: 64 | pygame.draw.rect(screen, WHITE, rect, 1) 65 | 66 | def main(): 67 | pygame.init() 68 | screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) 69 | pygame.display.set_caption('Map Explorer') 70 | 71 | map = read_map('map.txt') 72 | start = None 73 | destination = None 74 | 75 | for y, row in enumerate(map): 76 | for x, tile in enumerate(row): 77 | if tile == 'S': 78 | start = (x, y) 79 | elif tile == 'D': 80 | destination = (x, y) 81 | 82 | if start is None or destination is None: 83 | print("Map must have a start (S) and a destination (D)") 84 | sys.exit(1) 85 | 86 | explored = set() 87 | def on_explore(current, frontier): 88 | nonlocal explored 89 | explored.add(current) 90 | draw_map(screen, map, explored, frontier) 91 | pygame.display.flip() 92 | pygame.time.delay(100) # Delay to visualize BFS exploration 93 | 94 | path = bfs(map, start, destination, on_explore) 95 | 96 | clock = pygame.time.Clock() 97 | 98 | running = True 99 | while running: 100 | for event in pygame.event.get(): 101 | if event.type == pygame.QUIT: 102 | running = False 103 | 104 | # Draw the final path 105 | draw_map(screen, map, explored) 106 | for point in path: 107 | pygame.draw.rect(screen, RED, (point[0]*TILE_SIZE, point[1]*TILE_SIZE, TILE_SIZE, TILE_SIZE)) 108 | 109 | pygame.display.flip() 110 | clock.tick(60) # Refresh rate 111 | 112 | pygame.quit() 113 | sys.exit() 114 | 115 | if __name__ == '__main__': 116 | main() 117 | -------------------------------------------------------------------------------- /A_star-Search.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | import sys 3 | from collections import deque 4 | 5 | # Constants for the game 6 | TILE_SIZE = 10 7 | WINDOW_WIDTH = 400 8 | WINDOW_HEIGHT = 400 9 | 10 | # Colors 11 | WHITE = (255, 255, 255) 12 | BLACK = (0, 0, 0) 13 | GREEN = (0, 255, 0) 14 | RED = (255, 0, 0) 15 | BLUE = (0, 0, 255) # Color for BFS exploration 16 | 17 | def read_map(filename): 18 | with open(filename, 'r') as f: 19 | return [list(line.strip()) for line in f.readlines()] 20 | 21 | def find_neighbors(map, node): 22 | directions = [(0, -1), (0, 1), (-1, 0), (1, 0)] 23 | neighbors = [] 24 | for dx, dy in directions: 25 | nx, ny = node[0] + dx, node[1] + dy 26 | if 0 <= nx < len(map[0]) and 0 <= ny < len(map) and map[ny][nx] in ('.', 'D'): 27 | neighbors.append((nx, ny)) 28 | return neighbors 29 | 30 | def heuristic(a, b): 31 | return abs(a[0] - b[0]) + abs(a[1] - b[1]) 32 | 33 | def a_star(map, start, destination, on_explore): 34 | open_set = {start} 35 | came_from = {} 36 | g_score = {start: 0} 37 | f_score = {start: heuristic(start, destination)} 38 | 39 | while open_set: 40 | current = min(open_set, key=lambda x: f_score[x]) 41 | on_explore(current, open_set) # Callback for visualization 42 | 43 | if current == destination: 44 | break 45 | 46 | open_set.remove(current) 47 | 48 | for neighbor in find_neighbors(map, current): 49 | tentative_g_score = g_score[current] + 1 50 | if neighbor not in g_score or tentative_g_score < g_score[neighbor]: 51 | came_from[neighbor] = current 52 | g_score[neighbor] = tentative_g_score 53 | f_score[neighbor] = tentative_g_score + heuristic(neighbor, destination) 54 | if neighbor not in open_set: 55 | open_set.add(neighbor) 56 | 57 | path = [] 58 | while current != start: 59 | path.append(current) 60 | current = came_from[current] 61 | path.append(start) 62 | path.reverse() 63 | return path 64 | 65 | def draw_map(screen, map, explored=None, frontier=None): 66 | explored = explored or set() 67 | frontier = frontier or [] 68 | for y, row in enumerate(map): 69 | for x, tile in enumerate(row): 70 | rect = pygame.Rect(x*TILE_SIZE, y*TILE_SIZE, TILE_SIZE, TILE_SIZE) 71 | if tile == 'X': 72 | pygame.draw.rect(screen, BLACK, rect) 73 | elif (x, y) in explored: 74 | pygame.draw.rect(screen, BLUE, rect) # Explored tiles 75 | elif (x, y) in frontier: 76 | pygame.draw.rect(screen, GREEN, rect) # Frontier tiles 77 | else: 78 | pygame.draw.rect(screen, WHITE, rect, 1) 79 | 80 | def main(): 81 | pygame.init() 82 | screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) 83 | pygame.display.set_caption('Map Explorer') 84 | 85 | map = read_map('map.txt') 86 | start = None 87 | destination = None 88 | 89 | for y, row in enumerate(map): 90 | for x, tile in enumerate(row): 91 | if tile == 'S': 92 | start = (x, y) 93 | elif tile == 'D': 94 | destination = (x, y) 95 | 96 | if start is None or destination is None: 97 | print("Map must have a start (S) and a destination (D)") 98 | sys.exit(1) 99 | 100 | explored = set() 101 | def on_explore(current, frontier): 102 | nonlocal explored 103 | explored.add(current) 104 | draw_map(screen, map, explored, frontier) 105 | pygame.display.flip() 106 | pygame.time.delay(100) # Delay to visualize A* exploration 107 | 108 | path = a_star(map, start, destination, on_explore) 109 | 110 | clock = pygame.time.Clock() 111 | 112 | running = True 113 | while running: 114 | for event in pygame.event.get(): 115 | if event.type == pygame.QUIT: 116 | running = False 117 | 118 | # Draw the final path 119 | draw_map(screen, map, explored) 120 | for point in path: 121 | pygame.draw.rect(screen, RED, (point[0]*TILE_SIZE, point[1]*TILE_SIZE, TILE_SIZE, TILE_SIZE)) 122 | 123 | pygame.display.flip() 124 | clock.tick(60) # Refresh rate 125 | 126 | pygame.quit() 127 | sys.exit() 128 | 129 | if __name__ == '__main__': 130 | main() 131 | --------------------------------------------------------------------------------