Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).ready(function () {
- createMaze();
- });
- let currentNode = getJQueryCellByRowCol(0 , 9)
- function createMaze(){
- createGrid(20, 20);
- figureMaze();
- }
- function createGrid(x, y) {
- for(let i = 0; i < x; i++) {
- for(let j = 0; j < y; j++) {
- let node = $("<div class='node'></div>");
- $("#container").append(node);
- nodeAttr(node, i, j)
- }
- }
- getJQueryCellByRowCol(0, 9).addClass("start")
- getJQueryCellByRowCol(14, 19).addClass("exit")
- $(".node").width(800/x);
- $(".node").height(800/y)
- };
- function nodeAttr(node, i, j) {
- node.attr('row', i);
- node.attr('column', j);
- };
- function figureMaze(){
- while(!(currentNode.i == 19 && currentNode.j == 14)) {
- loopThroughMazeGenerator();
- }
- }
- function Direction(){
- let randomNum = Math.floor(Math.random() * 4) + 1;
- return randomNum
- }
- function loopThroughMazeGenerator(){
- let Row = parseInt(currentNode.attr('row'));
- let Col = parseInt(currentNode.attr('column'));
- let rDirection = Direction()
- let validDirection = isValidDirection(rDirection)
- console.log(validDirection)
- console.log(rDirection)
- if(ValidDirection == false){
- Direction()
- }
- if(validDirection == true){
- if(rDirection == 1){
- let newRow = Row--
- currentNode = getJQueryCellByRowCol(newRow, Col)
- }
- else if(rDirection == 2){
- let newCol = Col++
- currentNode = getJQueryCellByRowCol(Row, newCol)
- }
- else if(rDirection == 3){
- let newRow = Row++
- currentNode = getJQueryCellByRowCol(newRow, Col)
- }
- else if(rDirection == 4){
- let newCol = Col--
- currentNode = getJQueryCellByRowCol(Row, newCol)
- }
- }
- }
- function isValidDirection(direction) {
- let stringNumRow = currentNode.attr('row');
- let stringNumCol = currentNode.attr('column');
- let numRow = parseInt(stringNumRow);
- let numCol = parseInt(stringNumCol);
- if(direction == 1){
- if(numRow == 0){
- return false;
- }
- }
- else if(direction == 2){
- if(numCol == 19){
- return false;
- }
- }
- else if(direction == 3){
- if(numRow == 19){
- return false;
- }
- }
- else if(direction == 4){
- if(numCol == 0){
- return false;
- }
- }
- return true;
- }
- function getJQueryCellByRowCol(row, column){
- const jQueryResult = $('[row="'+row+'"][column="'+column+'"]');
- if(jQueryResult.length === 0){
- throw `Sorry, no node found that matches row:${row}, col:${column}`;
- }
- if(jQueryResult.length !== 1){
- throw `Sorry, multiple cells found that match row:${row}, col:${column}`;
- }
- return jQueryResult;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement