diff --git a/board.js b/board.js index 0d5146c..3e8e568 100644 --- a/board.js +++ b/board.js @@ -9,14 +9,18 @@ var tilesColor = [water,beach,grass] var board_numbers = []; var cycle = 50; -var tile_width=16; -var tile_height=16; -var width = Math.floor(1600 / tile_width); -var height = Math.floor(800 / tile_height); +var tile_width=32; +var tile_height=32; +var width = Math.floor(640 / tile_width); +var height = Math.floor(640 / tile_height); var current_noise = []; var currentBoardArray = []; var clickCounter = 0; +var startx = 0; +var starty = 0; +var endx = 0; +var endy = 0; function findHeighestNumber(array){ @@ -100,7 +104,7 @@ function createBoard(height, width, array){ var board = document.getElementById("can"); var context = board.getContext("2d"); - console.log(array); + ////console.log(array); for(var i = 0; i < height; i++){ for(var j = 0; j < width; j++){ @@ -137,7 +141,7 @@ function generate_perlin_noise(rounds,height,width){ for(var r = 0; r < rounds; r++){ var tmp_array = []; - //console.log("------------------ Step "+r+" --------------"); + ////console.log("------------------ Step "+r+" --------------"); @@ -148,11 +152,11 @@ function generate_perlin_noise(rounds,height,width){ var startValue = Math.random() * (maxValue - minValue) + minValue; //Stupid, but i always forgot, how to set the boundries var counter = 0; for(var j = 0; j < width; j++){ - //console.log(counter); - //console.log("Step "+i+"+"+j+":"); - //console.log("counter: "+counter); + ////console.log(counter); + ////console.log("Step "+i+"+"+j+":"); + ////console.log("counter: "+counter); if (j % (points_width - 1) === 0 && !(j === 0) || j === (width - 1)){ - //console.log(counter); + ////console.log(counter); let endValue = Math.random() * (maxValue - minValue) + minValue; //Stupid, but i always forgot, how to set the boundries interpolate(startValue, endValue, counter); row = row.concat(interpolate(startValue, endValue, counter)); @@ -162,7 +166,7 @@ function generate_perlin_noise(rounds,height,width){ counter++; } } - //console.log(row); + ////console.log(row); tmp_array.push(row); } @@ -192,7 +196,7 @@ function generate_perlin_noise(rounds,height,width){ points_width = Math.floor(points_width / 2)+1; points_height = Math.floor(points_height / 2)+1; } - console.log(result); + //console.log(result); //return result; @@ -206,8 +210,8 @@ function generate_perlin_noise(rounds,height,width){ combi.push(rows); } - console.log("------------- Ergebnisse ----------- Ergebnisse -------------"); - console.log(combi); + //console.log("------------- Ergebnisse ----------- Ergebnisse -------------"); + //console.log(combi); // adding all array to one array for(var r = 0; r < rounds; r++){ @@ -230,8 +234,8 @@ function generate_perlin_noise(rounds,height,width){ // ???? Anomalie ???? - console.log(result); - console.log(combi); + //console.log(result); + //console.log(combi); return combi; } @@ -242,11 +246,12 @@ function generate_perlin_noise(rounds,height,width){ * */ function run_perlin_noise(){ var noise_array = generate_perlin_noise(cycle,width,height); - console.log(noise_array); + //console.log(noise_array); current_noise = noise_array; currentBoardArray = createBoardArray(current_noise, height, width); createBoard(height,width,currentBoardArray); - + //currentBoardArray = createBoardArray(current_noise, height, width); + //dijkstra(currentBoardArray,startx,15,15,30,30); } @@ -283,16 +288,26 @@ function moveShip(canvas, e){ 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); + //console.log("x: "+x+" y: "+y); } window.onload = (function(){ - var can = document.getElementById("can"); - can.addEventListener('mousedown', function(e){ + var can = document.getElementById("can"); + rect = can.getBoundingClientRect(); + can.addEventListener('mousedown', function(e){ + var x = Math.floor((e.clientX - rect.left)/tile_width); + var y = Math.floor((e.clientY - rect.top )/tile_height); if(clickCounter === 2){ clickcounter = 1; + endx = x; + endy = y; + console.log(startx+" "+starty+" : "+endx+" "+endy); + currentBoardArray = createBoardArray(current_noise, height, width); + dijkstra(currentBoardArray, startx, starty,endx,endy); } else { clickCounter++; + startx = x; + starty = y; } createBoard(height,width,currentBoardArray); moveShip(can, e); diff --git a/dijkstra.js b/dijkstra.js index c89fc00..5e2dcfa 100644 --- a/dijkstra.js +++ b/dijkstra.js @@ -1,45 +1,47 @@ //implemtantion of dijkstra algorithm -var max = Number.MAX_SAFE_INTEGER; +var max = Number.MAX_SAFE_INTEGER-100; -function findNodeWithMinDist(list){ - var result = ""; +function findNodeWithMinDistAndInQ(list, Q){ + var result = "NaN"; 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; - } + for(var i = 0; i < Q.length; i++){ + //console.log(Q[i]); + if(list.get(Q[i]) < dist){ + dist = list.get(Q[i]); + result = Q[i]; } } - + //console.log("result: "+result); 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); - } - } - } - } + if((i-1) > -1 && (i+1) < array.length && (j-1) > -1 && j+1 < array[i].length){ + for(var k = -1; k < 2; k++){ + for(var l = -1; l < 2; l++){ + var row =i+k; + var coloumn=j+l; + if(array[row][coloumn].accessible){ + result.push(row+"$"+coloumn); + } + } + } } else { + for(var k = -1; k < 2; k++){ - for(var l = -1; l < 2; l){ + for(var l = -1; l < 2; l++){ + var row =i+k; + var coloumn=j+l; try{ - if(array[i][j].accessible){ - result.push(i+"$"+j); + if(array[row][coloumn].accessible){ + result.push(row+"$"+coloumn); } } catch(e){ - console.log("zugriff auserhalb des feldes"); + ////console.log("zugriff auserhalb des feldes"); } } } @@ -53,40 +55,96 @@ function getNeighbours(array, i,j){ * */ function dijkstra(array, start_i, start_j, end_i,end_j){ - var Q = new Set(); - var distance = []; - var pre = []; - + + var Q = []; + var distance = new Map(); + var pre = new Map(); + 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); + var tmp = i+"$"+j; + distance.set(tmp, max); + pre.set(tmp, null); + Q.push(tmp); } } - distance[start_i][start_j] = 0; - - while !(Q.size === 0){ - var node = findNodeWithMinDist(distance); - Q.delete(node); + + distance.set(start_i+"$"+start_j, 0); + var t = 0; + while (Q.length !== 0){ + var node = findNodeWithMinDistAndInQ(distance,Q); + if(node === "NaN"){ + console.log(Q); + console.log(distance); + + return null; + } + + Q.splice(Q.indexOf(node),1); 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; + indices[0] = parseInt(10, indices[0]); + indices[1] = parseInt(10, indices[1]); + + var neighbours = getNeighbours(array,indices[0],indices[1]); + //console.log(node+" === "+end_i+"$"+end_j); + + if(node.localeCompare(end_i+"$"+end_j) === 0){ + + console.log("YAY"); + + console.log(t); + console.log(distance); + console.log(pre); + + + + return reverse(end_i+"$"+end_j,start_i+"$"+start_j,pre); + } + for(var i = 0; i < neighbours.length; i++){ + var alt = distance.get(node)+1; + + if(Q.includes(neighbours[i])){ + if(alt < distance.get(neighbours[i])){ + distance.set(neighbours[i], alt); + pre.set(neighbours[i], node); } } } - } - + //console.log(t); + t++; + }// + console.log(t); + console.log(distance); + console.log(pre); - - return {dist: distance[], pre: pre[]}; + return null; } + +function reverse(target, source, pre){ + var path = []; + var u = target; + + console.log("target: "+target); + console.log("source: "+source); + if(pre.get(u) != null || u === source){ + console.log("hallo"); + while(u != null){ + console.log("hallo"); + u = pre.get(u); + path = [u].concat(path); + } + } + path.push(target); + path.shift(); + + + console.log("!!!!") + console.log(path); + + return path; + + +} \ No newline at end of file diff --git a/index.html b/index.html index e818f16..ad8ab60 100644 --- a/index.html +++ b/index.html @@ -2,11 +2,12 @@ Perlin Noise +

Perlin Noise

- +