function findRoute() {
      let dist = new Uint16Array(rooms.length), todo = [0];
      dist.fill(-1);
      dist[0] = 0;
      while(true) {
        const i = todo.shift(), d = dist[i], walls = rooms[i],
          rc = rcFromI(i),
          row = rc[0], col = rc[1];
        if (i===rooms.length-1) { break; }
        if (col>0 && !(walls&LEFT) && dist[i-1]>d+1) {
          dist[i-1] = d+1;
          todo.push(i-1);
        }
        if (row>0 && !(walls&TOP) && dist[i-cols]>d+1) {
          dist[i-cols] = d+1;
          todo.push(i-cols);
        }
        if (col<cols-1 && !(walls&RIGHT) && dist[i+1]>d+1) {
          dist[i+1] = d+1;
          todo.push(i+1);
        }
        if (row<rows-1 && !(walls&BOTTOM) && dist[i+cols]>d+1) {
          dist[i+cols] = d+1;
          todo.push(i+cols);
        }
      }

      route = [rooms.length-1];
      while(true) {
        const i = route[0], d = dist[i], walls = rooms[i],
          rc = rcFromI(i),
          row = rc[0], col = rc[1];
        if (i===0) { break; }
        if (col<cols-1 && !(walls&RIGHT) && dist[i+1]<d) {
          route.unshift(i+1);
          continue;
        }
        if (row<rows-1 && !(walls&BOTTOM) && dist[i+cols]<d) {
          route.unshift(i+cols);
          continue;
        }
        if (row>0 && !(walls&TOP) && dist[i-cols]<d) {
          route.unshift(i-cols);
          continue;
        }
        if (col>0 && !(walls&LEFT) && dist[i-1]<d) {
          route.unshift(i-1);
          continue;
        }
        // this should never happen!
        console.log("No route found!");
        break;
      }
    }
/**
     * We wouldn't want to generate a maze we can't solve ourselves...
     */
We wouldn't want to generate a maze we can't solve ourselves
We wouldn ' t want to generate a maze we can ' t solve ourselves
function findRoute() {
      let dist = new Uint16Array(rooms.length), todo = [0];
      dist.fill(-1);
      dist[0] = 0;
      while(true) {
        const i = todo.shift(), d = dist[i], walls = rooms[i],
          rc = rcFromI(i),
          row = rc[0], col = rc[1];
        if (i===rooms.length-1) { break; }
        if (col>0 && !(walls&LEFT) && dist[i-1]>d+1) {
          dist[i-1] = d+1;
          todo.push(i-1);
        }
        if (row>0 && !(walls&TOP) && dist[i-cols]>d+1) {
          dist[i-cols] = d+1;
          todo.push(i-cols);
        }
        if (col<cols-1 && !(walls&RIGHT) && dist[i+1]>d+1) {
          dist[i+1] = d+1;
          todo.push(i+1);
        }
        if (row<rows-1 && !(walls&BOTTOM) && dist[i+cols]>d+1) {
          dist[i+cols] = d+1;
          todo.push(i+cols);
        }
      }
      route = [rooms.length-1];
      while(true) {
        const i = route[0], d = dist[i], walls = rooms[i],
          rc = rcFromI(i),
          row = rc[0], col = rc[1];
        if (i===0) { break; }
        if (col<cols-1 && !(walls&RIGHT) && dist[i+1]<d) {
          route.unshift(i+1);
          continue;
        }
        if (row<rows-1 && !(walls&BOTTOM) && dist[i+cols]<d) {
          route.unshift(i+cols);
          continue;
        }
        if (row>0 && !(walls&TOP) && dist[i-cols]<d) {
          route.unshift(i-cols);
          continue;
        }
        if (col>0 && !(walls&LEFT) && dist[i-1]<d) {
          route.unshift(i-1);
          continue;
        }
        console.log("No route found!");
        break;
      }
    }
function findRoute ( ) { let dist = new Uint16Array ( rooms . length ) , todo = [ 0 ] ; dist . fill ( - 1 ) ; dist [ 0 ] = 0 ; while ( true ) { const i = todo . shift ( ) , d = dist [ i ] , walls = rooms [ i ] , rc = rcFromI ( i ) , row = rc [ 0 ] , col = rc [ 1 ] ; if ( i === rooms . length - 1 ) { break ; } if ( col > 0 && ! ( walls & LEFT ) && dist [ i - 1 ] > d + 1 ) { dist [ i - 1 ] = d + 1 ; todo . push ( i - 1 ) ; } if ( row > 0 && ! ( walls & TOP ) && dist [ i - cols ] > d + 1 ) { dist [ i - cols ] = d + 1 ; todo . push ( i - cols ) ; } if ( col < cols - 1 && ! ( walls & RIGHT ) && dist [ i + 1 ] > d + 1 ) { dist [ i + 1 ] = d + 1 ; todo . push ( i + 1 ) ; } if ( row < rows - 1 && ! ( walls & BOTTOM ) && dist [ i + cols ] > d + 1 ) { dist [ i + cols ] = d + 1 ; todo . push ( i + cols ) ; } } route = [ rooms . length - 1 ] ; while ( true ) { const i = route [ 0 ] , d = dist [ i ] , walls = rooms [ i ] , rc = rcFromI ( i ) , row = rc [ 0 ] , col = rc [ 1 ] ; if ( i === 0 ) { break ; } if ( col < cols - 1 && ! ( walls & RIGHT ) && dist [ i + 1 ] < d ) { route . unshift ( i + 1 ) ; continue ; } if ( row < rows - 1 && ! ( walls & BOTTOM ) && dist [ i + cols ] < d ) { route . unshift ( i + cols ) ; continue ; } if ( row > 0 && ! ( walls & TOP ) && dist [ i - cols ] < d ) { route . unshift ( i - cols ) ; continue ; } if ( col > 0 && ! ( walls & LEFT ) && dist [ i - 1 ] < d ) { route . unshift ( i - 1 ) ; continue ; } console . log ( "No route found!" ) ; break ; } }
We wouldn't want to generate a maze we can't solve ourselves...
We wouldn ' t want to generate a maze we can ' t solve ourselves ...
// this should never happen!