Skip to content

Instantly share code, notes, and snippets.

@ianmustafa
Last active April 22, 2021 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianmustafa/61c1f0c12b89eae047d9b2299997cf85 to your computer and use it in GitHub Desktop.
Save ianmustafa/61c1f0c12b89eae047d9b2299997cf85 to your computer and use it in GitHub Desktop.
Bledheg generator
#! /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