├── .github └── FUNDING.yml ├── README.md ├── astar.py ├── bfs.py └── djikstra.py /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: ['paypal.me/naschwindias'] 13 | 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # path-finding 2 | 3 | A* Path Finding 4 | Astar Path Finding Algorithm with diagonal movement and euclidean distance as the heuristics function. 5 | -------------------------------------------------------------------------------- /astar.py: -------------------------------------------------------------------------------- 1 | import pygame, sys, random, math 2 | from tkinter import messagebox, Tk 3 | 4 | 5 | 6 | size = (width, height) = 600, 600 7 | 8 | pygame.init() 9 | 10 | win = pygame.display.set_mode(size) 11 | 12 | clock = pygame.time.Clock() 13 | 14 | cols, rows = 50, 50 15 | 16 | 17 | grid = [] 18 | openSet, closeSet = [], [] 19 | path = [] 20 | 21 | w = width//cols 22 | h = height//rows 23 | 24 | class Spot: 25 | def __init__(self, i, j): 26 | self.x, self.y = i, j 27 | self.f, self.g, self.h = 0, 0, 0 28 | self.neighbors = [] 29 | self.prev = None 30 | self.wall = False 31 | # if random.randint(0, 100) < 20: 32 | # self.wall = True 33 | 34 | def show(self, win, col): 35 | if self.wall == True: 36 | col = (0, 0, 0) 37 | pygame.draw.rect(win, col, (self.x*w, self.y*h, w-1, h-1)) 38 | 39 | def add_neighbors(self, grid): 40 | if self.x < cols - 1: 41 | self.neighbors.append(grid[self.x+1][self.y]) 42 | if self.x > 0: 43 | self.neighbors.append(grid[self.x-1][self.y]) 44 | if self.y < rows - 1: 45 | self.neighbors.append(grid[self.x][self.y+1]) 46 | if self.y > 0: 47 | self.neighbors.append(grid[self.x][self.y-1]) 48 | #Add Diagonals 49 | if self.x < cols - 1 and self.y < rows - 1: 50 | self.neighbors.append(grid[self.x+1][self.y+1]) 51 | if self.x < cols - 1 and self.y > 0: 52 | self.neighbors.append(grid[self.x+1][self.y-1]) 53 | if self.x > 0 and self.y < rows - 1: 54 | self.neighbors.append(grid[self.x-1][self.y+1]) 55 | if self.x > 0 and self.y > 0: 56 | self.neighbors.append(grid[self.x-1][self.y-1]) 57 | 58 | 59 | def clickWall(pos, state): 60 | i = pos[0] // w 61 | j = pos[1] // h 62 | grid[i][j].wall = state 63 | 64 | def place(pos): 65 | i = pos[0] // w 66 | j = pos[1] // h 67 | return w, h 68 | 69 | def heuristics(a, b): 70 | return math.sqrt((a.x - b.x)**2 + abs(a.y - b.y)**2) 71 | 72 | 73 | for i in range(cols): 74 | arr = [] 75 | for j in range(rows): 76 | arr.append(Spot(i, j)) 77 | grid.append(arr) 78 | 79 | for i in range(cols): 80 | for j in range(rows): 81 | grid[i][j].add_neighbors(grid) 82 | 83 | start = grid[0][0] 84 | end = grid[cols - cols//2][rows - cols//4] 85 | 86 | openSet.append(start) 87 | 88 | 89 | 90 | def close(): 91 | pygame.quit() 92 | sys.exit() 93 | 94 | 95 | 96 | 97 | def main(): 98 | flag = False 99 | noflag = True 100 | startflag = False 101 | 102 | while True: 103 | for event in pygame.event.get(): 104 | if event.type == pygame.QUIT: 105 | close() 106 | if event.type == pygame.MOUSEBUTTONUP: 107 | if pygame.mouse.get_pressed(0): 108 | clickWall(pygame.mouse.get_pos(), True) 109 | if pygame.mouse.get_pressed(2): 110 | clickWall(pygame.mouse.get_pos(), False) 111 | if event.type == pygame.MOUSEMOTION: 112 | if pygame.mouse.get_pressed()[0]: 113 | clickWall(pygame.mouse.get_pos(), True) 114 | if event.type == pygame.KEYDOWN: 115 | if event.key == pygame.K_RETURN: 116 | startflag = True 117 | 118 | if startflag: 119 | if len(openSet) > 0: 120 | winner = 0 121 | for i in range(len(openSet)): 122 | if openSet[i].f < openSet[winner].f: 123 | winner = i 124 | 125 | current = openSet[winner] 126 | 127 | if current == end: 128 | temp = current 129 | while temp.prev: 130 | path.append(temp.prev) 131 | temp = temp.prev 132 | if not flag: 133 | flag = True 134 | print("Done") 135 | elif flag: 136 | continue 137 | 138 | if flag == False: 139 | openSet.remove(current) 140 | closeSet.append(current) 141 | 142 | for neighbor in current.neighbors: 143 | if neighbor in closeSet or neighbor.wall: 144 | continue 145 | tempG = current.g + 1 146 | 147 | newPath = False 148 | if neighbor in openSet: 149 | if tempG < neighbor.g: 150 | neighbor.g = tempG 151 | newPath = True 152 | else: 153 | neighbor.g = tempG 154 | newPath = True 155 | openSet.append(neighbor) 156 | 157 | if newPath: 158 | neighbor.h = heuristics(neighbor, end) 159 | neighbor.f = neighbor.g + neighbor.h 160 | neighbor.prev = current 161 | 162 | else: 163 | if noflag: 164 | Tk().wm_withdraw() 165 | messagebox.showinfo("No Solution", "There was no solution" ) 166 | noflag = False 167 | 168 | win.fill((0, 20, 20)) 169 | for i in range(cols): 170 | for j in range(rows): 171 | spot = grid[j][i] 172 | spot.show(win, (255, 255, 255)) 173 | if flag and spot in path: 174 | spot.show(win, (25, 120, 250)) 175 | elif spot in closeSet: 176 | spot.show(win, (255, 0, 0)) 177 | elif spot in openSet: 178 | spot.show(win, (0, 255, 0)) 179 | try: 180 | if spot == end: 181 | spot.show(win, (0, 120, 255)) 182 | except Exception: 183 | pass 184 | 185 | pygame.display.flip() 186 | 187 | 188 | 189 | main() 190 | -------------------------------------------------------------------------------- /bfs.py: -------------------------------------------------------------------------------- 1 | """Breadth First Search""" 2 | 3 | import pygame, sys, random, math 4 | from collections import deque 5 | from tkinter import messagebox, Tk 6 | 7 | size = (width, height) = 640, 480 8 | pygame.init() 9 | 10 | win = pygame.display.set_mode(size) 11 | pygame.display.set_caption('Breadth First Search') 12 | clock = pygame.time.Clock() 13 | 14 | cols, rows = 64, 48 15 | 16 | w = width//cols 17 | h = height//rows 18 | 19 | grid = [] 20 | queue, visited = deque(), [] 21 | path = [] 22 | 23 | class Spot: 24 | def __init__(self, i, j): 25 | self.x, self.y = i, j 26 | self.f, self.g, self.h = 0, 0, 0 27 | self.neighbors = [] 28 | self.prev = None 29 | self.wall = False 30 | self.visited = False 31 | # if random.randint(0, 100) < 20: 32 | # self.wall = True 33 | 34 | def show(self, win, col): 35 | if self.wall == True: 36 | col = (0, 0, 0) 37 | pygame.draw.rect(win, col, (self.x*w, self.y*h, w-1, h-1)) 38 | 39 | def add_neighbors(self, grid): 40 | if self.x < cols - 1: 41 | self.neighbors.append(grid[self.x+1][self.y]) 42 | if self.x > 0: 43 | self.neighbors.append(grid[self.x-1][self.y]) 44 | if self.y < rows - 1: 45 | self.neighbors.append(grid[self.x][self.y+1]) 46 | if self.y > 0: 47 | self.neighbors.append(grid[self.x][self.y-1]) 48 | #Add Diagonals 49 | # if self.x < cols - 1 and self.y < rows - 1: 50 | # self.neighbors.append(grid[self.x+1][self.y+1]) 51 | # if self.x < cols - 1 and self.y > 0: 52 | # self.neighbors.append(grid[self.x+1][self.y-1]) 53 | # if self.x > 0 and self.y < rows - 1: 54 | # self.neighbors.append(grid[self.x-1][self.y+1]) 55 | # if self.x > 0 and self.y > 0: 56 | # self.neighbors.append(grid[self.x-1][self.y-1]) 57 | 58 | 59 | def clickWall(pos, state): 60 | i = pos[0] // w 61 | j = pos[1] // h 62 | grid[i][j].wall = state 63 | 64 | def place(pos): 65 | i = pos[0] // w 66 | j = pos[1] // h 67 | return w, h 68 | 69 | for i in range(cols): 70 | arr = [] 71 | for j in range(rows): 72 | arr.append(Spot(i, j)) 73 | grid.append(arr) 74 | 75 | for i in range(cols): 76 | for j in range(rows): 77 | grid[i][j].add_neighbors(grid) 78 | 79 | 80 | start = grid[cols//2][rows//2] 81 | end = grid[cols-1][rows - cols//2] 82 | start.wall = False 83 | end.wall = False 84 | 85 | queue.append(start) 86 | start.visited = True 87 | 88 | def main(): 89 | flag = False 90 | noflag = True 91 | startflag = False 92 | 93 | while True: 94 | for event in pygame.event.get(): 95 | if event.type == pygame.QUIT: 96 | pygame.quit() 97 | sys.exit() 98 | if event.type == pygame.MOUSEBUTTONUP: 99 | if pygame.mouse.get_pressed(0): 100 | clickWall(pygame.mouse.get_pos(), True) 101 | if pygame.mouse.get_pressed(2): 102 | clickWall(pygame.mouse.get_pos(), False) 103 | if event.type == pygame.MOUSEMOTION: 104 | if pygame.mouse.get_pressed()[0]: 105 | clickWall(pygame.mouse.get_pos(), True) 106 | if event.type == pygame.KEYDOWN: 107 | if event.key == pygame.K_RETURN: 108 | startflag = True 109 | 110 | if startflag: 111 | if len(queue) > 0: 112 | current = queue.popleft() 113 | if current == end: 114 | temp = current 115 | while temp.prev: 116 | path.append(temp.prev) 117 | temp = temp.prev 118 | if not flag: 119 | flag = True 120 | print("Done") 121 | elif flag: 122 | continue 123 | if flag == False: 124 | for i in current.neighbors: 125 | if not i.visited and not i.wall: 126 | i.visited = True 127 | i.prev = current 128 | queue.append(i) 129 | else: 130 | if noflag and not flag: 131 | Tk().wm_withdraw() 132 | messagebox.showinfo("No Solution", "There was no solution" ) 133 | noflag = False 134 | else: 135 | continue 136 | 137 | 138 | win.fill((0, 20, 20)) 139 | for i in range(cols): 140 | for j in range(rows): 141 | spot = grid[i][j] 142 | spot.show(win, (255, 255, 255)) 143 | if spot in path: 144 | spot.show(win, (25, 120, 250)) 145 | elif spot.visited: 146 | spot.show(win, (255, 0, 0)) 147 | if spot in queue: 148 | spot.show(win, (0, 255, 0)) 149 | if spot == end: 150 | spot.show(win, (0, 120, 255)) 151 | 152 | 153 | pygame.display.flip() 154 | 155 | 156 | main() -------------------------------------------------------------------------------- /djikstra.py: -------------------------------------------------------------------------------- 1 | """Djikstra's Path Finding""" 2 | 3 | import pygame, sys, random, math 4 | from collections import deque 5 | from tkinter import messagebox, Tk 6 | 7 | size = (width, height) = 640, 480 8 | pygame.init() 9 | 10 | win = pygame.display.set_mode(size) 11 | pygame.display.set_caption("Dijktdtra's Path Finding") 12 | clock = pygame.time.Clock() 13 | 14 | cols, rows = 64, 48 15 | 16 | w = width//cols 17 | h = height//rows 18 | 19 | grid = [] 20 | queue, visited = deque(), [] 21 | path = [] 22 | 23 | class Spot: 24 | def __init__(self, i, j): 25 | self.x, self.y = i, j 26 | self.f, self.g, self.h = 0, 0, 0 27 | self.neighbors = [] 28 | self.prev = None 29 | self.wall = False 30 | self.visited = False 31 | if (i+j)%7 == 0: 32 | self.wall == True 33 | # if random.randint(0, 100) < 20: 34 | # self.wall = True 35 | 36 | def show(self, win, col, shape= 1): 37 | if self.wall == True: 38 | col = (0, 0, 0) 39 | if shape == 1: 40 | pygame.draw.rect(win, col, (self.x*w, self.y*h, w-1, h-1)) 41 | else: 42 | pygame.draw.circle(win, col, (self.x*w+w//2, self.y*h+h//2), w//3) 43 | 44 | def add_neighbors(self, grid): 45 | if self.x < cols - 1: 46 | self.neighbors.append(grid[self.x+1][self.y]) 47 | if self.x > 0: 48 | self.neighbors.append(grid[self.x-1][self.y]) 49 | if self.y < rows - 1: 50 | self.neighbors.append(grid[self.x][self.y+1]) 51 | if self.y > 0: 52 | self.neighbors.append(grid[self.x][self.y-1]) 53 | #Add Diagonals 54 | # if self.x < cols - 1 and self.y < rows - 1: 55 | # self.neighbors.append(grid[self.x+1][self.y+1]) 56 | # if self.x < cols - 1 and self.y > 0: 57 | # self.neighbors.append(grid[self.x+1][self.y-1]) 58 | # if self.x > 0 and self.y < rows - 1: 59 | # self.neighbors.append(grid[self.x-1][self.y+1]) 60 | # if self.x > 0 and self.y > 0: 61 | # self.neighbors.append(grid[self.x-1][self.y-1]) 62 | 63 | 64 | def clickWall(pos, state): 65 | i = pos[0] // w 66 | j = pos[1] // h 67 | grid[i][j].wall = state 68 | 69 | def place(pos): 70 | i = pos[0] // w 71 | j = pos[1] // h 72 | return w, h 73 | 74 | for i in range(cols): 75 | arr = [] 76 | for j in range(rows): 77 | arr.append(Spot(i, j)) 78 | grid.append(arr) 79 | 80 | for i in range(cols): 81 | for j in range(rows): 82 | grid[i][j].add_neighbors(grid) 83 | 84 | 85 | start = grid[0][0] 86 | end = grid[cols - cols//2][rows - cols//4] 87 | start.wall = False 88 | end.wall = False 89 | 90 | queue.append(start) 91 | start.visited = True 92 | 93 | def main(): 94 | flag = False 95 | noflag = True 96 | startflag = False 97 | 98 | while True: 99 | for event in pygame.event.get(): 100 | if event.type == pygame.QUIT: 101 | pygame.quit() 102 | sys.exit() 103 | elif event.type == pygame.MOUSEBUTTONDOWN: 104 | if event.button in (1, 3): 105 | clickWall(pygame.mouse.get_pos(), event.button==1) 106 | elif event.type == pygame.MOUSEMOTION: 107 | if event.buttons[0] or event.buttons[2]: 108 | clickWall(pygame.mouse.get_pos(), event.buttons[0]) 109 | if event.type == pygame.KEYDOWN: 110 | if event.key == pygame.K_RETURN: 111 | startflag = True 112 | 113 | if startflag: 114 | if len(queue) > 0: 115 | current = queue.popleft() 116 | if current == end: 117 | temp = current 118 | while temp.prev: 119 | path.append(temp.prev) 120 | temp = temp.prev 121 | if not flag: 122 | flag = True 123 | print("Done") 124 | elif flag: 125 | continue 126 | if flag == False: 127 | for i in current.neighbors: 128 | if not i.visited and not i.wall: 129 | i.visited = True 130 | i.prev = current 131 | queue.append(i) 132 | else: 133 | if noflag and not flag: 134 | Tk().wm_withdraw() 135 | messagebox.showinfo("No Solution", "There was no solution" ) 136 | noflag = False 137 | else: 138 | continue 139 | 140 | 141 | win.fill((0, 20, 20)) 142 | for i in range(cols): 143 | for j in range(rows): 144 | spot = grid[i][j] 145 | spot.show(win, (44, 62, 80)) 146 | if spot in path: 147 | spot.show(win, (46, 204, 113)) 148 | spot.show(win, (192, 57, 43), 0) 149 | elif spot.visited: 150 | spot.show(win, (39, 174, 96)) 151 | if spot in queue and not flag: 152 | spot.show(win, (44, 62, 80)) 153 | spot.show(win, (39, 174, 96), 0) 154 | if spot == start: 155 | spot.show(win, (0, 255, 200)) 156 | if spot == end: 157 | spot.show(win, (0, 120, 255)) 158 | 159 | 160 | pygame.display.flip() 161 | 162 | 163 | main() 164 | --------------------------------------------------------------------------------