Python code here.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: A.Akdogan
"""
from random import randint
import os
import sys
import time
from time import sleep
print(sys.path)
class Node:
def __init__(self, x, y):
self.x = x
self.y = y
self.f = 0
self.g = 0
self.h = 0
self.neighbors = []
self.previous = None
self. obstacle = False
def add_neighbors(self,grid, columns, rows):
neighbor_x = self.x
neighbor_y = self.y
if neighbor_x < columns - 1:
self.neighbors.append(grid[neighbor_x+1][neighbor_y])
if neighbor_x > 0:
self.neighbors.append(grid[neighbor_x-1][neighbor_y])
if neighbor_y < rows -1:
self.neighbors.append(grid[neighbor_x][neighbor_y +1])
if neighbor_y > 0:
self.neighbors.append(grid[neighbor_x][neighbor_y-1])
#diagonals
""" if neighbor_x > 0 and neighbor_y > 0:
self.neighbors.append(grid[neighbor_x-1][neighbor_y-1])
if neighbor_x < columns -1 and neighbor_y > 0:
self.neighbors.append(grid[neighbor_x+1][neighbor_y-1])
if neighbor_x > 0 and neighbor_y 0:
open_set, closed_set, current_node, final_path = AStar.start_path(open_set, closed_set, current_node, self.end)
if len(final_path) > 0:
break
return final_path
cols = 25
rows = 25
start = [0,0]
end = [24,24]
open_set = []
closed_set = []
current_node = None
final_path = []
grid = []
def show_func(grid_element,color, width,height):
if grid_element.obstacle == True:
fill("black")
else:
fill(color)
noStroke()
rect(grid_element.x * width, grid_element.y * height, width-1 , height-1)
def setup():
global grid
createCanvas(500, 500)
background(160)
flag = False
def draw():
global grid
global end
global open_set
global closed_set
global final_path
global current_node
global flag
global start
global cols
global rows
frameRate(60)
w = width / cols
h = height / rows
if flag == False:
grid = AStar.create_grid(cols, rows)
grid = AStar.fill_grids(grid, cols, rows, obstacle_ratio = 30)
grid = AStar.get_neighbors(grid, cols, rows)
start = grid[start[0]][start[1]]
end = grid[end[0]][end[1]]
end.obstacle = False
start.obstacle = False
background(0)
for i in range(cols):
for j in range(rows):
show_func(grid[i][j], color(255),w,h)
stroke(0,0,0)
line(0, 0, 0, width)
line(0,0,height, 1)
open_set.append(start)
flag = True
if len(open_set) > 0:
open_set, closed_set, current_node, final_path = AStar.start_path(open_set, closed_set, current_node, end)
#grid
show_func(start, "green", w,h)
show_func(end, "red",w,h)
for i in range(len(open_set)):
#show_func(open_set[i], "#00ffbf",w,h)
show_func(open_set[i], "#00ffbf",w,h)
for i in range(len(closed_set)):
show_func(closed_set[i], "#ffa500",w,h)
show_func(start, "green", w,h)
show_func(current_node, "#8a2be2",w,h)
if len(open_set) == 0:
print("No way!")
#noLoop()
frameRate(1)
cols = 25
rows = 25
start = [0,0]
end = [24,24]
open_set = []
closed_set = []
current_node = None
final_path = []
grid = []
flag = False
if len(final_path) > 0:
for i in range(len(final_path)):
show_func(final_path[i], "#8a2be2",w,h)
#show_func(final_path[i], "red",w,h)
show_func(start, "green", w,h)
show_func(end, "red",w,h)
print("Done!!")
frameRate(1)
cols = 25
rows = 25
start = [0,0]
end = [24,24]
open_set = []
closed_set = []
current_node = None
final_path = []
grid = []
flag = False