Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- var maxLoop = 0;
- function mazeFinder(startx, starty, maze, maxx, maxy){
- //console.log("mazeFinder " + startx + ' ' + starty + " ");
- //Goal is up/left in hardcoded maze so might as well prioritze those
- //Left -> i - 1, j.
- //right -> i + 1, j.
- //up -> i, j-1.
- //down -> i, j + 1.
- // i and j can't be greater than maxx/j
- maxLoop++;
- if(maxLoop > 80){
- console.log("MaxLoop hit " + startx + ' ' + starty + " ");
- return false;
- }
- var foundExit = false;
- if(startx > maxx || starty > maxy){
- console.log("out of bounds " + startx + ' ' + starty);
- return false;
- }
- if(maze[startx][starty] == 'E'){
- console.log("Found exit! " + startx + ' ' + starty + " ");
- foundExit = true;
- }
- maze[startx][starty] = 'K';
- if( (validStep(startx-1,starty,maze,maxx,maxy)) && !foundExit)
- {
- console.log("At " + startx + ' ' + starty + " Steping Left" + maze[startx-1][starty] + "'");;
- foundExit = mazeFinder(startx-1,starty,maze,maxx,maxy); //step left!
- }
- if( (validStep(startx,starty-1,maze,maxx,maxy)) && !foundExit)
- {
- console.log("At " + startx + ' ' + starty + " Steping up " + maze[startx][starty-1]);
- foundExit = mazeFinder(startx,starty-1,maze,maxx,maxy); //step up!
- }
- if( (validStep(startx+1,starty,maze,maxx,maxy)) && !foundExit)
- {
- console.log("At " + startx + ' ' + starty + " Steping Right");
- foundExit = mazeFinder(startx+1,starty,maze,maxx,maxy); //step right!
- }
- if( (validStep(startx,starty+1,maze,maxx,maxy)) && !foundExit)
- {
- console.log("At " + startx + ' ' + starty + " Steping Down");
- foundExit = mazeFinder(startx,starty+1,maze,maxx,maxy); //step down!
- }
- if(foundExit){
- maze[startx][starty] = '!';
- console.log("Marking Path " + startx + ' ' + starty + " ");
- return true;
- }
- console.log("At " + startx + ' ' + starty + " backtracking");
- maze[startx][starty] = 'x';
- return false;
- }
- function validStep(startx, starty, maze, maxx,maxy){
- if(startx > maxx || starty > maxy){
- return false;
- }
- if(maze[startx][starty] === ' ' || maze[startx][starty] === 'E'){
- return true;
- }
- }
- function createArray(length) {
- var arr = new Array(length || 0),
- i = length;
- if (arguments.length > 1) {
- var args = Array.prototype.slice.call(arguments, 1);
- while(i--) arr[length-1 - i] = createArray.apply(this, args);
- }
- return arr;
- }
- var maze = createArray(10,10);
- for(var i = 0; i < 10; i++){
- for( var j = 0; j < 10; j++){
- maze[i][j] = ' ';
- }
- }
- maze[0][0] = 'W';
- maze[1][0] = 'E';
- maze[2][0] = 'W';
- maze[3][0] = 'W';
- maze[4][0] = 'W';
- maze[5][0] = 'W';
- maze[6][0] = 'W';
- maze[7][0] = 'W';
- maze[8][0] = 'W';
- maze[9][0] = 'W';
- maze[0][1] = 'W';
- maze[0][2] = 'W';
- maze[0][3] = 'W';
- maze[0][4] = 'W';
- maze[0][5] = 'W';
- maze[0][6] = 'W';
- maze[0][7] = 'W';
- maze[0][8] = 'W';
- maze[0][9] = 'W';
- maze[7][1] = 'W';
- maze[9][1] = 'W';
- maze[2][2] = 'W';
- maze[3][2] = 'W';
- maze[4][2] = 'W';
- maze[5][2] = 'W';
- maze[7][2] = 'W';
- maze[9][2] = 'W';
- maze[3][3] = 'W';
- maze[7][3] = 'W';
- maze[9][3] = 'W';
- maze[3][4] = 'W';
- maze[5][4] = 'W';
- maze[7][4] = 'W';
- maze[9][4] = 'W';
- maze[5][5] = 'W';
- maze[9][5] = 'W';
- maze[2][6] = 'W';
- maze[5][6] = 'W';
- maze[9][6] = 'W';
- maze[2][7] = 'W';
- maze[5][7] = 'W';
- maze[6][7] = 'W';
- maze[7][7] = 'W';
- maze[9][7] = 'W';
- maze[2][8] = 'W';
- maze[5][8] = 'W';
- maze[1][9] = 'W';
- maze[2][9] = 'W';
- maze[3][9] = 'W';
- maze[4][9] = 'W';
- maze[5][9] = 'W';
- maze[6][9] = 'W';
- maze[7][9] = 'W';
- maze[8][9] = 'W';
- maze[9][9] = 'W';
- console.log(maze[8][6]);
- mazeFinder(9,8,maze,9,9);
- for(var i = 0; i < 10; i++){
- var display = '';
- for(var j = 0; j < 10; j++){
- display += maze[j][i];
- }
- console.log(display);
- }
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">var maxLoop = 0;
- function mazeFinder(startx, starty, maze, maxx, maxy){
- //console.log("mazeFinder " + startx + ' ' + starty + " ");
- //Goal is up/left in hardcoded maze so might as well prioritze those
- //Left -> i - 1, j.
- //right -> i + 1, j.
- //up -> i, j-1.
- //down -> i, j + 1.
- // i and j can't be greater than maxx/j
- maxLoop++;
- if(maxLoop > 80){
- console.log("MaxLoop hit " + startx + ' ' + starty + " ");
- return false;
- }
- var foundExit = false;
- if(startx > maxx || starty > maxy){
- console.log("out of bounds " + startx + ' ' + starty);
- return false;
- }
- if(maze[startx][starty] == 'E'){
- console.log("Found exit! " + startx + ' ' + starty + " ");
- foundExit = true;
- }
- maze[startx][starty] = 'K';
- if( (validStep(startx-1,starty,maze,maxx,maxy)) && !foundExit)
- {
- console.log("At " + startx + ' ' + starty + " Steping Left" + maze[startx-1][starty] + "'");;
- foundExit = mazeFinder(startx-1,starty,maze,maxx,maxy); //step left!
- }
- if( (validStep(startx,starty-1,maze,maxx,maxy)) && !foundExit)
- {
- console.log("At " + startx + ' ' + starty + " Steping up " + maze[startx][starty-1]);
- foundExit = mazeFinder(startx,starty-1,maze,maxx,maxy); //step up!
- }
- if( (validStep(startx+1,starty,maze,maxx,maxy)) && !foundExit)
- {
- console.log("At " + startx + ' ' + starty + " Steping Right");
- foundExit = mazeFinder(startx+1,starty,maze,maxx,maxy); //step right!
- }
- if( (validStep(startx,starty+1,maze,maxx,maxy)) && !foundExit)
- {
- console.log("At " + startx + ' ' + starty + " Steping Down");
- foundExit = mazeFinder(startx,starty+1,maze,maxx,maxy); //step down!
- }
- if(foundExit){
- maze[startx][starty] = '!';
- console.log("Marking Path " + startx + ' ' + starty + " ");
- return true;
- }
- console.log("At " + startx + ' ' + starty + " backtracking");
- maze[startx][starty] = 'x';
- return false;
- }
- function validStep(startx, starty, maze, maxx,maxy){
- if(startx > maxx || starty > maxy){
- return false;
- }
- if(maze[startx][starty] === ' ' || maze[startx][starty] === 'E'){
- return true;
- }
- }
- function createArray(length) {
- var arr = new Array(length || 0),
- i = length;
- if (arguments.length > 1) {
- var args = Array.prototype.slice.call(arguments, 1);
- while(i--) arr[length-1 - i] = createArray.apply(this, args);
- }
- return arr;
- }
- var maze = createArray(10,10);
- for(var i = 0; i < 10; i++){
- for( var j = 0; j < 10; j++){
- maze[i][j] = ' ';
- }
- }
- maze[0][0] = 'W';
- maze[1][0] = 'E';
- maze[2][0] = 'W';
- maze[3][0] = 'W';
- maze[4][0] = 'W';
- maze[5][0] = 'W';
- maze[6][0] = 'W';
- maze[7][0] = 'W';
- maze[8][0] = 'W';
- maze[9][0] = 'W';
- maze[0][1] = 'W';
- maze[0][2] = 'W';
- maze[0][3] = 'W';
- maze[0][4] = 'W';
- maze[0][5] = 'W';
- maze[0][6] = 'W';
- maze[0][7] = 'W';
- maze[0][8] = 'W';
- maze[0][9] = 'W';
- maze[7][1] = 'W';
- maze[9][1] = 'W';
- maze[2][2] = 'W';
- maze[3][2] = 'W';
- maze[4][2] = 'W';
- maze[5][2] = 'W';
- maze[7][2] = 'W';
- maze[9][2] = 'W';
- maze[3][3] = 'W';
- maze[7][3] = 'W';
- maze[9][3] = 'W';
- maze[3][4] = 'W';
- maze[5][4] = 'W';
- maze[7][4] = 'W';
- maze[9][4] = 'W';
- maze[5][5] = 'W';
- maze[9][5] = 'W';
- maze[2][6] = 'W';
- maze[5][6] = 'W';
- maze[9][6] = 'W';
- maze[2][7] = 'W';
- maze[5][7] = 'W';
- maze[6][7] = 'W';
- maze[7][7] = 'W';
- maze[9][7] = 'W';
- maze[2][8] = 'W';
- maze[5][8] = 'W';
- maze[1][9] = 'W';
- maze[2][9] = 'W';
- maze[3][9] = 'W';
- maze[4][9] = 'W';
- maze[5][9] = 'W';
- maze[6][9] = 'W';
- maze[7][9] = 'W';
- maze[8][9] = 'W';
- maze[9][9] = 'W';
- console.log(maze[8][6]);
- mazeFinder(9,8,maze,9,9);
- for(var i = 0; i < 10; i++){
- var display = '';
- for(var j = 0; j < 10; j++){
- display += maze[j][i];
- }
- console.log(display);
- }
- </script></body>
- </html>
Add Comment
Please, Sign In to add comment