Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

PHP maze-generator with Looping

i have problem about create maze-generator

i have variable $num = 5, and i want output like this :

@ @@@
@   @
@@@ @
@   @
@ @@@

i already trying to solve my problem and the output like this :

@ @@@
@   @
@@@ @
@   @
@@@ @

how to make output correct like i want first? I already write scipt on php like this :

<?php

$num        = 5;
$space      = $num - 2;
$end        = $num - 1;
$left       = true;
$mid        = false;
$right      = false;
$leftCount  = 1;
$rightCount = 0;

for ($i = 0; $i < $num; $i++)
{
    for ($j = 0; $j < $num; $j++)
    {
        if ($left) {
            $line = ($i % 2 == 0 && $j == 1) ? "&nbsp;&nbsp; ":" 1 ";

            ($j == $end) ? $left = false:$mid = true;
            $leftCount  = 0;
            $rightCount = 1;

            echo $line;
        }
        else if ($mid)
        {
            $line = (($i % 2 == 1 && $j == 0) || ($i % 2 == 1 && $j == $end)) ? " 2 ":"&nbsp;&nbsp; ";
            ($j == $end) ? $mid = false:"";
            ($rightCount > $leftCount) ? $right = true:$left = false;
            
            echo $line;
        }
        else if ($right)
        {
            $line = ($i % 2 == 0 && $j == $space) ? "&nbsp;&nbsp; ":" 3 ";

            ($j == $end) ? $mid = true:"";
            $rightCount = 0;
            $leftCount  = 1;

            echo $line;
        }
    }
    echo "<br>";
}

please need help to improve my code, so i can get print like first output

question from:https://stackoverflow.com/questions/65909662/php-maze-generator-with-looping

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Its seems like a lot of code to do essentually 3 things, break it down.

  • Make a line which has one hole

    • Pick the position where the hole will be rand(1, max-1)
    • Fill the rest with wall
  • On alternating lines there is no wall

    • Fill all with empty space
  • Surround the outer wall

    • Is it first or last

Join all lines/array and output.

Example:

<?php
$maze = [];
for($i=0; $i<=8; $i++) {
    $hole = rand(1, 6);
    $line = [];
    for ($ii=0; $ii<8; $ii++)
        $line[] = $ii === 0 || $ii === 7 ? '@' : ($ii === $hole || $i % 2 ? ' ' : '@');
    $maze[] = join($line).PHP_EOL;
}
echo join($maze);

Output:

@@@@@ @@
@      @
@@@ @@@@
@      @
@@@@@ @@
@      @
@@@ @@@@
@      @
@@@@@@ @

https://3v4l.org/XrWbE

Use $hole = $i % 4 ? 6 : 1; if you want holes to be in same place like your example.

@ @@@@@@
@      @
@@@@@@ @
@      @
@ @@@@@@
@      @
@@@@@@ @
@      @
@ @@@@@@

Edit:

Now changed to 5

https://3v4l.org/jTbeI

@ @@@
@   @
@@@ @
@   @
@ @@@

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...