Esempio n. 1
0
 public function testMovement()
 {
     $d = new RoverPosition(new Coordinate(1, 2), 'E');
     $newPosition = $d->evalCommand('M');
     $this->assertTrue($newPosition->getCoordinates()->getX() == 2 && $newPosition->getCoordinates()->getY() == 2);
     $d = new RoverPosition(new Coordinate(1, 2), 'W');
     $newPosition = $d->evalCommand('M');
     $this->assertTrue($newPosition->getCoordinates()->getX() == 0 && $newPosition->getCoordinates()->getY() == 2);
     $d = new RoverPosition(new Coordinate(1, 2), 'S');
     $newPosition = $d->evalCommand('M');
     $this->assertTrue($newPosition->getCoordinates()->getX() == 1 && $newPosition->getCoordinates()->getY() == 1);
     $d = new RoverPosition(new Coordinate(1, 2), 'N');
     $newPosition = $d->evalCommand('M');
     $this->assertTrue($newPosition->getCoordinates()->getX() == 1 && $newPosition->getCoordinates()->getY() == 3);
 }
Esempio n. 2
0
 /**
  * Eval command against current position
  * @param $command
  *
  * @return RoverPosition new Position
  */
 public function evalCommand($command)
 {
     $newPosition = new RoverPosition(new Coordinate($this->_coordinates->getX(), $this->_coordinates->getY()), $this->getDirection());
     if ($command == CommandSequence::TURN_LEFT || $command == CommandSequence::TURN_RIGHT) {
         $nextDirectionIndex = $this->_getCurrentDirectionIndex();
         if ($command == CommandSequence::TURN_RIGHT) {
             if ($nextDirectionIndex >= 3) {
                 $nextDirectionIndex = 0;
             } else {
                 $nextDirectionIndex++;
             }
         } else {
             if ($nextDirectionIndex <= 0) {
                 $nextDirectionIndex = 3;
             } else {
                 $nextDirectionIndex--;
             }
         }
         $newPosition->changeDirection($this->_getDirection($nextDirectionIndex));
     } else {
         if ($command == CommandSequence::MOVE_FORWARD) {
             if ($newPosition->getDirection() == self::DIRECTION_EAST) {
                 $newPosition->getCoordinates()->moveAxis('x', 1);
             } else {
                 if ($newPosition->getDirection() == self::DIRECTION_WEST) {
                     $newPosition->getCoordinates()->moveAxis('x', -1);
                 } else {
                     if ($newPosition->getDirection() == self::DIRECTION_SOUTH) {
                         $newPosition->getCoordinates()->moveAxis('y', -1);
                     } else {
                         if ($newPosition->getDirection() == self::DIRECTION_NORTH) {
                             $newPosition->getCoordinates()->moveAxis('y', 1);
                         }
                     }
                 }
             }
         }
     }
     return $newPosition;
 }
Esempio n. 3
0
 private function _formatPosition(RoverPosition $position)
 {
     return sprintf('%d %d %s', $position->getCoordinates()->getX(), $position->getCoordinates()->getY(), $position->getDirection());
 }