/robot

Robot

Primary LanguageElixirMIT LicenseMIT

Given a robot which can only move in four directions, UP(U), DOWN(D), LEFT(L), RIGHT(R).

Given a string consisting of instructions to move. Output the coordinates of a robot after executing the instructions. Initial position of robot is at origin(0, 0).

If the robot exceeds the established limits it will travel to the negative limit of the maximum position.

The maximum position by default is set to 5, but it can be changed. For more details, see Robot.setBounds/2.

Getting Started

A real life example:

# Initialize a Robot process.
iex(1)> {:ok, pid} = Robot.start(self())


# Change the default Robot bounds by x: 3 and y: 6.
iex(2)> :ok = Robot.setBounds(pid, {3, 6})


# Move the Robot.
iex(3)> :ok = Robot.move(pid, "RRRRUUUUUUU")


# Receive the next Robot's move.
iex(4)> %{x: 1, y: 0} = receive do {:robot_position_changed, position} -> position end


# Get the current Robot position.
iex(5)> %Robot.Models.Point{
  bounds: %{x: 3, y: 6},
  subscribers: [_caller_pid],
  x: -3,
  y: -6
} = Robot.get(pid)


# Stop the Robot process.
iex(6)> :ok = Robot.stop(pid)

get(pid)

Get the current position of the Robot.

move(pid, command)

Move the Robot to the desired location according to the given commands.

setBounds(pid, arg)

Change the Robot bounds by a given tuple {x, y}.

start()

start(caller)

Initialize a Robot process.

stop(pid)

Stop a Robot process.

Link to this function

get(pid)

View Source

Specs

get(pid()) :: %Robot.Models.Point{
  bounds: term(),
  subscribers: term(),
  x: term(),
  y: term()
}

Get the current position of the Robot.

Returns %Robot.Models.Point{}.

Examples

iex> {:ok, pid} = Robot.start()
iex> Robot.get(pid)
%Robot.Models.Point{x: 0, y: 0}

Link to this function

move(pid, command)

View Source

Specs

move(pid(), String.t()) :: :ok

Move the Robot to the desired location according to the given commands.

Returns :ok.

Examples

iex> {:ok, pid} = Robot.start()
iex> Robot.move(pid, "UUUDR")
:ok

Link to this function

setBounds(pid, arg)

View Source

Specs

setBounds(pid(), tuple()) :: :ok

Change the Robot bounds by a given tuple {x, y}.

Returns :ok.

Examples

iex> {:ok, pid} = Robot.start()
iex> :ok = Robot.setBounds(pid, {10, 10})
:ok

Link to this function

start()

View Source

Link to this function

start(caller)

View Source

Specs

start(pid()) :: {:ok, pid()}

Initialize a Robot process.

Returns {:ok, #PID<0.162.0>}.

Examples

iex> {:ok, _pid} = Robot.start()

# With a subscriber
iex> {:ok, _pid} = Robot.start(self())

Link to this function

stop(pid)

View Source

Specs

stop(pid()) :: :ok

Stop a Robot process.

Returns :ok.

Examples

iex> {:ok, pid} = Robot.start()
iex> :ok = Robot.stop(pid)
:ok

Installation

If available in Hex, the package can be installed by adding robot to your list of dependencies in mix.exs:

def deps do
  [
    {:robot, "~> 0.1.0"}
  ]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/robot.

Test

To test without concurrency tests, use the following command:

  mix test

To test with concurrency tests, run:

  mix test --include concurrency

Author

Yamil Díaz Aguirre

https://github.com/Yamilquery

yamilquery@gmail.com