cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

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.

            
              <table id="tablo"></table>
            
          
!
            
              #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;
}
            
          
!
            
              /**
 * 
 * @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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console