Spiral Php checkerboard display with array

So, I'm looking at how to make a control panel, but with a spiral in it instead of the standard checkerboard, made like this:

$checkerboard=array();
    for($row=0;$row<10;$row++){
    if($row%2==0){
        for($col=0;$col<10;$col++){
            if($col%2==0){
                $checkerboard[$row][$col]="white";
            }else{
                $checkerboard[$row][$col]="black";
            }
        }
    }else{
        for($col=0;$col<10;$col++){
            if($col%2==0){
                $checkerboard[$row][$col]="black";
            }else{
                $checkerboard[$row][$col]="white";
            }
        }
    }
}

I also tried this with two diagonals:

$diagonal=array();
    for($row=0;$row<10;$row++){
        for($col=0;$col<10;$col++){
            if($row==$col){
                $diagonal[$row][$col]='black';
            }else{
                $diagonal[$row][$col]='white';
            }
            if($row+$col==9){
                $diagonal[$row][$col]='black';
            }
        }
    }

And then echo'd just like that:

echo "<table>";
for($row=0;$row<count($checkerboard);$row++){
    echo "<tr>";
        for($col=0;$col<count($checkerboard);$col++){
            echo "<td width='50px' height='50px' bgcolor='".$checkerboard[$row][$col]."'></td>";
        }
    echo "</tr>";
}

I would like to keep the code simple because I did not program php very long and should work with an array.

I tried this here:

$spiral=array();
    for($row=0;$row<10;$row++){
        for($col=0;$col<10;$col++){
            $spiral[$row][$col]='white';
            if($row==0 or $row==9 or $col==0 or $col==9){
                $spiral[$row][$col]='black';
            }if($row==1 and $col==0){
                $spiral[$row][$col]='white';
            }if($row==2 and $col<8){
                $spiral[$row][$col]='black';
            }if($row>1 and $row<8 and $col==7){
                $spiral[$row][$col]='black';
            }if($row==7 and $col>1 and $col<8){
                $spiral[$row][$col]='black';
            }if($row>3 and $row<7 and $col==2){
                $spiral[$row][$col]='black';
            }if($row==4 and $col>2 and $col<6){
                $spiral[$row][$col]='black';
            }if($row==5 and $col==5){
                $spiral[$row][$col]='black';
            }
        }
    }

But if the chessboard becomes larger, it will be very difficult to change. Is this a way of relief?

+4
source share
2 answers

Try the following:

I create an empty board, then start drawing horizontal and vertical lines, starting at the edges each time. The code may need some tweaking, but it's a good start.

$checkerboard=array();

$size = 12;

for ($row=0; $row<$size; $row++) {
    for ($col=0; $col<$size; $col++) {
        $checkerboard[$row][$col]="red";
    }
}

//horizontal
$pair = 0 ;
while ($pair < (int) $size / 2) {
    //drawing top half rows
    $row = 2 * $pair;
    $end = min($row, $size - $row);
    $start = $end - 2;
    for ($col = $start; ($col < $size - $end) && ($row < $size / 2); $col++){
        $checkerboard[$row][$col]="black";
    }
    //drawing bottom half rows
    $far_row = $size - 1 - 2 * $pair;
    $end = min($far_row, $size - $far_row) + 1 - 2;
    $start = $end ;
    for ($col = $start; ($col < $size - $end) && ($far_row > $size / 2 ); $col++){
        $checkerboard[$far_row][$col]="black";
    }
    $pair++;
}

$pair = 0;
//vertical
while ($pair < (int) $size / 2)  {
    //drawing left half columns
    $col = 2 * $pair;
    $end = min($col, $size - $col);
    $start = $end +2 ;
    for ($row = $start; ($row < $size - $end) && ($col < $size / 2); $row++){
        $checkerboard[$row][$col]="black";
    }
    //drawing right half columns
    $far_columns = $size - 1 - 2 * $pair;
    $end = min($far_columns, $size - $far_columns) - 1;
    $start = $end ;
    for ($row = $start; ($row < $size - $end) && ($far_columns  >= ($size / 2 ) ); $row++){
        $checkerboard[$row][$far_columns]="black";
    }
    $pair++;
}


echo "<table>";
for($row=0;$row< $size;$row++){
    echo "<tr>";
        for($col=0; $col< $size; $col++){
            echo "<td width='50px' height='50px' bgcolor='".$checkerboard[$row][$col]."'></td>";
        }
    echo "</tr>";
}

$size

+1

:

function buildSpiral($gridSize)
{
    /**
     * Origin is at the top left handcorner
     */
    $x = 0;
    $y = 0;
    $xMin = 0;
    $xMax = $gridSize-1;
    $yMin = 2;
    $yMax = $gridSize-1;
    $pattern = [];
    $size = $gridSize;

    $collision = function($p, $limit) {
        return (bool) ($p == $limit);
    };

    // increment x
    $shadeRight = function(&$x, $y) use (&$pattern, &$xMin, &$xMax, $collision) {
        if ($xMin > $xMax) {
            return;     
        }
        do {
            $pattern[$y][$x] = 1;
        } while (!$collision($x++, $xMax));
        if ($x >= $xMax) {
            $x=$xMax;
        }
        $xMax-=2;
    };

    // increment y
    $shadeDown = function($x, &$y) use (&$pattern, &$yMin, &$yMax, $collision) {
        while ($y < $yMax && $yMin > $yMax) {
            $pattern[++$y][$x] = 1;
        }
        if ($yMin > $yMax) {
            return;
        }

        do {
            $pattern[$y][$x] = 1;
        } while (!$collision($y++, $yMax));
        if ($y >= $yMax) {
            $y = $yMax;
        }
        $yMax-=2;
    };

    // decrement x
    $shadeLeft = function(&$x, $y) use (&$pattern, &$xMin, &$xMax, $collision, $gridSize) {
        if ($xMin > $xMax) {
            return;
        }
        do {
            $pattern[$y][$x] = 1;
        } while (!$collision($x--, $xMin));
        if ($x < $xMin) {
            $x=$xMin;
        }
        $xMin+=2;
    };

    // decrement y
    $shadeUp = function($x, &$y) use (&$pattern, &$yMin, &$yMax, $collision, $gridSize) {
        while ($y > $yMin && $yMin > $yMax) {
            $pattern[--$y][$x] = 1;
        }

        if ($yMin > $yMax) {
            return;
        }
        do {
            $pattern[$y][$x] = 1;
        } while(!$collision(--$y, $yMin));
        if ($y < $yMin) {
            $y = $yMin;
        }

        $yMin+=2;
    };

    while ($size > 0) {
        $shadeRight($x, $y);
        $shadeDown($x, $y);
        $shadeLeft($x, $y);
        $shadeUp($x, $y);

        $size-=2;
    }

    return $pattern;    
}

for ($i = 1; $i <= 25; $i++) {
    $checkboard = buildSpiral($i);
    echo  "<h1>$i</h1>";
    echo "<table style='margin-bottom: 2em;'>";
    for($row=0;$row<count($checkboard);$row++){
        echo "<tr>";
            for($col=0;$col<count($checkboard);$col++){
                if (!isset($checkboard[$row][$col])) {
                    echo "<td width='50px' height='50px' bgcolor=\"red\"></td>";
                } else {
                    echo "<td width='50px' height='50px' bgcolor=\"black\"></td>";
                }
            }
        echo "</tr>";
    }
    echo "</table>";
}

.

. , , , , , :

function buildSpiral($gridSize)
{
    /**
     * Origin is at the top left handcorner
     */
    $x = $gridSize-1;
    $y = 0;
    $xMin = 0;
    $xMax = $gridSize-1;
    $yMin = 2;
    $yMax = $gridSize-1;
    $pattern = [];
    $size = $gridSize;

    $collision = function($p, $limit) {
        return (bool) ($p == $limit);
    };

    // increment x
    $shadeRight = function(&$x, $y) use (&$pattern, &$xMin, &$xMax, $collision) {
        if ($xMin > $xMax) {
            return;     
        }
        do {
            $pattern[$y][$x] = 1;
        } while (!$collision($x++, $xMax));
        if ($x >= $xMax) {
            $x=$xMax;
        }
        $xMax-=2;
    };

    // increment y
    $shadeDown = function($x, &$y) use (&$pattern, &$yMin, &$yMax, $collision) {
        while ($y < $yMax && $yMin > $yMax) {
            $pattern[++$y][$x] = 1;
        }
        if ($yMin > $yMax) {
            return;
        }

        do {
            $pattern[$y][$x] = 1;
        } while (!$collision($y++, $yMax));
        if ($y >= $yMax) {
            $y = $yMax;
        }
        $yMax-=2;
    };

    // decrement x
    $shadeLeft = function(&$x, $y) use (&$pattern, &$xMin, &$xMax, $collision, $gridSize) {
        if ($xMin > $xMax) {
            return;
        }
        do {
            $pattern[$y][$x] = 1;
        } while (!$collision($x--, $xMin));
        if ($x < $xMin) {
            $x=$xMin;
        }
        $xMin+=2;
    };

    // decrement y
    $shadeUp = function($x, &$y) use (&$pattern, &$yMin, &$yMax, $collision, $gridSize) {
        while ($y > $yMin && $yMin > $yMax) {
            $pattern[--$y][$x] = 1;
        }

        if ($yMin > $yMax) {
            return;
        }
        do {
            $pattern[$y][$x] = 1;
        } while(!$collision(--$y, $yMin));
        if ($y < $yMin) {
            $y = $yMin;
        }

        $yMin+=2;
    };

    while ($size > 0) {
        $shadeLeft($x, $y);
        $shadeDown($x, $y);
        $shadeRight($x, $y);
        $shadeUp($x, $y);

        $size-=2;
    }

    return $pattern;    
}

for ($i = 1; $i <= 25; $i++) {
    $checkboard = buildSpiral($i);
    echo  "<h1>$i</h1>";
    echo "<table style='margin-bottom: 2em;'>";
    for($row=0;$row<count($checkboard);$row++){
        echo "<tr>";
            for($col=0;$col<count($checkboard);$col++){
                if (!isset($checkboard[$row][$col])) {
                    echo "<td width='50px' height='50px' bgcolor=\"red\"></td>";
                } else {
                    echo "<td width='50px' height='50px' bgcolor=\"black\"></td>";
                }
            }
        echo "</tr>";
    }
    echo "</table>";
}
+1

All Articles