Guest User

Untitled

a guest
Jan 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.54 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. var maxLoop = 0;
  12. function mazeFinder(startx, starty, maze, maxx, maxy){
  13. //console.log("mazeFinder " + startx + ' ' + starty + " ");
  14. //Goal is up/left in hardcoded maze so might as well prioritze those
  15. //Left -> i - 1, j.
  16. //right -> i + 1, j.
  17. //up -> i, j-1.
  18. //down -> i, j + 1.
  19. // i and j can't be greater than maxx/j
  20. maxLoop++;
  21.  
  22. if(maxLoop > 80){
  23. console.log("MaxLoop hit " + startx + ' ' + starty + " ");
  24. return false;
  25. }
  26. var foundExit = false;
  27. if(startx > maxx || starty > maxy){
  28. console.log("out of bounds " + startx + ' ' + starty);
  29. return false;
  30. }
  31. if(maze[startx][starty] == 'E'){
  32. console.log("Found exit! " + startx + ' ' + starty + " ");
  33. foundExit = true;
  34. }
  35.  
  36. maze[startx][starty] = 'K';
  37. if( (validStep(startx-1,starty,maze,maxx,maxy)) && !foundExit)
  38. {
  39. console.log("At " + startx + ' ' + starty + " Steping Left" + maze[startx-1][starty] + "'");;
  40. foundExit = mazeFinder(startx-1,starty,maze,maxx,maxy); //step left!
  41. }
  42. if( (validStep(startx,starty-1,maze,maxx,maxy)) && !foundExit)
  43. {
  44. console.log("At " + startx + ' ' + starty + " Steping up " + maze[startx][starty-1]);
  45. foundExit = mazeFinder(startx,starty-1,maze,maxx,maxy); //step up!
  46. }
  47. if( (validStep(startx+1,starty,maze,maxx,maxy)) && !foundExit)
  48. {
  49. console.log("At " + startx + ' ' + starty + " Steping Right");
  50. foundExit = mazeFinder(startx+1,starty,maze,maxx,maxy); //step right!
  51. }
  52. if( (validStep(startx,starty+1,maze,maxx,maxy)) && !foundExit)
  53. {
  54. console.log("At " + startx + ' ' + starty + " Steping Down");
  55. foundExit = mazeFinder(startx,starty+1,maze,maxx,maxy); //step down!
  56. }
  57. if(foundExit){
  58. maze[startx][starty] = '!';
  59. console.log("Marking Path " + startx + ' ' + starty + " ");
  60. return true;
  61. }
  62. console.log("At " + startx + ' ' + starty + " backtracking");
  63. maze[startx][starty] = 'x';
  64. return false;
  65. }
  66.  
  67.  
  68.  
  69. function validStep(startx, starty, maze, maxx,maxy){
  70. if(startx > maxx || starty > maxy){
  71. return false;
  72. }
  73. if(maze[startx][starty] === ' ' || maze[startx][starty] === 'E'){
  74. return true;
  75. }
  76. }
  77.  
  78. function createArray(length) {
  79. var arr = new Array(length || 0),
  80. i = length;
  81. if (arguments.length > 1) {
  82. var args = Array.prototype.slice.call(arguments, 1);
  83. while(i--) arr[length-1 - i] = createArray.apply(this, args);
  84. }
  85. return arr;
  86. }
  87. var maze = createArray(10,10);
  88. for(var i = 0; i < 10; i++){
  89. for( var j = 0; j < 10; j++){
  90. maze[i][j] = ' ';
  91. }
  92. }
  93. maze[0][0] = 'W';
  94. maze[1][0] = 'E';
  95. maze[2][0] = 'W';
  96. maze[3][0] = 'W';
  97. maze[4][0] = 'W';
  98. maze[5][0] = 'W';
  99. maze[6][0] = 'W';
  100. maze[7][0] = 'W';
  101. maze[8][0] = 'W';
  102. maze[9][0] = 'W';
  103. maze[0][1] = 'W';
  104. maze[0][2] = 'W';
  105. maze[0][3] = 'W';
  106. maze[0][4] = 'W';
  107. maze[0][5] = 'W';
  108. maze[0][6] = 'W';
  109. maze[0][7] = 'W';
  110. maze[0][8] = 'W';
  111. maze[0][9] = 'W';
  112. maze[7][1] = 'W';
  113. maze[9][1] = 'W';
  114. maze[2][2] = 'W';
  115. maze[3][2] = 'W';
  116. maze[4][2] = 'W';
  117. maze[5][2] = 'W';
  118. maze[7][2] = 'W';
  119. maze[9][2] = 'W';
  120. maze[3][3] = 'W';
  121. maze[7][3] = 'W';
  122. maze[9][3] = 'W';
  123. maze[3][4] = 'W';
  124. maze[5][4] = 'W';
  125. maze[7][4] = 'W';
  126. maze[9][4] = 'W';
  127. maze[5][5] = 'W';
  128. maze[9][5] = 'W';
  129. maze[2][6] = 'W';
  130. maze[5][6] = 'W';
  131. maze[9][6] = 'W';
  132. maze[2][7] = 'W';
  133. maze[5][7] = 'W';
  134. maze[6][7] = 'W';
  135. maze[7][7] = 'W';
  136. maze[9][7] = 'W';
  137. maze[2][8] = 'W';
  138. maze[5][8] = 'W';
  139. maze[1][9] = 'W';
  140. maze[2][9] = 'W';
  141. maze[3][9] = 'W';
  142. maze[4][9] = 'W';
  143. maze[5][9] = 'W';
  144. maze[6][9] = 'W';
  145. maze[7][9] = 'W';
  146. maze[8][9] = 'W';
  147. maze[9][9] = 'W';
  148.  
  149. console.log(maze[8][6]);
  150. mazeFinder(9,8,maze,9,9);
  151. for(var i = 0; i < 10; i++){
  152. var display = '';
  153. for(var j = 0; j < 10; j++){
  154. display += maze[j][i];
  155. }
  156. console.log(display);
  157. }
  158. </script>
  159.  
  160.  
  161.  
  162. <script id="jsbin-source-javascript" type="text/javascript">var maxLoop = 0;
  163. function mazeFinder(startx, starty, maze, maxx, maxy){
  164. //console.log("mazeFinder " + startx + ' ' + starty + " ");
  165. //Goal is up/left in hardcoded maze so might as well prioritze those
  166. //Left -> i - 1, j.
  167. //right -> i + 1, j.
  168. //up -> i, j-1.
  169. //down -> i, j + 1.
  170. // i and j can't be greater than maxx/j
  171. maxLoop++;
  172.  
  173. if(maxLoop > 80){
  174. console.log("MaxLoop hit " + startx + ' ' + starty + " ");
  175. return false;
  176. }
  177. var foundExit = false;
  178. if(startx > maxx || starty > maxy){
  179. console.log("out of bounds " + startx + ' ' + starty);
  180. return false;
  181. }
  182. if(maze[startx][starty] == 'E'){
  183. console.log("Found exit! " + startx + ' ' + starty + " ");
  184. foundExit = true;
  185. }
  186.  
  187. maze[startx][starty] = 'K';
  188. if( (validStep(startx-1,starty,maze,maxx,maxy)) && !foundExit)
  189. {
  190. console.log("At " + startx + ' ' + starty + " Steping Left" + maze[startx-1][starty] + "'");;
  191. foundExit = mazeFinder(startx-1,starty,maze,maxx,maxy); //step left!
  192. }
  193. if( (validStep(startx,starty-1,maze,maxx,maxy)) && !foundExit)
  194. {
  195. console.log("At " + startx + ' ' + starty + " Steping up " + maze[startx][starty-1]);
  196. foundExit = mazeFinder(startx,starty-1,maze,maxx,maxy); //step up!
  197. }
  198. if( (validStep(startx+1,starty,maze,maxx,maxy)) && !foundExit)
  199. {
  200. console.log("At " + startx + ' ' + starty + " Steping Right");
  201. foundExit = mazeFinder(startx+1,starty,maze,maxx,maxy); //step right!
  202. }
  203. if( (validStep(startx,starty+1,maze,maxx,maxy)) && !foundExit)
  204. {
  205. console.log("At " + startx + ' ' + starty + " Steping Down");
  206. foundExit = mazeFinder(startx,starty+1,maze,maxx,maxy); //step down!
  207. }
  208. if(foundExit){
  209. maze[startx][starty] = '!';
  210. console.log("Marking Path " + startx + ' ' + starty + " ");
  211. return true;
  212. }
  213. console.log("At " + startx + ' ' + starty + " backtracking");
  214. maze[startx][starty] = 'x';
  215. return false;
  216. }
  217.  
  218.  
  219.  
  220. function validStep(startx, starty, maze, maxx,maxy){
  221. if(startx > maxx || starty > maxy){
  222. return false;
  223. }
  224. if(maze[startx][starty] === ' ' || maze[startx][starty] === 'E'){
  225. return true;
  226. }
  227. }
  228.  
  229. function createArray(length) {
  230. var arr = new Array(length || 0),
  231. i = length;
  232. if (arguments.length > 1) {
  233. var args = Array.prototype.slice.call(arguments, 1);
  234. while(i--) arr[length-1 - i] = createArray.apply(this, args);
  235. }
  236. return arr;
  237. }
  238. var maze = createArray(10,10);
  239. for(var i = 0; i < 10; i++){
  240. for( var j = 0; j < 10; j++){
  241. maze[i][j] = ' ';
  242. }
  243. }
  244. maze[0][0] = 'W';
  245. maze[1][0] = 'E';
  246. maze[2][0] = 'W';
  247. maze[3][0] = 'W';
  248. maze[4][0] = 'W';
  249. maze[5][0] = 'W';
  250. maze[6][0] = 'W';
  251. maze[7][0] = 'W';
  252. maze[8][0] = 'W';
  253. maze[9][0] = 'W';
  254. maze[0][1] = 'W';
  255. maze[0][2] = 'W';
  256. maze[0][3] = 'W';
  257. maze[0][4] = 'W';
  258. maze[0][5] = 'W';
  259. maze[0][6] = 'W';
  260. maze[0][7] = 'W';
  261. maze[0][8] = 'W';
  262. maze[0][9] = 'W';
  263. maze[7][1] = 'W';
  264. maze[9][1] = 'W';
  265. maze[2][2] = 'W';
  266. maze[3][2] = 'W';
  267. maze[4][2] = 'W';
  268. maze[5][2] = 'W';
  269. maze[7][2] = 'W';
  270. maze[9][2] = 'W';
  271. maze[3][3] = 'W';
  272. maze[7][3] = 'W';
  273. maze[9][3] = 'W';
  274. maze[3][4] = 'W';
  275. maze[5][4] = 'W';
  276. maze[7][4] = 'W';
  277. maze[9][4] = 'W';
  278. maze[5][5] = 'W';
  279. maze[9][5] = 'W';
  280. maze[2][6] = 'W';
  281. maze[5][6] = 'W';
  282. maze[9][6] = 'W';
  283. maze[2][7] = 'W';
  284. maze[5][7] = 'W';
  285. maze[6][7] = 'W';
  286. maze[7][7] = 'W';
  287. maze[9][7] = 'W';
  288. maze[2][8] = 'W';
  289. maze[5][8] = 'W';
  290. maze[1][9] = 'W';
  291. maze[2][9] = 'W';
  292. maze[3][9] = 'W';
  293. maze[4][9] = 'W';
  294. maze[5][9] = 'W';
  295. maze[6][9] = 'W';
  296. maze[7][9] = 'W';
  297. maze[8][9] = 'W';
  298. maze[9][9] = 'W';
  299.  
  300. console.log(maze[8][6]);
  301. mazeFinder(9,8,maze,9,9);
  302. for(var i = 0; i < 10; i++){
  303. var display = '';
  304. for(var j = 0; j < 10; j++){
  305. display += maze[j][i];
  306. }
  307. console.log(display);
  308. }
  309. </script></body>
  310. </html>
Add Comment
Please, Sign In to add comment