You'll understand
<?php
class Bunny {
private $x;
private $y;
private $direction; // left | right | up | down
private $distance;
private $hopper;
private $hops_hopped;
const TURN_OPTIONS = ['left', 'right', 'up', 'down'];
public function __construct($goal, $name) {
$this->distance = $goal;
$this->hopper = $name;
$this->x = 0;
$this->y = 0;
$this->direction = 'right';
$this->hops_hopped = 0;
}
public function run() {
$this->hop(5);
$this->face_new_direction('top');
$this->hop(5);
$this->face_new_direction('right');
$this->hop(200);
echo $this->hopper . ' is at ' . $this->x . ' in: ' . $this->hops_hopped;
}
public function execute_hops(int $hops) {
$this->hop($hops);
return [$this->x, $this->y];
}
public function execute_turn(string $turn, $hop = false) {
if ($this->bunny_is_done()) return $this->get_resposnse_message();
if (!in_array($turn, Bunny::TURN_OPTIONS)) return 'This isn\'t 3D land';
$this->face_new_direction($turn);
if ($hop) {
$this->hop();
}
return $this->get_resposnse_message();
}
private function hop($hops = 1) {
for ($i=0; $i <= $hops ; $i++) {
if ($this->x >= $this->distance) {
return $this;
}
$this->x += $this->get_horizontal_hops(1);
$this->y += $this->get_vertical_hops(1);
$this->hops_hopped += 1;
}
return $this;
}
private function face_new_direction($direction) {
$this->direction = $direction;
}
private function get_horizontal_hops($number_of_hops) {
// right is +
if ($this->direction === 'right') {
return $number_of_hops;
} else if ($this->direction === 'left') {
return ((int) $number_of_hops) * -1;
}
return 0;
}
private function get_vertical_hops($number_of_hops) {
// right is +
if ($this->direction === 'top') {
return $number_of_hops;
} else if ($this->direction === 'down') {
return ((int) $number_of_hops) * -1;
}
return 0;
}
private function bunny_is_done() {
return $this->x >= $this->distance;
}
private function get_resposnse_message() {
return $this->hopper . ' is at ' . $this->x . ' in: ' . $this->hops_hopped . ' facing ' . $this->direction;
}
}
$z = new Bunny(7, 'Rabbit');
$z->run();