Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <table id="tablo"></table>
              
            
!

CSS

              
                #tablo {
  width : 100%;
}


.node {
  height : 40px;
  width : 40px;
  background-color : #009999;
  color : white;
  font-size : 12px;
	text-align: center;
}


main {
  position : absolute;
  left : 0px;
  right : 0px;
  bottom : 0px;
  top : 0px;
}
              
            
!

JS

              
                /**
 * 
 * @author faruk can ->  @farukcan.net
 *
 * @import Vec2.js  ->  farukcan.net/examples/Vec2.js
 * @import Heap.js  ->  farukcan.net/examples/Heap.js
 * @import Jquery  ->  cdn
 * @import dat.gui ->	cdn
 * 
 * @classes Grid,Node,Astar
 * @functions  main
 */

$(main);

// @class Grid
function Grid(size) {
	this.nodes = []
	this.size = size;
	this.start;this.startNode;this.target;this.targetNode;
	
	this.create = function(){
		this.nodes = [];
		for (var i = 0; i < size.x; i++) {
			this.nodes[i] = [];
			for (var j = 0; j < size.y; j++) 
				this.nodes[i][j] = new Node(i, j);
		}
	}
	
	this.create();

}

// @prototype Grid
Grid.prototype = {
	get : function(v) {
		if(this.nodes[v.x] && this.nodes[v.x][v.y])
			return this.nodes[v.x][v.y];
		return false;
	},
	setStart : function(v) {
		this.start = v;
		this.startNode = this.get(v);
	},
	setTarget : function(v) {
		this.target = v;
		this.targetNode = this.get(v);
	}
}

// @class Node
function Node(i, j) {
	this.loc = new Vec2(i, j);
	this.gCost = 0;
	this.hCost = 0;
	this.fCost = function(){return this.gCost+this.hCost};
	this.open = false;
	this.closed = false;
	this.blocked = false;
	this.onPath = false;
	this.parent;
}

// @class Astart
function Astar(grid) {
	this.grid = grid;
	
	this.gFunc = function(v) {
		return this.grid.start.d(v);
	}

	this.hFunc = function(v) {
		return this.grid.target.d(v);
	}

	this.getNeighbors = function(x, y) {
		var neighbors = [];

		neighbors.add = function(e) {
			// kontrol et gerçeekten komşumu diye
			if(!e) return;
			if(e.blocked) return;
			neighbors.push(e);
		}
		neighbors.add(this.grid.get(new Vec2(x, y - 1)));
		neighbors.add(this.grid.get(new Vec2(x - 1, y)));
		neighbors.add(this.grid.get(new Vec2(x + 1, y)));
		neighbors.add(this.grid.get(new Vec2(x, y + 1)));

		if (conf.allowDiagonal) {
			neighbors.add(this.grid.get(new Vec2(x - 1, y - 1)));
			neighbors.add(this.grid.get(new Vec2(x + 1, y - 1)));
			neighbors.add(this.grid.get(new Vec2(x + 1, y + 1)));
			neighbors.add(this.grid.get(new Vec2(x - 1, y + 1)));
		}
		
		delete neighbors.add;
		
		return neighbors;
	}

	this.find = function() {
		var OPEN = new Heap(function(a,b){
			return a.fCost() - b.fCost();
		});
		
		OPEN.add(this.grid.startNode);

		while (OPEN.length != 0) {

			// current open içindeki en küçük f costta sahip noddür
			var currentNode = OPEN.get();
			
			if( typeof currentNode == "undefined") return false;
					console.log(currentNode.loc,this.grid.target)

			// current == target ise while döngüsünden çık ve createPath'ı çalıştır.
			if(currentNode.loc.d(this.grid.target)==0 ){
				this.createPath(this.grid.startNode,this.grid.targetNode);
				return true;
			}
			
			// currenti open'dan çıkar closed'e ekle
			currentNode.open = false;
			currentNode.closed = true;


			
			currentNode.gCost = this.gFunc(currentNode.loc);

			var neighbors = this.getNeighbors(currentNode.loc.x,currentNode.loc.y);
			// foreach komşu için
			var _this = this;
			neighbors.forEach(function(neighbor){
				// komşu kapalıysa, bunu geç
				if(neighbor.closed) return;
				
				// komşuya gidiş = current.gcost + komşuya mesafe
				var toNeighbor  = currentNode.gCost + currentNode.loc.d(neighbor.loc);
				
				neighbor.gCost = _this.gFunc(neighbor.loc);

				// eğer komşu open içinde değilse veya komşuya gidiş, komşunun gcost'dan düşük ise
				
				if(!neighbor.open || toNeighbor<neighbor.gCost){
					// komşunun gcost = komşuya gidiş
					neighbor.gCost = toNeighbor;
					
					// komşunun hcost = komuşunun hedefe mesafesi
					neighbor.hCost = _this.hFunc(neighbor.loc);

					// komşunun parrent = current;
					
					neighbor.parent = currentNode;

					// eğer komşu open içinde değilse open'a ekle
					if(!neighbor.open){
						OPEN.add(neighbor);
						neighbor.open = true;
					}
					
				}


			})


		}
		return false;

	}

	this.createPath = function(startNode, endNode) {

		var path = [];
		currentNode = endNode;

		while(currentNode.loc.d(startNode.loc)!=0){
			path.push(currentNode);
			currentNode.onPath = true;
			currentNode = currentNode.parent;
		}
		path.reverse();
		console.log(path);
		return path;
	}

}

// @func Map
function main() {
	conf = {
		sizex : 20,
		sizey : 20,
		allowDiagonal: false,
		FIND: function() {
			A.find();
		},
		RELOAD: function() {
			clearInterval(updateval);
			create();
		}
	}

	
	var gui = new dat.GUI();
	gui.add(conf, 'allowDiagonal');
	gui.add(conf, 'sizex',5,100);
	gui.add(conf, 'sizey',5,100);
	gui.add(conf, 'RELOAD');
	gui.add(conf, 'FIND');
	
	create();

}

function create(){
		
	size = new Vec2(conf.sizex, conf.sizey);

	grid = new Grid(size);
	
	grid.createTable();
	updateval = setInterval(function(){
		grid.updateTable();
	},10);

	grid.setStart(new Vec2(conf.sizex-2,conf.sizey-2));
	grid.setTarget(new Vec2(2,2));
	
	startSELECTED = false;
	targetSELECTED = false;
	



	A = new Astar(grid);
	
}


Grid.prototype.createTable = function(){
	var tablo = "";
	for (var j = 0; j < this.size.y; j++) {
		tablo += "<tr>";
		for (var i = 0; i < this.size.x; i++) {
			tablo += ("<td id='" + i + "x" + j + "' class='node'></td>");
		}
		tablo += "</tr>";
	}
	$("#tablo").html(tablo);
	
	for (var i = 0; i < this.size.x; i++) 
			for (var j = 0; j < this.size.y; j++) {
						var _this = this
						$("#"+i + "x" + j).click(function(){
								if(!startSELECTED){
										startSELECTED=true;
										grid.setStart(new Vec2(this.id.split("x")[0], this.id.split("x")[1]));
								}else if(!targetSELECTED){
										targetSELECTED=true;
										grid.setTarget(new Vec2(parseInt(this.id.split("x")[0]), parseInt(this.id.split("x")[1])));

								}else{
										_this.nodes[this.id.split("x")[0]][this.id.split("x")[1]].blocked= true;

								}
						});
			}
}
Grid.prototype.updateTable = function(){
	
		for (var i = 0; i < this.size.x; i++) 
			for (var j = 0; j < this.size.y; j++) {
					$("#"+i + "x" + j).css("background-color","#009999");
				if(this.target.d(this.nodes[i][j].loc)==0)
					$("#"+i + "x" + j).css("background-color","red");
				else if(this.start.d(this.nodes[i][j].loc)==0)
					$("#"+i + "x" + j).css("background-color","blue");
				else{
					if(this.nodes[i][j].onPath)
						$("#"+i + "x" + j).css("background-color","grey");
					else if(this.nodes[i][j].open)
						$("#"+i + "x" + j).css("background-color","green");
					else if(this.nodes[i][j].closed)
						$("#"+i + "x" + j).css("background-color","orange");
					else if(this.nodes[i][j].blocked)
						$("#"+i + "x" + j).css("background-color","black");
						
					
				}
				
				if(this.nodes[i][j].gCost != 0 && this.nodes[i][j].gCost != 0){
					var html = Math.floor(this.nodes[i][j].gCost);
					html+="+"+Math.floor(this.nodes[i][j].hCost);
					html+="</br>"+Math.floor(this.nodes[i][j].fCost());
					$("#"+i + "x" + j).html(html);
				}

			}
}

              
            
!
999px

Console