Forked from ad3n/gist:e325ffa730bdb07c6b570394c1ccfd71
Created
April 11, 2016 09:08
-
-
Save 3Shoka/62ca611155461fe7c81c486ffb4d7a85 to your computer and use it in GitHub Desktop.
[PHPID QUIZ] Jajar Genjang OOP
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
<?php | |
class JajarGenjang | |
{ | |
private $panjang = 18; | |
private $increment = 1; | |
private function createLine() | |
{ | |
$limit = $this->panjang + $this->increment; | |
for ($i = $this->increment; $i <= $limit; $i++) { | |
echo '|'; | |
} | |
echo PHP_EOL; | |
} | |
private function createEmptyLine() | |
{ | |
$limit = $this->panjang + $this->increment; | |
for ($i = $this->increment; $i <= $limit; $i++) { | |
if ($this->increment === $i || $limit === $i) { | |
echo '|'; | |
} else { | |
echo ' '; | |
} | |
} | |
echo PHP_EOL; | |
$this->increment++; | |
} | |
private function createSpace() | |
{ | |
for ($i = 1; $i <= $this->increment; $i++) { | |
echo ' '; | |
} | |
} | |
private function printILovePhp() | |
{ | |
$string = 'ILOVEPHP'; | |
$printed = false; | |
$length = strlen($string); | |
$limit = $this->panjang + $this->increment; | |
for ($i = $this->increment; $i <= $limit; $i++) { | |
if ($this->increment === $i || $limit === $i) { | |
echo '|'; | |
} elseif (!$printed && $i === (int) round(($limit - ($length - 4)) / 2)) { | |
echo $string; | |
$printed = true; | |
$i += $length - 1; | |
} else { | |
echo ' '; | |
} | |
} | |
echo PHP_EOL; | |
$this->increment++; | |
} | |
public function draw() | |
{ | |
$this->createLine(); | |
$this->createSpace(); | |
$this->createEmptyLine(); | |
$this->createSpace(); | |
$this->printILovePhp(); | |
$this->createSpace(); | |
$this->printILovePhp(); | |
$this->createSpace(); | |
$this->printILovePhp(); | |
$this->createSpace(); | |
$this->printILovePhp(); | |
$this->createSpace(); | |
$this->createEmptyLine(); | |
$this->createSpace(); | |
$this->createLine(); | |
} | |
} | |
$jajarGenjang = new JajarGenjang(); | |
$jajarGenjang->draw(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment