  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # Algorithm to generate a segment maze  » algorithm » Algorithm to generate a segment maze

By : 張哲瑋
Date : January 10 2021, 01:57 PM
Does that help create a random path between point A and B randomly add walls as long as it doesn't lie on the path until you're satisfied code : ## What's a good algorithm to generate a maze?

By : Verushka
Date : March 29 2020, 07:55 AM
hope this fix your issue From http://www.astrolog.org/labyrnth/algrithm.htm ## Generate a Maze Java Algorithm

By : dzemikowocowy
Date : March 29 2020, 07:55 AM
With these it helps Your description is kinda confusing since you are not addressing what your needs are in detail, however, I stronly suggest you to go through Astrolog.org and check all the proposed Maze Algorithms.
Generally speaking, if you need to build a "hard" solving maze algorithm, you can implement Prim's algorithm and add few circles (by removing a certain amount of walls) to make it harder to solve. ## Efficient algorithm to generate a kind of maze

By : Doug Eoff
Date : March 29 2020, 07:55 AM
wish of those help Well, I've given it a try. One hour of work seems more than enough for a simple question. It is, of course, far from perfect, but it illustrates what I was talking about. It generates solutions like this:
code :
``````<?php

// error reporting
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// configuration
const SIZE_X     = 20;
const SIZE_Y     = 20;
const COMPLEXITY = 20;

// grid class
class Grid
{

public function __construct(\$width,\$height)
{
// remember
\$this->width  = \$width;
\$this->height = \$height;
// initiate grid
foreach (range(1,\$width) as \$x) {
foreach (range(1,\$height) as \$y) {
\$this->cells[\$x][\$y] = FALSE;  // false means: not in path
}
}
}

public function isWithinGrid(\$x,\$y)
// testb whether (x,y) is within the grid
{
return (\$x >= 1) && (\$x <= \$this->width) &&
(\$y >= 1) && (\$y <= \$this->height);
}

public function isWithinPath(\$x,\$y)
// is a cell part of the path?
{
return \$this->isWithinGrid(\$x,\$y) && \$this->cells[\$x][\$y];
}

public function setCellInPath(\$x,\$y,\$boolean)
// remember whether a cell is part of the path or not
{
\$this->cells[\$x][\$y] = \$boolean;
return \$this;
}

public function drawHorizontalLine(\$x1,\$x2,\$y)
// simple horizontal line
{
foreach (range(\$x1,\$x2) as \$x) \$this->setCellInPath(\$x,\$y,TRUE);
return \$this;
}

public function drawVerticalLine(\$x,\$y1,\$y2)
// simple vertical line
{
foreach (range(\$y1,\$y2) as \$y) \$this->setCellInPath(\$x,\$y,TRUE);
return \$this;
}

public function drawSquare()
// simple square
{
\$left   = round(\$this->width/5);
\$right  = round(4*\$this->width/5);
\$top    = round(\$this->height/5);
\$bottom = round(4*\$this->height/5);
\$this->drawHorizontalLine(\$left,\$right,\$top)
->drawHorizontalLine(\$left,\$right,\$bottom)
->drawVerticalLine(\$left,\$top,\$bottom)
->drawVerticalLine(\$right,\$top,\$bottom);
return \$this;
}

private function moveCell(\$x,\$y,\$dx,\$dy)
// move a cell
{
\$this->setCellInPath(\$x,\$y,FALSE);
\$this->setCellInPath(\$x+\$dx,\$y+\$dy,TRUE);
}

private function canMoveCell(\$x,\$y,\$dx,\$dy)
// answers the question whether or not we can move (x,y) by (dx,dy)
{
return \$this->isWithinPath(\$x,\$y) &&                   // must be part of path
\$this->isWithinGrid(\$x+\$dx,\$y+\$dy) &&           // stay within grid
!\$this->isWithinPath(\$x+\$dx,\$y+\$dy) &&          // but not on the path
!\$this->isWithinPath(\$x+2*\$dx,\$y+2*\$dy) &&      // and don't touch path
!\$this->isWithinPath(\$x+\$dy+\$dx,\$y+\$dx+\$dy) &&  // and don't touch path
!\$this->isWithinPath(\$x-\$dy+\$dx,\$y-\$dx+\$dy);    // and don't touch path
}

private function tryToDistortOnce(\$x,\$y,\$dx,\$dy)
{
// this one should be able to move
if (!\$this->canMoveCell(\$x,\$y,\$dx,\$dy)) return FALSE;
// but also its neighbours must be able to move
if (!\$this->canMoveCell(\$x+\$dy,\$y+\$dx,\$dx,\$dy)) return FALSE;
if (!\$this->canMoveCell(\$x-\$dy,\$y-\$dx,\$dx,\$dy)) return FALSE;
// move the target cell by displacement
\$this->moveCell(\$x,\$y,\$dx,\$dy);
// move neighbours by adding two cells
\$this->setCellInPath(\$x+\$dy+\$dx,\$y+\$dx+\$dy,TRUE);
\$this->setCellInPath(\$x-\$dy+\$dx,\$y-\$dx+\$dy,TRUE);
return TRUE; // success!
}

private function distortOnce()
// distort a random cell, returns success or failure
{
// find a random cell in path
do {
\$x = rand(1,\$this->width);
\$y = rand(1,\$this->height);
} while (!\$this->isWithinPath(\$x,\$y));
// choose one of four directions to move in
switch (rand(1,4))
{
case 1: \$dx = -1; \$dy = 0; break;
case 2: \$dx = +1; \$dy = 0; break;
case 3: \$dx = 0; \$dy = +1; break;
case 4: \$dx = 0; \$dy = -1; break;
}
// try to do it
if (\$this->tryToDistortOnce(\$x,\$y,\$dx,\$dy))
{
// more moves
do {
\$x += \$dx;
\$y += \$dy;
} while (\$this->tryToDistortOnce(\$x,\$y,\$dx,\$dy));
return TRUE; // it was a success!
}
return FALSE; // we failed
}

public function distortPath(\$numberOfDistortions = 10)
// distort up to a certain amount of times
{
// find a random cell that is part of the path to distort
for (\$counter = 1; \$counter <= \$numberOfDistortions; \$counter++) {
// we try that a limited number of times, depending on the grid size
\$tries = 0;
while (!\$this->distortOnce() &&
(\$tries < \$this->width+\$this->height)) { \$tries++; }
}
return \$this;
}

public function renderGrid()
// render grid
{
'  td { width:20px; height: 20px; border: 1px solid #000; }'.
'  .path { background-color: #44F; }'.
foreach (range(1,SIZE_Y) as \$y) {
echo '<tr>';
foreach (range(1,SIZE_X) as \$x) {
echo '<td'.(\$this->cells[\$x][\$y] ? ' class="path">' : '>');
}
echo '</tr>';
}
echo '</body></html></table>';
return \$this;
}

}

// create grid
\$grid = new Grid(SIZE_X,SIZE_Y);
\$grid->drawSquare()
->distortPath(COMPLEXITY)
->renderGrid();
`````` ## Why is my random maze generation algorithm creating a pattern of columns at the bottom of my maze?

By : JGroup
Date : March 29 2020, 07:55 AM
I hope this helps you . I fix your solution, the problem is the corner cases checking.
The main changes: ## Algorithm to generate a maze with / without solution

By : benwen
Date : March 29 2020, 07:55 AM
this will help Use a Recursive Backtracker to generate a labyrinth with one solution. Or any of the several algorithms available.
These algorithms will generate some kind of labyrinth data format, usually indicating for each cell the status of two of its edges (so, two bits per cell), and also the unique solving path.
code :
``````if (noSolution) {
var kill = Math.floor(maze.path.length/2);
maze.cell[maze.path[kill].y]
[maze.path[kill].x].edges = { };
}
`````` Related Posts : 