<?php
function towerBuild( $n ) {
$tower = new Tower($n);
return $tower->build();
}
class Tower {
protected $height;
protected $tower = [];
public function __construct(int $height) {
$this->height = $height;
}
public function build() {
for ( $i = 1; $i <= $this->height; $i++ ) {
$this->tower[] = $this->addFloor($i);
}
print_r($this->tower);
return $this->tower;
}
protected function addFloor(int $floor) {
$result = "";
$result.= $this->addMargin($floor);
$result.= $this->addBricks($floor);
$result.= $this->addMargin($floor);
return $result;
}
public function addMargin($floor) { return str_repeat("_", $this->height - $floor); }
public function addBricks($floor) { return str_repeat("*", 2 * $floor - 1); }
}
towerBuild(4);
function tower_builder(int $n) {
$height = $n;
$tower = array();
for ($i = 1; $i <= $height; $i++ ) {
$str = "";
if ( $height == 1 ) { return "*"; }
$str.= str_repeat(' ', $height - $i);
$str.= str_repeat('*', $i * 2 - 1);
$str.= str_repeat(' ', $height - $i);
$tower[] .= $str;
}
return $tower;
}
$vara = tower_builder(4);
Leave A Comment?