# tzarick's solution

## to Robot Simulator in the C Track

Published at Jul 13 2018 · 3 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code 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

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
• Turn left
• 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.

## Getting Started

Make sure you have read the C page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

## Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development.

The included makefile can be used to create and run the tests using the `test` task.

``````make test
``````

Create just the functions you need to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

## Source

Inspired by an interview question at a famous company.

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### test_robot_simulator.c

``````#include "../src/robot_simulator.h"
#include "vendor/unity.h"

void setUp(void)
{
}

void tearDown(void)
{
}

// Test Helper Function
void confirm_position(robot_grid_status_t * expected,
robot_grid_status_t * actual)
{
TEST_ASSERT_EQUAL(expected->bearing, actual->bearing);
TEST_ASSERT_EQUAL(expected->grid.x_position, actual->grid.x_position);
TEST_ASSERT_EQUAL(expected->grid.y_position, actual->grid.y_position);
}

// Tests...
void test_init(void)
{
robot_grid_status_t expected =
{ DEFAULT_BEARING, {DEFAULT_X_COORDINATE, DEFAULT_Y_COORDINATE} };
robot_grid_status_t actual = robot_init();

confirm_position(&expected, &actual);
}

{
TEST_IGNORE();               // delete this line to run test
robot_grid_status_t expected =
{ DEFAULT_BEARING, {DEFAULT_X_COORDINATE, DEFAULT_Y_COORDINATE} };
robot_grid_status_t actual =
robot_init_with_position(99, DEFAULT_X_COORDINATE, DEFAULT_Y_COORDINATE);

confirm_position(&expected, &actual);
}

void test_init_with_negative_positions(void)
{
TEST_IGNORE();
robot_grid_status_t expected = { HEADING_SOUTH, {-1, -1} };
robot_grid_status_t actual = robot_init_with_position(HEADING_SOUTH, -1, -1);

confirm_position(&expected, &actual);
}

void test_turn_right(void)
{
TEST_IGNORE();
robot_grid_status_t expected = { HEADING_EAST, {0, 0} };
robot_grid_status_t actual = robot_init();

robot_turn_right(&actual);
confirm_position(&expected, &actual);

robot_turn_right(&actual);
confirm_position(&expected, &actual);

robot_turn_right(&actual);
confirm_position(&expected, &actual);

robot_turn_right(&actual);
confirm_position(&expected, &actual);
}

void test_turn_left(void)
{
TEST_IGNORE();
robot_grid_status_t expected = { HEADING_WEST, {0, 0} };
robot_grid_status_t actual = robot_init();

robot_turn_left(&actual);
confirm_position(&expected, &actual);

robot_turn_left(&actual);
confirm_position(&expected, &actual);

robot_turn_left(&actual);
confirm_position(&expected, &actual);

robot_turn_left(&actual);
confirm_position(&expected, &actual);
}

{
TEST_IGNORE();
robot_grid_status_t expected = { HEADING_NORTH, {0, 1} };
robot_grid_status_t actual = robot_init();

confirm_position(&expected, &actual);
}

{
TEST_IGNORE();
robot_grid_status_t expected = { HEADING_EAST, {1, 0} };
robot_grid_status_t actual = robot_init_with_position(HEADING_EAST, 0, 0);

confirm_position(&expected, &actual);
}

{
TEST_IGNORE();
robot_grid_status_t expected = { HEADING_SOUTH, {0, -1} };
robot_grid_status_t actual = robot_init_with_position(HEADING_SOUTH, 0, 0);

confirm_position(&expected, &actual);
}

{
TEST_IGNORE();
robot_grid_status_t expected = { HEADING_WEST, {-1, 0} };
robot_grid_status_t actual = robot_init_with_position(HEADING_WEST, 0, 0);

confirm_position(&expected, &actual);
}

void test_simulate_move_west_and_north(void)
{
TEST_IGNORE();
robot_grid_status_t expected = { HEADING_WEST, {-4, 1} };
robot_grid_status_t actual = robot_init();

robot_simulator(&actual, "LAAARALA");
confirm_position(&expected, &actual);
}

void test_simulate_move_west_and_south(void)
{
TEST_IGNORE();
robot_grid_status_t expected = { HEADING_SOUTH, {-3, -8} };
robot_grid_status_t actual = robot_init_with_position(HEADING_EAST, 2, -7);

robot_simulator(&actual, "RRAAAAALA");
confirm_position(&expected, &actual);
}

void test_simulate_move_east_and_north(void)
{
TEST_IGNORE();
robot_grid_status_t expected = { HEADING_NORTH, {11, 5} };
robot_grid_status_t actual = robot_init_with_position(HEADING_SOUTH, 8, 4);

robot_simulator(&actual, "LAAARRRALLLL");
confirm_position(&expected, &actual);
}

int main(void)
{
UnityBegin("test/test_word_count.c");

RUN_TEST(test_init);
RUN_TEST(test_init_with_negative_positions);
RUN_TEST(test_turn_right);
RUN_TEST(test_turn_left);
RUN_TEST(test_simulate_move_west_and_north);
RUN_TEST(test_simulate_move_west_and_south);
RUN_TEST(test_simulate_move_east_and_north);

UnityEnd();

return 0;
}``````

### src/robot_simulator.c

``````/*!
* @file
* @brief Robot Controller
*
*
*/

#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include "robot_simulator.h"

#define for_each(listOfLetters) \
for (size_t i = 0; i < strlen(listOfLetters); i++)

int FilterBearingInput(int inputBearing) // helper function to make sure bearing direction is valid
{
if(inputBearing > 3 || inputBearing < 0)
{
inputBearing = 0; // default value = North = 0
}
return inputBearing;
}

RobotGridStatus_t robot_init(void)
{
RobotGridStatus_t robotInitialState;

RobotCoordinates_t position;
position.x_position = Default_X_Coordinate;
position.y_position = Default_Y_Coordinate;

robotInitialState.grid = position;
robotInitialState.bearing = Default_Bearing;

return robotInitialState;
}

RobotGridStatus_t robot_init_with_position(int bearing, int x, int y)
{
RobotGridStatus_t robotInitialState;

RobotCoordinates_t position;
position.x_position = x;
position.y_position = y;

robotInitialState.grid = position;
robotInitialState.bearing = FilterBearingInput(bearing);

return robotInitialState;
}

void robot_turn_right(RobotGridStatus_t * robot)
{
robot->bearing++;
{
}
}

void robot_turn_left(RobotGridStatus_t * robot)
{
}

{
switch(robot->bearing)
{

robot->grid.y_position++;
break;
robot->grid.x_position++;
break;
robot->grid.y_position--;
break;
robot->grid.x_position--;
break;
default:
printf("uh oh :/");
}
}

void robot_simulator(RobotGridStatus_t * robot, const char *commands)
{
for_each(commands)
{
switch(commands[i])
{
case Command_Left:
robot_turn_left(robot);
break;
case Command_Right:
robot_turn_right(robot);
break;
break;
default:
printf("uh oh. Not a valid command");
}
}
}``````

### src/robot_simulator.h

``````#ifndef ROBOT_SIMULATOR_H
#define ROBOT_SIMULATOR_H

typedef enum {
} Bearing_t;

enum {
Default_X_Coordinate = 0,
Default_Y_Coordinate = 0,
};

enum {
Command_Left = 'L',
Command_Right = 'R',
};

typedef struct RobotCoordinates {
int x_position;
int y_position;
} RobotCoordinates_t;

typedef struct RobotGridStatus {
Bearing_t bearing;
RobotCoordinates_t grid;
} RobotGridStatus_t;

RobotGridStatus_t robot_init(void);
RobotGridStatus_t robot_init_with_position(int bearing, int x, int y);
void robot_turn_right(RobotGridStatus_t * robot);
void robot_turn_left(RobotGridStatus_t * robot);
void robot_simulator(RobotGridStatus_t * robot, const char *commands);

#endif``````

tzarick
Solution Author
commented over 3 years ago

The macro probably isn't necessary I was just experimenting a little bit there

thats what edison said shen he created the lightbulb

@tzarick Could just instead do a while (*commands) { switch (*commands++) { //...

then there's no need for string.h

### 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?