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
.
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 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.
get(pid()) :: %Robot.Models.Point{
bounds: term(),
subscribers: term(),
x: term(),
y: term()
}
Get the current position of the Robot.
Returns %Robot.Models.Point{}
.
iex> {:ok, pid} = Robot.start()
iex> Robot.get(pid)
%Robot.Models.Point{x: 0, y: 0}
move(pid(), String.t()) :: :ok
Move the Robot to the desired location according to the given commands.
Returns :ok
.
iex> {:ok, pid} = Robot.start()
iex> Robot.move(pid, "UUUDR")
:ok
setBounds(pid(), tuple()) :: :ok
Change the Robot bounds by a given tuple {x, y}
.
Returns :ok
.
iex> {:ok, pid} = Robot.start()
iex> :ok = Robot.setBounds(pid, {10, 10})
:ok
start(pid()) :: {:ok, pid()}
Initialize a Robot process.
Returns {:ok, #PID<0.162.0>}
.
iex> {:ok, _pid} = Robot.start()
# With a subscriber
iex> {:ok, _pid} = Robot.start(self())
stop(pid()) :: :ok
Stop a Robot process.
Returns :ok
.
iex> {:ok, pid} = Robot.start()
iex> :ok = Robot.stop(pid)
:ok
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.
To test without concurrency tests, use the following command:
mix test
To test with concurrency tests, run:
mix test --include concurrency
Yamil Díaz Aguirre