Sha256: 37f764eab988cfede0e2b12bd122d1f9fa0a73c58a460b122637ab34eda4d200

Contents?: true

Size: 1.92 KB

Versions: 22

Compression:

Stored size: 1.92 KB

Contents

-module( example ).

-export([advance/1,
         control/2,
         create/0,
         direction/1,
         left/1,
         place/3,
         position/1,
         right/1,
         test_version/0]).

advance( Robot ) -> Robot ! advance.

control( Robot, String ) ->
  Funs = lists:flatten( [control_function(X) || X <- String] ),
  [X(Robot) || X <- Funs].

create() -> erlang:spawn( fun() -> loop( undefined, {undefined, undefined} ) end ).

left( Robot ) -> Robot ! {turn, left}.

direction( Robot ) -> request( Robot, direction ).

place( Robot, Direction, Position ) -> Robot ! {place, Direction, Position}.

position( Robot ) -> request( Robot, position ).

right( Robot ) -> Robot ! {turn, right}.

test_version() ->
    1.



control_function( $A ) -> fun advance/1;
control_function( $L ) -> fun left/1;
control_function( $R ) -> fun right/1;
control_function( _C ) -> [].

loop( Direction, Position ) ->
  receive
    advance ->
      loop( Direction, new_position(Direction, Position) );
    {direction, Pid} ->
      Pid ! {direction, Direction},
      loop( Direction, Position );
    {turn, Turn} ->
      loop( new_direction(Direction, Turn), Position );
    {place, New_direction, New_position} ->
      loop( New_direction, New_position );
    {position, Pid} ->
      Pid ! {position, Position},
      loop( Direction, Position )
  end.

new_direction( north, right ) -> east;
new_direction( south, right ) -> west;
new_direction( east, right ) -> south;
new_direction( west, right ) -> north;
new_direction( north, left ) -> west;
new_direction( south, left ) -> east;
new_direction( east, left ) -> north;
new_direction( west, left ) -> south.

new_position( north, {X, Y} ) -> {X, Y + 1};
new_position( south, {X, Y} ) -> {X, Y - 1};
new_position( east, {X, Y} ) -> {X + 1, Y};
new_position( west, {X, Y} ) -> {X - 1, Y}.

request( Robot, Request ) ->
  Robot ! {Request, erlang:self()},
  receive
    {Request, Answer} -> Answer
  end.

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
trackler-2.0.8.7 tracks/erlang/exercises/robot-simulator/src/example.erl
trackler-2.0.8.6 tracks/erlang/exercises/robot-simulator/src/example.erl
trackler-2.0.8.5 tracks/erlang/exercises/robot-simulator/src/example.erl
trackler-2.0.8.4 tracks/erlang/exercises/robot-simulator/src/example.erl
trackler-2.0.8.3 tracks/erlang/exercises/robot-simulator/src/example.erl
trackler-2.0.8.2 tracks/erlang/exercises/robot-simulator/src/example.erl
trackler-2.0.8.1 tracks/erlang/exercises/robot-simulator/src/example.erl
trackler-2.0.7.0 tracks/erlang/exercises/robot-simulator/src/example.erl
trackler-2.0.6.44 tracks/erlang/exercises/robot-simulator/src/example.erl
trackler-2.0.6.43 tracks/erlang/exercises/robot-simulator/src/example.erl
trackler-2.0.6.42 tracks/erlang/exercises/robot-simulator/src/example.erl
trackler-2.0.6.41 tracks/erlang/exercises/robot-simulator/src/example.erl
trackler-2.0.6.40 tracks/erlang/exercises/robot-simulator/src/example.erl
trackler-2.0.6.39 tracks/erlang/exercises/robot-simulator/src/example.erl
trackler-2.0.6.38 tracks/erlang/exercises/robot-simulator/src/example.erl
trackler-2.0.6.37 tracks/erlang/exercises/robot-simulator/src/example.erl
trackler-2.0.6.36 tracks/erlang/exercises/robot-simulator/src/example.erl
trackler-2.0.6.35 tracks/erlang/exercises/robot-simulator/src/example.erl
trackler-2.0.6.34 tracks/erlang/exercises/robot-simulator/src/example.erl
trackler-2.0.6.33 tracks/erlang/exercises/robot-simulator/src/example.erl