Robot
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)
Summary
Functions
Get the current position of the Robot.
Move the Robot to the desired location according to the given commands.
Change the Robot bounds by a given tuple {x, y}.
Initialize a Robot process.
Stop a Robot process.
Functions
get(pid)
Specs
get(pid()) :: %Robot.Models.Point{bounds: 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}move(pid, command)
Specs
move(pid(), String.t()) :: :okMove the Robot to the desired location according to the given commands.
Returns :ok.
Examples
iex> {:ok, pid} = Robot.start()
iex> Robot.move(pid, "UUUDR")
:oksetBounds(pid, arg)
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})
:okstart()
Specs
start() :: {:ok, pid()}Initialize a Robot process.
Returns {:ok, #PID<0.162.0>}.
Examples
iex> {:ok, _pid} = Robot.start()stop(pid)
Specs
stop(pid()) :: :okStop a Robot process.
Returns :ok.
Examples
iex> {:ok, pid} = Robot.start()
iex> :ok = Robot.stop(pid)
:okInstallation
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"}
]
endDocumentation 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 testTo test with concurrency tests, run:
mix test --include concurrencyAuthor
Yamil Díaz Aguirre
yamilquery@gmail.com