Avatar of artemkorsakov

artemkorsakov's solution

to Robot Simulator in the C# Track

Published at Apr 27 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Write a robot simulator.

A robot factory's test facility needs a program to verify robot movements.

The robots have three possible movements:

  • turn right
  • turn left
  • advance

Robots are placed on a hypothetical infinite grid, facing a particular direction (north, east, south, or west) at a set of {x,y} coordinates, e.g., {3,8}, with coordinates increasing to the north and east.

The robot then receives a number of instructions, at which point the testing facility verifies the robot's new position, and in which direction it is pointing.

  • The letter-string "RAALAL" means:
    • Turn right
    • Advance twice
    • Turn left
    • Advance once
    • Turn left yet again
  • Say a robot starts at {7, 3} facing north. Then running this stream of instructions should leave it at {9, 4} facing west.

Running the tests

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit RobotSimulator.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

Source

Inspired by an interview question at a famous company.

RobotSimulatorTest.cs

// This file was auto-generated based on version 3.1.0 of the canonical data.

using Xunit;

public class RobotSimulatorTest
{
    [Fact]
    public void A_robot_is_created_with_a_position_and_a_direction_robots_are_created_with_a_position_and_direction()
    {
        var sut = new RobotSimulator(Direction.North, 0, 0);
        Assert.Equal(Direction.North, sut.Direction);
        Assert.Equal(0, sut.X);
        Assert.Equal(0, sut.Y);
    }

    [Fact(Skip = "Remove to run test")]
    public void A_robot_is_created_with_a_position_and_a_direction_negative_positions_are_allowed()
    {
        var sut = new RobotSimulator(Direction.South, -1, -1);
        Assert.Equal(Direction.South, sut.Direction);
        Assert.Equal(-1, sut.X);
        Assert.Equal(-1, sut.Y);
    }

    [Fact(Skip = "Remove to run test")]
    public void Rotates_the_robots_direction_90_degrees_clockwise_changes_the_direction_from_north_to_east()
    {
        var sut = new RobotSimulator(Direction.North, 0, 0);
        sut.Move("R");
        Assert.Equal(Direction.East, sut.Direction);
        Assert.Equal(0, sut.X);
        Assert.Equal(0, sut.Y);
    }

    [Fact(Skip = "Remove to run test")]
    public void Rotates_the_robots_direction_90_degrees_clockwise_changes_the_direction_from_east_to_south()
    {
        var sut = new RobotSimulator(Direction.East, 0, 0);
        sut.Move("R");
        Assert.Equal(Direction.South, sut.Direction);
        Assert.Equal(0, sut.X);
        Assert.Equal(0, sut.Y);
    }

    [Fact(Skip = "Remove to run test")]
    public void Rotates_the_robots_direction_90_degrees_clockwise_changes_the_direction_from_south_to_west()
    {
        var sut = new RobotSimulator(Direction.South, 0, 0);
        sut.Move("R");
        Assert.Equal(Direction.West, sut.Direction);
        Assert.Equal(0, sut.X);
        Assert.Equal(0, sut.Y);
    }

    [Fact(Skip = "Remove to run test")]
    public void Rotates_the_robots_direction_90_degrees_clockwise_changes_the_direction_from_west_to_north()
    {
        var sut = new RobotSimulator(Direction.West, 0, 0);
        sut.Move("R");
        Assert.Equal(Direction.North, sut.Direction);
        Assert.Equal(0, sut.X);
        Assert.Equal(0, sut.Y);
    }

    [Fact(Skip = "Remove to run test")]
    public void Rotates_the_robots_direction_90_degrees_counter_clockwise_changes_the_direction_from_north_to_west()
    {
        var sut = new RobotSimulator(Direction.North, 0, 0);
        sut.Move("L");
        Assert.Equal(Direction.West, sut.Direction);
        Assert.Equal(0, sut.X);
        Assert.Equal(0, sut.Y);
    }

    [Fact(Skip = "Remove to run test")]
    public void Rotates_the_robots_direction_90_degrees_counter_clockwise_changes_the_direction_from_west_to_south()
    {
        var sut = new RobotSimulator(Direction.West, 0, 0);
        sut.Move("L");
        Assert.Equal(Direction.South, sut.Direction);
        Assert.Equal(0, sut.X);
        Assert.Equal(0, sut.Y);
    }

    [Fact(Skip = "Remove to run test")]
    public void Rotates_the_robots_direction_90_degrees_counter_clockwise_changes_the_direction_from_south_to_east()
    {
        var sut = new RobotSimulator(Direction.South, 0, 0);
        sut.Move("L");
        Assert.Equal(Direction.East, sut.Direction);
        Assert.Equal(0, sut.X);
        Assert.Equal(0, sut.Y);
    }

    [Fact(Skip = "Remove to run test")]
    public void Rotates_the_robots_direction_90_degrees_counter_clockwise_changes_the_direction_from_east_to_north()
    {
        var sut = new RobotSimulator(Direction.East, 0, 0);
        sut.Move("L");
        Assert.Equal(Direction.North, sut.Direction);
        Assert.Equal(0, sut.X);
        Assert.Equal(0, sut.Y);
    }

    [Fact(Skip = "Remove to run test")]
    public void Moves_the_robot_forward_1_space_in_the_direction_it_is_pointing_increases_the_y_coordinate_one_when_facing_north()
    {
        var sut = new RobotSimulator(Direction.North, 0, 0);
        sut.Move("A");
        Assert.Equal(Direction.North, sut.Direction);
        Assert.Equal(0, sut.X);
        Assert.Equal(1, sut.Y);
    }

    [Fact(Skip = "Remove to run test")]
    public void Moves_the_robot_forward_1_space_in_the_direction_it_is_pointing_decreases_the_y_coordinate_by_one_when_facing_south()
    {
        var sut = new RobotSimulator(Direction.South, 0, 0);
        sut.Move("A");
        Assert.Equal(Direction.South, sut.Direction);
        Assert.Equal(0, sut.X);
        Assert.Equal(-1, sut.Y);
    }

    [Fact(Skip = "Remove to run test")]
    public void Moves_the_robot_forward_1_space_in_the_direction_it_is_pointing_increases_the_x_coordinate_by_one_when_facing_east()
    {
        var sut = new RobotSimulator(Direction.East, 0, 0);
        sut.Move("A");
        Assert.Equal(Direction.East, sut.Direction);
        Assert.Equal(1, sut.X);
        Assert.Equal(0, sut.Y);
    }

    [Fact(Skip = "Remove to run test")]
    public void Moves_the_robot_forward_1_space_in_the_direction_it_is_pointing_decreases_the_x_coordinate_by_one_when_facing_west()
    {
        var sut = new RobotSimulator(Direction.West, 0, 0);
        sut.Move("A");
        Assert.Equal(Direction.West, sut.Direction);
        Assert.Equal(-1, sut.X);
        Assert.Equal(0, sut.Y);
    }

    [Fact(Skip = "Remove to run test")]
    public void Where_r_turn_right_l_turn_left_and_a_advance_the_robot_can_follow_a_series_of_instructions_and_end_up_with_the_correct_position_and_direction_instructions_to_move_east_and_north_from_readme()
    {
        var sut = new RobotSimulator(Direction.North, 7, 3);
        sut.Move("RAALAL");
        Assert.Equal(Direction.West, sut.Direction);
        Assert.Equal(9, sut.X);
        Assert.Equal(4, sut.Y);
    }

    [Fact(Skip = "Remove to run test")]
    public void Where_r_turn_right_l_turn_left_and_a_advance_the_robot_can_follow_a_series_of_instructions_and_end_up_with_the_correct_position_and_direction_instructions_to_move_west_and_north()
    {
        var sut = new RobotSimulator(Direction.North, 0, 0);
        sut.Move("LAAARALA");
        Assert.Equal(Direction.West, sut.Direction);
        Assert.Equal(-4, sut.X);
        Assert.Equal(1, sut.Y);
    }

    [Fact(Skip = "Remove to run test")]
    public void Where_r_turn_right_l_turn_left_and_a_advance_the_robot_can_follow_a_series_of_instructions_and_end_up_with_the_correct_position_and_direction_instructions_to_move_west_and_south()
    {
        var sut = new RobotSimulator(Direction.East, 2, -7);
        sut.Move("RRAAAAALA");
        Assert.Equal(Direction.South, sut.Direction);
        Assert.Equal(-3, sut.X);
        Assert.Equal(-8, sut.Y);
    }

    [Fact(Skip = "Remove to run test")]
    public void Where_r_turn_right_l_turn_left_and_a_advance_the_robot_can_follow_a_series_of_instructions_and_end_up_with_the_correct_position_and_direction_instructions_to_move_east_and_north()
    {
        var sut = new RobotSimulator(Direction.South, 8, 4);
        sut.Move("LAAARRRALLLL");
        Assert.Equal(Direction.North, sut.Direction);
        Assert.Equal(11, sut.X);
        Assert.Equal(5, sut.Y);
    }
}
public enum Direction
{
    North,
    East,
    South,
    West
}

public class RobotSimulator
{
    public RobotSimulator(Direction direction, int x, int y)
    {
        Direction = direction;
        X = x;
        Y = y;
    }

    public Direction Direction { get; private set; }

    public int X { get; private set; }

    public int Y { get; private set; }

    public void Move(string instructions)
    {
        foreach (var key in instructions.ToCharArray())
        {
            Execute(key);
        }
    }

    private void Execute(char key)
    {
        switch (key)
        {
            case 'R':
                TurnRight();
                break;
            case 'L':
                TurnLeft();
                break;
            case 'A':
                Advance();
                break;
        }
    }

    private void TurnRight() => Direction = (Direction)(((int)Direction + 1) % 4);

    private void TurnLeft() => Direction = (Direction)(((int)Direction + 3) % 4);

    private void Advance()
    {
        switch (Direction)
        {
            case Direction.North:
                Y++;
                break;
            case Direction.East:
                X++;
                break;
            case Direction.South:
                Y--;
                break;
            case Direction.West:
                X--;
                break;
        }
    }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?