Here generating a maze by backtracking works correctly but the thing is that I am trying to implement this in a pixel game (Minecraft)... so the problem is in drawing the maze. In this game, the size of the wall blocks should be the exact same as the size of an empty block/space, so the only solution I thought of was a bonus 2d array called totalmaze. Its purpose is to store both empty spaces and wall blocks so I made its size (x*3, y*3) and tried outputting the walls but unfortunately this causes a lot of problems such as too much empty space / blocked paths. Note : X, Z because it is a 3d maze.
If this was JavaScript and a simple app, I would just draw the walls as lines but in Minecraft, the sizes should be the same so this is where things become troublesome even though the algorithm is correct. Please help.
This is how I am trying to fix it but perhaps is not the way : https://prnt.sc/fbp88o
public function generateMaze($dim_x, $walls_height, $dim_z){
$maze = array();
$moves = array();
$cell_count = $dim_x*$dim_z;
for($position=0; $position<$cell_count; $position++){
$maze[$position] = "01111"; // visited, NSEW
}
$pos=0;
$maze[0]{0} = 1; /// initial
$visited = 1;
// determine possible directions
while($visited<$cell_count){
$possible = "";
if((floor($pos/$dim_x)==floor(($pos-1)/$dim_x)) and ($maze[$pos-1]{0}==0)){
$possible .= "W";
}
if((floor($pos/$dim_x)==floor(($pos+1)/$dim_x)) and ($maze[$pos+1]{0}==0)){
$possible .= "E";
}
if((($pos+$dim_x)<$cell_count) and ($maze[$pos+$dim_x]{0}==0)){
$possible .= "S";
}
if((($pos-$dim_x)>=0) and ($maze[$pos-$dim_x]{0}==0)){
$possible .= "N";
}
if($possible){
$visited ++;
array_push($moves,$pos);
$direction = $possible{rand(0,strlen($possible)-1)};
switch($direction){
case "N":
$maze[$pos]{1} = 0;
$maze[$pos-$dim_x]{2} = 0;
$pos -= $dim_x;
break;
case "S":
$maze[$pos]{2} = 0;
$maze[$pos+$dim_x]{1} = 0;
$pos += $dim_x;
break;
case "E":
$maze[$pos]{3} = 0;
$maze[$pos+1]{4} = 0;
$pos ++;
break;
case "W":
$maze[$pos]{4} = 0;
$maze[$pos-1]{3} = 0;
$pos --;
break;
}
$maze[$pos]{0} = 1;
}
else{
$pos = array_pop($moves);
}
}
$totalmaze = array();
for($i=0; $i<$dim_x*3+1; $i++){
$totalmaze[$i][0] = 1;
$totalmaze[$i][$dim_z*3-1]=1;
}
for($i=0; $i<$dim_z*3+1; $i++){
$totalmaze[0][$i] = 1;
$totalmaze[$dim_x*3-1][$i]=1;
}
for($position=0; $position<$cell_count; $position++){
$x = $position % $dim_x;
$z = floor($position / $dim_x);
if($maze[$position]{1} == 1){
$totalmaze[$x*3+1][$z*3]=1;
}
if($maze[$position]{2} == 1){
$totalmaze[$x*3+1][$z*3+2]=1;
}
if($maze[$position]{3} == 1){
$totalmaze[$x*3+2][$z*3+1]=1;
}
if($maze[$position]{4} == 1){
$totalmaze[$x*3][$z*3+1]=1;
}
}