const WIDTH = 400; const HEIGHT = 400; const CELL_SIZE = 40; const NX = Math.floor(WIDTH / CELL_SIZE); const NY = Math.floor(HEIGHT / CELL_SIZE); function createCell() { let self = {}; self.visited = false; self.walls = { up: true, left: true, down: true, right: true }; return self; } function line(sx, sy, ex, ey) { beginPath(); moveTo(sx, sy); lineTo(ex, ey); stroke(); } function drawCell(cell, x, y) { if(cell.visited) fillColor(rgbToHex(0, 150, 0)); else fillColor(rgbToString(0, 255, 0)); fillRect(x, y, CELL_SIZE, CELL_SIZE); lineThickness(5); lineCap("round"); strokeColor(rgbToString(255, 255, 255)); if(cell.walls.up) line(x, y, x+CELL_SIZE, y); if(cell.walls.right) line(x+CELL_SIZE, y, x+CELL_SIZE, y+CELL_SIZE); if(cell.walls.down) line(x+CELL_SIZE, y+CELL_SIZE, x, y+CELL_SIZE); if(cell.walls.left) line(x, y+CELL_SIZE, x, y); } console.clear(); createCanvas(WIDTH, HEIGHT); clear(0, 0, 0); let cells = []; for(let i = 0; i < NX*NY; ++i) cells.push(createCell()); function drawCells() { for(let j = 0; j < NY; ++j) { let y = j*CELL_SIZE; for(let i = 0; i < NX; ++i) { let x = i*CELL_SIZE; let cell = cells[i + j*NX]; drawCell(cell, x, y); } } } drawCells(); function toIndex(i, j) { if(i < 0 || i >= NX || j < 0 || j >= NY) return -1; return i + j*NX; } function randomIndex() { let i = Math.floor(Math.random()*NX); let j = Math.floor(Math.random()*NY); return i + j*NX; } function findUnvisitedNeighbours(idx) { let i = idx % NX; let j = Math.floor(idx / NX); let upIndex = toIndex(i, j-1); let downIndex = toIndex(i, j+1); let leftIndex = toIndex(i-1, j); let rightIndex = toIndex(i+1, j); let neighbors = []; if(upIndex !== -1 && !cells[upIndex].visited) neighbors.push(upIndex); if(downIndex !== -1 && !cells[downIndex].visited) neighbors.push(downIndex); if(leftIndex !== -1 && !cells[leftIndex].visited) neighbors.push(leftIndex); if(rightIndex !== -1 && !cells[rightIndex].visited) neighbors.push(rightIndex); return neighbors; } function generateMaze() { let history = []; let idx = randomIndex(); history.push(idx); let interval = setInterval(function() { let cell = cells[idx]; let unvisitedNeighbors = findUnvisitedNeighbours(idx); cell.visited = true; console.log(idx, unvisitedNeighbors.length); if(unvisitedNeighbors.length > 0) { let newIdx = unvisitedNeighbors[Math.floor(Math.random()*unvisitedNeighbors.length)]; let newCell = cells[newIdx]; history.push(newIdx); if(newIdx < idx) { if(newIdx == idx - 1) { // left cell.walls.left = false; newCell.walls.right = false; } else { // up cell.walls.up = false; newCell.walls.down = false; } } else { if(newIdx == idx + 1) { // right cell.walls.right = false; newCell.walls.left = false; } else { // down cell.walls.down = false; newCell.walls.up = false; } } idx = newIdx; drawCells(); } else { history.pop(); console.log(history.length); if(history.length >= 1) { idx = history[history.length-1]; } else { clearInterval(interval); } drawCells(); } }, 10); } generateMaze(); drawCells();
Editor je nyní spuštěn v režimu pouze pro čtení. Scripty můžete s příslušným oprávněním vytvářet a editovat z
uživatelské sekce
.