Last active
April 22, 2021 14:45
-
-
Save ianmustafa/61c1f0c12b89eae047d9b2299997cf85 to your computer and use it in GitHub Desktop.
Bledheg generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env php | |
<?php | |
$input = $argv[1] ?? null; | |
if (is_null($input) || $input < -5 || $input > 20) { | |
exit("Bilangan bulat tidak termasuk dalam rentang yang diizinkan: -5 s/d 20!\n"); | |
} | |
if ($input < 1) { | |
exit(); | |
} | |
$totalLoop = (2 * $input) - 1; | |
foreach (range(1, $totalLoop) as $i) { | |
$posY = $i - $input; | |
foreach (range(1, $totalLoop) as $j) { | |
switch ($posY <=> 0) { | |
case -1: | |
$posX = $i + $j - $input - 1; | |
if ($posX < 0 || $j > $input || abs($posX % 2) === 1) { | |
echo ' '; | |
break; | |
} | |
if (abs($posX % 4) === 0) { | |
echo 'x'; | |
break; | |
} | |
echo 'o'; | |
break; | |
case 1: | |
$posX = $i + $j - ($input * 3) + 1; | |
if ($posX > 0 || $j < $input || abs($posX % 2) === 1) { | |
echo ' '; | |
break; | |
} | |
if (abs($posX % 4) === 0) { | |
echo 'x'; | |
break; | |
} | |
echo 'o'; | |
break; | |
case 0: | |
$posX = $input - abs($j - $input); | |
if ($posX % 2 === 0) { | |
echo ' '; | |
break; | |
} | |
if ($posX % 4 === 1) { | |
echo 'x'; | |
break; | |
} | |
echo 'o'; | |
break; | |
} | |
} | |
echo "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment