Dijkstra Algorithmus

master
Schoko 4 years ago
parent 0aa3e3de59
commit ab1a2fa905
  1. 20
      board.js
  2. 92
      dijkstra.js

@ -11,11 +11,11 @@ var tilesColor = [water,beach,grass]
var board_numbers = [];
var cycle = 50;
var tile_width=10;
var tile_height=10;
var tile_width=16;
var tile_height=16;
var width = Math.floor(1600 / tile_width);
var height = Math.floor(800 / tile_height);
var current_noise = [];
function findHeighestNumber(array){
@ -80,15 +80,17 @@ class Ship{
draw(context){
var img = new Image();
img.src = "ship.png";
context.drawImage(img,32,32);
img.height = "16";
img.width = "16";
context.drawImage(img,this.x,this.y);
}
update(){
this.draw(context);
this.x += dx;
this.y += dy;
this.draw(context);
}
}
@ -107,7 +109,7 @@ function createBoard(height, width, array){
//console.log(value);
//console.log(array[i][j]);
if(x <= 100.00 && x >= 70.00){
color = 2;
color = 1;
}
if(x <= 69.00 && x >= 65.00){
color = 1;
@ -257,6 +259,7 @@ function generate_perlin_noise(rounds,height,width){
function run_perlin_noise(){
var noise_array = generate_perlin_noise(cycle,width,height);
console.log(noise_array);
current_noise = noise_array;
createBoard(height,width,noise_array);
}
@ -264,8 +267,8 @@ function run_perlin_noise(){
function moveShip(canvas, e){
var rect = canvas.getBoundingClientRect();
var x = e.clientX - rect.left;
var y = e.clientY - rect.top;
var x = Math.floor(e.clientX - rect.left);
var y = Math.floor(e.clientY - rect.top);
var ship = new Ship(x,y,5);
ship.draw(canvas.getContext("2d"));
console.log("x: "+x+" y: "+y);
@ -274,6 +277,7 @@ function moveShip(canvas, e){
window.onload = (function(){
var can = document.getElementById("can");
can.addEventListener('mousedown', function(e){
createBoard(height,width,current_noise);
moveShip(can, e);
});

@ -0,0 +1,92 @@
//implemtantion of dijkstra algorithm
var max = Number.MAX_SAFE_INTEGER;
function findNodeWithMinDist(list){
var result = "";
var dist = max;
for(var i = 0; i < list.length; i++){
for(var j = 0; j < list[i].length; j++){
if(dist < list[i][j]){
dist = list[i][j];
result = i+"$"+j;
}
}
}
return result;
}
function getNeighbours(array, i,j){
var result = [];
if(i - 1 > -1 && i+1 < array.length){
if(j-1 > -1 && j+1 < array.length){
for(var k = -1; k < 2; k++){
for(var l = -1; l < 2; l){
if(array[i][j].accessible){
result.push(i+"$"+j);
}
}
}
}
} else {
for(var k = -1; k < 2; k++){
for(var l = -1; l < 2; l){
try{
if(array[i][j].accessible){
result.push(i+"$"+j);
}
} catch(e){
console.log("zugriff auserhalb des feldes");
}
}
}
}
return result;
}
/**
*
*/
function dijkstra(array, start_i, start_j, end_i,end_j){
var Q = new Set();
var distance = [];
var pre = [];
for(var i = 0; i < array.length; i++){
for(var j = 0; j < array[i].length; j++){
distance[i][j] = max;
pre[i][j] = null;
Q.add(i+"$"+j);
}
}
distance[start_i][start_j] = 0;
while !(Q.size === 0){
var node = findNodeWithMinDist(distance);
Q.delete(node);
var indices = node.split("$");
var neighbours = getNeighbours(array,indicies[0],indices[1]);
for(var i = 0; i < indices.length; i++){
var alt = dist[indices[0]][indices[1]]+1;
if(Q.has(neighbours[i])){
var indices_neighbour = neighbour[i].split("$");
if(alt < dist[indices_neighbour[i]][indices_neighbour[j]]){
dist[indicies_neighbour[i]][indices_neighbour[j]] = alt;
pre[indices_neighbour[i]][indices_neighbour[j]] = node;
}
}
}
}
return {dist: distance[], pre: pre[]};
}
Loading…
Cancel
Save