Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. $(document).ready(function () {
  2. createMaze();
  3. });
  4.  
  5.  
  6. let currentNode = getJQueryCellByRowCol(0 , 9)
  7.  
  8. function createMaze(){
  9. createGrid(20, 20);
  10. figureMaze();
  11. }
  12.  
  13.  
  14. function createGrid(x, y) {
  15. for(let i = 0; i < x; i++) {
  16. for(let j = 0; j < y; j++) {
  17. let node = $("<div class='node'></div>");
  18. $("#container").append(node);
  19. nodeAttr(node, i, j)
  20. }
  21. }
  22.  
  23. getJQueryCellByRowCol(0, 9).addClass("start")
  24. getJQueryCellByRowCol(14, 19).addClass("exit")
  25.  
  26. $(".node").width(800/x);
  27. $(".node").height(800/y)
  28. };
  29.  
  30. function nodeAttr(node, i, j) {
  31. node.attr('row', i);
  32. node.attr('column', j);
  33. };
  34.  
  35. function figureMaze(){
  36. while(!(currentNode.i == 19 && currentNode.j == 14)) {
  37. loopThroughMazeGenerator();
  38. }
  39.  
  40. }
  41.  
  42. function Direction(){
  43. let randomNum = Math.floor(Math.random() * 4) + 1;
  44. return randomNum
  45. }
  46.  
  47. function loopThroughMazeGenerator(){
  48. let Row = parseInt(currentNode.attr('row'));
  49. let Col = parseInt(currentNode.attr('column'));
  50.  
  51.  
  52. let rDirection = Direction()
  53. let validDirection = isValidDirection(rDirection)
  54. console.log(validDirection)
  55. console.log(rDirection)
  56. if(ValidDirection == false){
  57. Direction()
  58. }
  59. if(validDirection == true){
  60. if(rDirection == 1){
  61. let newRow = Row--
  62. currentNode = getJQueryCellByRowCol(newRow, Col)
  63. }
  64. else if(rDirection == 2){
  65. let newCol = Col++
  66. currentNode = getJQueryCellByRowCol(Row, newCol)
  67. }
  68. else if(rDirection == 3){
  69. let newRow = Row++
  70. currentNode = getJQueryCellByRowCol(newRow, Col)
  71. }
  72. else if(rDirection == 4){
  73. let newCol = Col--
  74. currentNode = getJQueryCellByRowCol(Row, newCol)
  75. }
  76. }
  77. }
  78.  
  79. function isValidDirection(direction) {
  80. let stringNumRow = currentNode.attr('row');
  81. let stringNumCol = currentNode.attr('column');
  82. let numRow = parseInt(stringNumRow);
  83. let numCol = parseInt(stringNumCol);
  84.  
  85. if(direction == 1){
  86. if(numRow == 0){
  87. return false;
  88. }
  89. }
  90. else if(direction == 2){
  91. if(numCol == 19){
  92. return false;
  93. }
  94. }
  95. else if(direction == 3){
  96. if(numRow == 19){
  97. return false;
  98. }
  99. }
  100. else if(direction == 4){
  101. if(numCol == 0){
  102. return false;
  103. }
  104. }
  105. return true;
  106.  
  107. }
  108.  
  109. function getJQueryCellByRowCol(row, column){
  110. const jQueryResult = $('[row="'+row+'"][column="'+column+'"]');
  111.  
  112. if(jQueryResult.length === 0){
  113. throw `Sorry, no node found that matches row:${row}, col:${column}`;
  114. }
  115.  
  116. if(jQueryResult.length !== 1){
  117. throw `Sorry, multiple cells found that match row:${row}, col:${column}`;
  118. }
  119.  
  120. return jQueryResult;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement