Skip to main content
Commonmark migration
Source Link

#PHP, 182 bytes#

PHP, 182 bytes

This seemed like a fun activity for my first answer. Comments on my code are welcome.

<?php function s($n){$e=2*$n+1;for($i=0;$i<$e*$e;$i++){$x=$i%$e;$y=floor($i/$e);echo$y==$x?($x==$n?"O":"\\"):($e-1==$x+$y?"/":($y==$n?"-":($x==$n?"|":" ")));echo$x==$e-1?"\n":"";}}?>

Here is the un-golfed code with comments:

<?php
function s($n) {
    $e=2*$n+1; //edge length
    for($i=0;$i<$e*$e;$i++) {
        $x = $i%$e; // current x coordinate
        $y = floor($i/$e); // current y coordinate

        if ($y==$n&&$x==$n) {
            // center of square
            echo'O';
        }
        else if ($y==$n) {
            // horizontal line
            echo'-';
        }
        else if ($x==$n) {
            // vertical line
            echo'|';
        }
        else if ($y==$x) {
            // diagonal line from top-left to bottom right
            echo'\\';
        }
        else if (($y-$n)==($n-$x)) {
            // diagonal line from bottom-left to top-right
            echo'/';
        }
        else {
            // empty space
            echo' ';
        }
        if ($x==$e-1) {
            // add new line for the end of the row
            echo"\n";
        }
    }
}?>
<pre>
<?php s(10); ?>
</pre>

Edited with code by royhowie

#PHP, 182 bytes#

This seemed like a fun activity for my first answer. Comments on my code are welcome.

<?php function s($n){$e=2*$n+1;for($i=0;$i<$e*$e;$i++){$x=$i%$e;$y=floor($i/$e);echo$y==$x?($x==$n?"O":"\\"):($e-1==$x+$y?"/":($y==$n?"-":($x==$n?"|":" ")));echo$x==$e-1?"\n":"";}}?>

Here is the un-golfed code with comments:

<?php
function s($n) {
    $e=2*$n+1; //edge length
    for($i=0;$i<$e*$e;$i++) {
        $x = $i%$e; // current x coordinate
        $y = floor($i/$e); // current y coordinate

        if ($y==$n&&$x==$n) {
            // center of square
            echo'O';
        }
        else if ($y==$n) {
            // horizontal line
            echo'-';
        }
        else if ($x==$n) {
            // vertical line
            echo'|';
        }
        else if ($y==$x) {
            // diagonal line from top-left to bottom right
            echo'\\';
        }
        else if (($y-$n)==($n-$x)) {
            // diagonal line from bottom-left to top-right
            echo'/';
        }
        else {
            // empty space
            echo' ';
        }
        if ($x==$e-1) {
            // add new line for the end of the row
            echo"\n";
        }
    }
}?>
<pre>
<?php s(10); ?>
</pre>

Edited with code by royhowie

PHP, 182 bytes

This seemed like a fun activity for my first answer. Comments on my code are welcome.

<?php function s($n){$e=2*$n+1;for($i=0;$i<$e*$e;$i++){$x=$i%$e;$y=floor($i/$e);echo$y==$x?($x==$n?"O":"\\"):($e-1==$x+$y?"/":($y==$n?"-":($x==$n?"|":" ")));echo$x==$e-1?"\n":"";}}?>

Here is the un-golfed code with comments:

<?php
function s($n) {
    $e=2*$n+1; //edge length
    for($i=0;$i<$e*$e;$i++) {
        $x = $i%$e; // current x coordinate
        $y = floor($i/$e); // current y coordinate

        if ($y==$n&&$x==$n) {
            // center of square
            echo'O';
        }
        else if ($y==$n) {
            // horizontal line
            echo'-';
        }
        else if ($x==$n) {
            // vertical line
            echo'|';
        }
        else if ($y==$x) {
            // diagonal line from top-left to bottom right
            echo'\\';
        }
        else if (($y-$n)==($n-$x)) {
            // diagonal line from bottom-left to top-right
            echo'/';
        }
        else {
            // empty space
            echo' ';
        }
        if ($x==$e-1) {
            // add new line for the end of the row
            echo"\n";
        }
    }
}?>
<pre>
<?php s(10); ?>
</pre>

Edited with code by royhowie

deleted 168 characters in body
Source Link

#PHP, 287182 bytes#

<?php function s($n){$r='';$e=2*$n+1;for$e=2*$n+1;for($i=0;$i<$e*$e;$i++){$h=floor($e/2);$x=floor($i%$e);$y=floor$x=$i%$e;$y=floor($i/$e);if($y==$h&&$x==$h)$r.='O';elseif($y==$h)$r.='-';elseif;echo$y==$x?($x==$h$x==$n?"O":"\\")$r.='|';elseif(:($y$e-$h)==1==$x+$y?"/":($x$y==$n?"-$h))$r.='\\';elseif(":($y-$h$x==$n?"|":" ")==($h-$x))$r.='/';else$r.=' ';if($x==$e;echo$x==$e-1)$r.="\n";?"\n":"";}echo$r;}?>
<?php
function s($n) {
    $r='';
    $e=2*$n+1; //edge length
    for($i=0;$i<$e*$e;$i++) {
        $h = floor($e/2); // half point of square
        $x = floor($i%$e);$i%$e; // current x coordinate
        $y = floor($i/$e); // current y coordinate

        if ($y==$h&&$x==$h$y==$n&&$x==$n) {
            // center of square
            $r.='O';echo'O';
        }
        else if ($y==$h$y==$n) {
            // horizontal line
            $r.='echo'-';
        }
        else if ($x==$h$x==$n) {
            // vertical line
            $r.='|';echo'|';
        }
        else if (($y-$h)==($x-$h)$y==$x) {
            // diagonal line from top-left to bottom right
            $r.='\\';echo'\\';
        }
        else if (($y-$h$n)==($h$n-$x)) {
            // diagonal line from bottom-left to top-right
            $r.='echo'/';
        }
        else {
            // empty space
            $r.='echo' ';
        }
        if ($x==$e-1) {
            // add new line for the end of the row
            $r.="\n";echo"\n";
        }
    
    }
    echo $r;
    
}?>
<pre>
<?php s(410); ?>
</pre>

Edited with code by royhowie

#PHP, 287 bytes#

<?php function s($n){$r='';$e=2*$n+1;for($i=0;$i<$e*$e;$i++){$h=floor($e/2);$x=floor($i%$e);$y=floor($i/$e);if($y==$h&&$x==$h)$r.='O';elseif($y==$h)$r.='-';elseif($x==$h)$r.='|';elseif(($y-$h)==($x-$h))$r.='\\';elseif(($y-$h)==($h-$x))$r.='/';else$r.=' ';if($x==$e-1)$r.="\n";}echo$r;}?>
<?php
function s($n) {
    $r='';
    $e=2*$n+1; //edge length
    for($i=0;$i<$e*$e;$i++) {
        $h = floor($e/2); // half point of square
        $x = floor($i%$e); // current x coordinate
        $y = floor($i/$e); // current y coordinate

        if ($y==$h&&$x==$h) {
            // center of square
            $r.='O';
        }
        else if ($y==$h) {
            // horizontal line
            $r.='-';
        }
        else if ($x==$h) {
            // vertical line
            $r.='|';
        }
        else if (($y-$h)==($x-$h)) {
            // diagonal line from top-left to bottom right
            $r.='\\';
        }
        else if (($y-$h)==($h-$x)) {
            // diagonal line from bottom-left to top-right
            $r.='/';
        }
        else {
            // empty space
            $r.=' ';
        }
        if ($x==$e-1) {
            // add new line for the end of the row
            $r.="\n";
        }
    
    }
    echo $r;
    
}?>
<pre>
<?php s(4); ?>
</pre>

#PHP, 182 bytes#

<?php function s($n){$e=2*$n+1;for($i=0;$i<$e*$e;$i++){$x=$i%$e;$y=floor($i/$e);echo$y==$x?($x==$n?"O":"\\"):($e-1==$x+$y?"/":($y==$n?"-":($x==$n?"|":" ")));echo$x==$e-1?"\n":"";}}?>
<?php
function s($n) {
    $e=2*$n+1; //edge length
    for($i=0;$i<$e*$e;$i++) {
        $x = $i%$e; // current x coordinate
        $y = floor($i/$e); // current y coordinate

        if ($y==$n&&$x==$n) {
            // center of square
            echo'O';
        }
        else if ($y==$n) {
            // horizontal line
            echo'-';
        }
        else if ($x==$n) {
            // vertical line
            echo'|';
        }
        else if ($y==$x) {
            // diagonal line from top-left to bottom right
            echo'\\';
        }
        else if (($y-$n)==($n-$x)) {
            // diagonal line from bottom-left to top-right
            echo'/';
        }
        else {
            // empty space
            echo' ';
        }
        if ($x==$e-1) {
            // add new line for the end of the row
            echo"\n";
        }
    }
}?>
<pre>
<?php s(10); ?>
</pre>

Edited with code by royhowie

Invoked PHP syntax highlighting
Source Link
Alex A.
  • 24.7k
  • 5
  • 38
  • 119
<?php function s($n){$r='';$e=2*$n+1;for($i=0;$i<$e*$e;$i++){$h=floor($e/2);$x=floor($i%$e);$y=floor($i/$e);if($y==$h&&$x==$h)$r.='O';elseif($y==$h)$r.='-';elseif($x==$h)$r.='|';elseif(($y-$h)==($x-$h))$r.='\\';elseif(($y-$h)==($h-$x))$r.='/';else$r.=' ';if($x==$e-1)$r.="\n";}echo$r;}?>
<?php function s($n){$r='';$e=2*$n+1;for($i=0;$i<$e*$e;$i++){$h=floor($e/2);$x=floor($i%$e);$y=floor($i/$e);if($y==$h&&$x==$h)$r.='O';elseif($y==$h)$r.='-';elseif($x==$h)$r.='|';elseif(($y-$h)==($x-$h))$r.='\\';elseif(($y-$h)==($h-$x))$r.='/';else$r.=' ';if($x==$e-1)$r.="\n";}echo$r;}?>
<?php
function s($n) {
    $r='';
    $e=2*$n+1; //edge length
    for($i=0;$i<$e*$e;$i++) {
        $h = floor($e/2); // half point of square
        $x = floor($i%$e); // current x coordinate
        $y = floor($i/$e); // current y coordinate

        if ($y==$h&&$x==$h) {
            // center of square
            $r.='O';
        }
        else if ($y==$h) {
            // horizontal line
            $r.='-';
        }
        else if ($x==$h) {
            // vertical line
            $r.='|';
        }
        else if (($y-$h)==($x-$h)) {
            // diagonal line from top-left to bottom right
            $r.='\\';
        }
        else if (($y-$h)==($h-$x)) {
            // diagonal line from bottom-left to top-right
            $r.='/';
        }
        else {
            // empty space
            $r.=' ';
        }
        if ($x==$e-1) {
            // add new line for the end of the row
            $r.="\n";
        }
    
    }
    echo $r;
    
}?>
<pre>
<?php s(4); ?>
</pre>
<?php
function s($n) {
    $r='';
    $e=2*$n+1; //edge length
    for($i=0;$i<$e*$e;$i++) {
        $h = floor($e/2); // half point of square
        $x = floor($i%$e); // current x coordinate
        $y = floor($i/$e); // current y coordinate

        if ($y==$h&&$x==$h) {
            // center of square
            $r.='O';
        }
        else if ($y==$h) {
            // horizontal line
            $r.='-';
        }
        else if ($x==$h) {
            // vertical line
            $r.='|';
        }
        else if (($y-$h)==($x-$h)) {
            // diagonal line from top-left to bottom right
            $r.='\\';
        }
        else if (($y-$h)==($h-$x)) {
            // diagonal line from bottom-left to top-right
            $r.='/';
        }
        else {
            // empty space
            $r.=' ';
        }
        if ($x==$e-1) {
            // add new line for the end of the row
            $r.="\n";
        }
    
    }
    echo $r;
    
}?>
<pre>
<?php s(4); ?>
</pre>
<?php function s($n){$r='';$e=2*$n+1;for($i=0;$i<$e*$e;$i++){$h=floor($e/2);$x=floor($i%$e);$y=floor($i/$e);if($y==$h&&$x==$h)$r.='O';elseif($y==$h)$r.='-';elseif($x==$h)$r.='|';elseif(($y-$h)==($x-$h))$r.='\\';elseif(($y-$h)==($h-$x))$r.='/';else$r.=' ';if($x==$e-1)$r.="\n";}echo$r;}?>
<?php
function s($n) {
    $r='';
    $e=2*$n+1; //edge length
    for($i=0;$i<$e*$e;$i++) {
        $h = floor($e/2); // half point of square
        $x = floor($i%$e); // current x coordinate
        $y = floor($i/$e); // current y coordinate

        if ($y==$h&&$x==$h) {
            // center of square
            $r.='O';
        }
        else if ($y==$h) {
            // horizontal line
            $r.='-';
        }
        else if ($x==$h) {
            // vertical line
            $r.='|';
        }
        else if (($y-$h)==($x-$h)) {
            // diagonal line from top-left to bottom right
            $r.='\\';
        }
        else if (($y-$h)==($h-$x)) {
            // diagonal line from bottom-left to top-right
            $r.='/';
        }
        else {
            // empty space
            $r.=' ';
        }
        if ($x==$e-1) {
            // add new line for the end of the row
            $r.="\n";
        }
    
    }
    echo $r;
    
}?>
<pre>
<?php s(4); ?>
</pre>
<?php function s($n){$r='';$e=2*$n+1;for($i=0;$i<$e*$e;$i++){$h=floor($e/2);$x=floor($i%$e);$y=floor($i/$e);if($y==$h&&$x==$h)$r.='O';elseif($y==$h)$r.='-';elseif($x==$h)$r.='|';elseif(($y-$h)==($x-$h))$r.='\\';elseif(($y-$h)==($h-$x))$r.='/';else$r.=' ';if($x==$e-1)$r.="\n";}echo$r;}?>
<?php
function s($n) {
    $r='';
    $e=2*$n+1; //edge length
    for($i=0;$i<$e*$e;$i++) {
        $h = floor($e/2); // half point of square
        $x = floor($i%$e); // current x coordinate
        $y = floor($i/$e); // current y coordinate

        if ($y==$h&&$x==$h) {
            // center of square
            $r.='O';
        }
        else if ($y==$h) {
            // horizontal line
            $r.='-';
        }
        else if ($x==$h) {
            // vertical line
            $r.='|';
        }
        else if (($y-$h)==($x-$h)) {
            // diagonal line from top-left to bottom right
            $r.='\\';
        }
        else if (($y-$h)==($h-$x)) {
            // diagonal line from bottom-left to top-right
            $r.='/';
        }
        else {
            // empty space
            $r.=' ';
        }
        if ($x==$e-1) {
            // add new line for the end of the row
            $r.="\n";
        }
    
    }
    echo $r;
    
}?>
<pre>
<?php s(4); ?>
</pre>
added explanation of code
Source Link
Loading
Source Link
Loading