Created
April 6, 2016 22:35
-
-
Save ikarius6/571d3dfc7dab6410d260e48da6179895 to your computer and use it in GitHub Desktop.
Where can I walk step by step? - on JS - by Jack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
/* | |
Where can I walk step by step? - by Jack | |
0 = can walk | |
1 = a wall | |
Other numbers > padding = steps | |
*/ | |
var map = [ | |
[0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,1,0,0,0,0], | |
[0,0,0,0,0,0,1,0,0,0], | |
[0,0,0,0,0,0,1,0,0,0], | |
[0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0,0], | |
]; | |
var point = [4,4]; | |
var distance = 2; //try with 6! | |
show_map(); | |
build_layer( 1, point, distance ); | |
/* This is magic from jack */ | |
function build_layer( step, point, distance ){ | |
var padding = 2; //padding dont include reserved status like 1 = block | |
var new_step = step + padding; //the real code of the steps | |
if( step == 1){ //first time dont exist nothing, so go to origin | |
map[point[0]][point[1]] = new_step; | |
}else{ | |
var width = map[0].length; | |
var height = map.length; | |
for(var y=0; y < height; y++){ | |
for(var x=0; x < width;x++){ | |
if( map[x][y] == new_step - 1 ){ | |
x_p = x+1; x_l = x-1; y_p = y+1; y_l = y-1; | |
if( x_p < width //less than width | |
&& map[x_p][y]!=1 //dont a block: can be a method to get all blocks not only '1' | |
&& !(map[x_p][y]>padding && map[x_p][y]<=padding+distance) //dont a current step | |
){ map[x_p][y] = new_step; } | |
if( x_l >= 0 && map[x_l][y]!=1 && !(map[x_l][y]>padding && map[x_l][y]<=padding+distance)){ map[x_l][y] = new_step; } | |
if(y_p < height && map[x][y_p]!=1 && !(map[x][y_p]>padding && map[x][y_p]<=padding+distance)){ map[x][y_p] = new_step; } | |
if(y_l >= 0 && map[x][y_l]!=1 && !(map[x][y_l]>padding && map[x][y_l]<=padding+distance)){ map[x][y_l] = new_step; } | |
} | |
} | |
} | |
} | |
show_map(); //for debug | |
if( step+1 <= distance ){ //limit of steps | |
build_layer( step+1, point, distance ); | |
} | |
} | |
function show_map(){ | |
for(var i=0; i < map.length; i++){ | |
print_line = ""; | |
for(var j=0; j < map[0].length; j++){ | |
print_line += map[i][j]+""; | |
} | |
console.log( print_line ); | |
} | |
console.log("----------------------"); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment