4

So I'm looking how to make a checkboard but with a spiral in it instead of the default 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 it with 2 diagonals like this:

$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 simply like this:

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'd like to keep the code simple because I've not been coding php for a very long time and it has to 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 checkerboard becomes bigger it will be very hard to change. It there a way to make it easier?

2
  • Did you already have tried it yourself to create the circle? Can you show the code of what you already tried?
    – S.Pols
    Dec 4, 2014 at 8:48
  • do you mean spiral? or circle?
    – 321zeno
    Dec 4, 2014 at 9:08

2 Answers 2

2

Try this out:

I create an empty board then start drawing horizontal & vertical lines, starting from the edges each time. The code might 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>";
}

You can just change the $size variable for different dimensions

1
  • 1
    If you really wanna learn, try to dissect 321zeno his code and try to recreate or rewrite it. He used some nice steps to make it understandable and simple.
    – Rimble
    Dec 5, 2014 at 17:47
2

Here is my attempt at the problem:

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>";
}

I stop each shading direction when a limit is hit.

Update Let's say I want the spirals to start from the top right corner, then we just need to set the new origin and call the shaders in the way we want the spiral to go like so:

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>";
}
2
  • 1
    It's good but the previous answer was equaly good and more compact even though you are using a function.
    – Wanjia
    Dec 5, 2014 at 17:15
  • In my attempt I was trying to come up with a solution I can use for different directions, I will update my solution to show you what I mean -- see if you can figure out how to make them start from the remaining corners.
    – ymas
    Dec 5, 2014 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.