Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template>
  2.     <div class="home">
  3.         <!-- <h1>{{num1}} + {{num2}} = {{ sum }}</h1>     #example calculator
  4.         <input v-model="num1" placeholder="Number One">
  5.         <input v-model="num2" placeholder="Number Two"> -->
  6.         <pre>{{displayGrid(grid)}}</pre> <!-- Displey grid on load -->
  7.         <pre>{{output}}</pre>
  8.         <input v-model="ro" placeholder="Number One"> <!-- update inputs-->
  9.         <input v-model="pe" placeholder="Number Two">
  10.         <button v-on:click="changeGrid(ro,pe);displayGrid(grid)" variant="success">Update Grid</button> <!-- on click sets a place in grid to 5 depedning on inputs-->
  11.  
  12.     </div>
  13. </template>
  14.  
  15. <script>
  16.     export default({
  17.         name: 'Home',
  18.         data() { //global variables init
  19.             return {
  20.                 grid: [],
  21.                 todis: "",
  22.                 pe: Number(0),
  23.                 ro: Number(0),
  24.                 output: []
  25.             };
  26.         },
  27.         methods: { //all needed methods
  28.             addToGrid(adding) {
  29.                 this.grid.push(adding)
  30.             },
  31.             makeGrid(width, height) {
  32.                 function Node(x,y,id) { //0 - unchecked; 1 - Wall; 2 - Start; 3 - End
  33.                     this.x = x
  34.                     this.y = y
  35.                     this.id = id
  36.                     this.visited = false
  37.                     this.prev = []
  38.                 }
  39.                 if (this.grid.length === 0) { //Stops grid being made multiple times
  40.                     for (let y = 0; y < height; y++) {
  41.                         let row = []
  42.                         for (let x = 0; x < width; x++) {
  43.                             var newNode = new Node(x, y, 0)
  44.                             row.push(newNode)
  45.                         }
  46.                         this.addToGrid(row)
  47.                     }
  48.                 }
  49.             },
  50.             Dijkstra(grid, start, end) {
  51.                 function findNeighbours(grid, x, y) {
  52.                     let Neighbours = []
  53.                     let PosNeighbours = []
  54.                     grid[y][x].visited = true
  55.                     if (x == 0) {
  56.                         x = x+1
  57.                     }
  58.                     if (y == 0) {
  59.                         y = y+1
  60.                     }
  61.                     if (typeof grid[y][x+1] !== 'undefined') {
  62.                             PosNeighbours.push(grid[y][x+1])
  63.                     }
  64.                     if (typeof grid[y][x-1] !== 'undefined') {
  65.                             PosNeighbours.push(grid[y][x-1])
  66.                     }
  67.                     if (typeof grid[y+1][x] !== 'undefined') {
  68.                             PosNeighbours.push(grid[y+1][x])
  69.                     }
  70.                     if (typeof grid[y-1][x] !== 'undefined') {
  71.                             PosNeighbours.push(grid[y-1][x])
  72.                     }
  73.                     for (let node of PosNeighbours) {
  74.                         if (typeof grid[node.y][node.x] === 'undefined') {
  75.                             null
  76.                         }
  77.                         else {
  78.                             if (node.visited === false) {
  79.                                 Neighbours.push(node)
  80.                                 let nx = node.x
  81.                                 let ny = node.y
  82.                                 if (grid[ny][nx].prev !== []) {
  83.                                     console.log(node)
  84.                                     grid[ny][nx].prev = grid[y][x]
  85.                                 }
  86.                             }
  87.                         }
  88.                     }
  89.                     return Neighbours
  90.                 }
  91.                 if (start !== end) {
  92.                     let startNode = grid[start[1]][start[0]]
  93.                     this.toCheck = []
  94.                     this.toCheck.push(startNode)
  95.                     let toChecknext = []
  96.                     while (grid[end[1]][end[0]].visited === false) {
  97.                         if (this.toCheck.length !== 0) {
  98.                             let node = this.toCheck.shift()
  99.                             let nodestoadd = findNeighbours(grid, node.x, node.y)
  100.                             for (let node of nodestoadd) {
  101.                                 toChecknext.push(node)
  102.                             }
  103.                         } else {
  104.                             for (let node in toChecknext) {
  105.                                 this.toCheck.push(toChecknext.shift())
  106.                             }
  107.                         }
  108.                     }
  109.                     let currentNode = grid[end[1]][end[0]]
  110.                     let pathnodes = []
  111.                     while (currentNode.x !== startNode.x && currentNode.y !== startNode.y) {
  112.                         console.log(currentNode)
  113.                         pathnodes.push(currentNode.prev)
  114.                         currentNode = currentNode.prev
  115.                     }
  116.                     this.output = pathnodes
  117.                 } else {
  118.                     return [start]
  119.                 }
  120.  
  121.             },
  122.             displayGrid(grid) {
  123.                 this.todis = ""
  124.                 for(const row of grid) {
  125.                     for (const node of row) {
  126.                         this.todis += " "+node.id
  127.                     }
  128.                     this.todis += "\n"
  129.                 }
  130.                 return this.todis
  131.             },
  132.             changeGrid(thisx, thisy) {
  133.                 this.grid[thisy][thisx].id = 1
  134.             }
  135.         },
  136.         created: function () {
  137.             this.makeGrid(10, 10)
  138.             this.Dijkstra(this.grid,[0,0],[5,5])
  139.         },
  140.         updated: function () {
  141.             //On Update
  142.         }
  143.     });
  144.    
  145. </script>
  146.  
  147. <style></style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement