Avatar of ajborla

ajborla's solution

to Robot Simulator in the C Track

Published at Aug 10 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.

Getting Started

Make sure you have read the "Guides" section of the C track 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
static 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...
static 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);
}

static void test_invalid_initial_heading(void)
{
   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);
}

static 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);
}

static 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);

   expected.bearing = HEADING_SOUTH;
   robot_turn_right(&actual);
   confirm_position(&expected, &actual);

   expected.bearing = HEADING_WEST;
   robot_turn_right(&actual);
   confirm_position(&expected, &actual);

   expected.bearing = HEADING_NORTH;
   robot_turn_right(&actual);
   confirm_position(&expected, &actual);
}

static 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);

   expected.bearing = HEADING_SOUTH;
   robot_turn_left(&actual);
   confirm_position(&expected, &actual);

   expected.bearing = HEADING_EAST;
   robot_turn_left(&actual);
   confirm_position(&expected, &actual);

   expected.bearing = HEADING_NORTH;
   robot_turn_left(&actual);
   confirm_position(&expected, &actual);
}

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

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

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

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

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

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

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

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

static 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);
}

static 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);
}

static 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_invalid_initial_heading);
   RUN_TEST(test_init_with_negative_positions);
   RUN_TEST(test_turn_right);
   RUN_TEST(test_turn_left);
   RUN_TEST(test_advance_positive_north);
   RUN_TEST(test_advance_positive_east);
   RUN_TEST(test_advance_negative_south);
   RUN_TEST(test_advance_negative_west);
   RUN_TEST(test_simulate_move_west_and_north);
   RUN_TEST(test_simulate_move_west_and_south);
   RUN_TEST(test_simulate_move_east_and_north);

   return UnityEnd();
}

src/robot_simulator.c

/* ------------------------------------------------------------------------- */
/* exercism.io                                                               */
/* C Track Exercise: robot_simulator                                         */
/* Contributed: Anthony J. Borla (ajborla@bigpond.com)                       */
/* ------------------------------------------------------------------------- */

#include "robot_simulator.h"

#include <stdlib.h>
#include <stdio.h>

/* ------------------------------------------------------------------------- */
/* Initialises robot status grid with default values                         */
/* ------------------------------------------------------------------------- */
robot_grid_status_t robot_init(void)
{
    robot_grid_status_t robot_grid_status;

    robot_grid_status.bearing = DEFAULT_BEARING;
    robot_grid_status.grid.x_position = DEFAULT_X_COORDINATE;
    robot_grid_status.grid.y_position = DEFAULT_Y_COORDINATE;

    return robot_grid_status;
}

/* ------------------------------------------------------------------------- */
/* Initialises robot status grid with supplied values                        */
/* ------------------------------------------------------------------------- */
robot_grid_status_t robot_init_with_position(int bearing, int x, int y)
{
    robot_grid_status_t robot_grid_status;

    if (bearing > HEADING_MAX) robot_grid_status.bearing = DEFAULT_BEARING;
    else robot_grid_status.bearing = bearing;

    robot_grid_status.grid.x_position = x;
    robot_grid_status.grid.y_position = y;

    return robot_grid_status;
}

/* ------------------------------------------------------------------------- */
/* Perform robot action - TURN RIGHT                                         */
/* ------------------------------------------------------------------------- */
void robot_turn_right(robot_grid_status_t * robot)
{
   bearing_t bearing = robot->bearing;

   switch (bearing)
   {
       case HEADING_NORTH: robot->bearing = HEADING_EAST; break;
       case HEADING_EAST: robot->bearing = HEADING_SOUTH; break;
       case HEADING_SOUTH: robot->bearing = HEADING_WEST; break;
       case HEADING_WEST: robot->bearing = HEADING_NORTH; break;
       default: robot->bearing = DEFAULT_BEARING;
   }
}

/* ------------------------------------------------------------------------- */
/* Perform robot action - TURN LEFT                                          */
/* ------------------------------------------------------------------------- */
void robot_turn_left(robot_grid_status_t * robot)
{
   bearing_t bearing = robot->bearing;

   switch (bearing)
   {
       case HEADING_NORTH: robot->bearing = HEADING_WEST; break;
       case HEADING_EAST: robot->bearing = HEADING_NORTH; break;
       case HEADING_SOUTH: robot->bearing = HEADING_EAST; break;
       case HEADING_WEST: robot->bearing = HEADING_SOUTH; break;
       default: robot->bearing = DEFAULT_BEARING;
   }
}

/* ------------------------------------------------------------------------- */
/* Perform robot action - ADVANCE                                            */
/* ------------------------------------------------------------------------- */
void robot_advance(robot_grid_status_t * robot)
{
   bearing_t bearing = robot->bearing;

   switch (bearing)
   {
       case HEADING_NORTH: robot->grid.y_position += 1; break;
       case HEADING_EAST: robot->grid.x_position += 1; break;
       case HEADING_SOUTH: robot->grid.y_position -= 1; break;
       case HEADING_WEST: robot->grid.x_position -= 1; break;
       default: break;
   }
}

/* ------------------------------------------------------------------------- */
/* Parses command list performing robot action for each command              */
/* ------------------------------------------------------------------------- */
void robot_simulator(robot_grid_status_t * robot, const char *commands)
{
    const char *p = commands; char c = '\0';

    // Read through list of commands
    while ((c = *p++) != '\0')
    {
        // Decode command and perform associated action
        switch (c)
        {
            case 'L': robot_turn_left(robot); break;
            case 'R': robot_turn_right(robot); break;
            case 'A': robot_advance(robot); break;
            default: break;
        }
    }
}

src/robot_simulator.h

/* ------------------------------------------------------------------------- */
/* exercism.io                                                               */
/* C Track Exercise: robot_simulator                                         */
/* Contributed: Anthony J. Borla (ajborla@bigpond.com)                       */
/* ------------------------------------------------------------------------- */

#ifndef ROBOT_SIMULATOR_H
#define ROBOT_SIMULATOR_H

typedef enum {
   HEADING_NORTH = 0,
   HEADING_EAST,
   HEADING_SOUTH,
   HEADING_WEST,
   HEADING_MAX
} bearing_t;

enum {
   DEFAULT_BEARING = HEADING_NORTH,
   DEFAULT_X_COORDINATE = 0,
   DEFAULT_Y_COORDINATE = 0,
};

enum {
   COMMAND_LEFT = 'L',
   COMMAND_RIGHT = 'R',
   COMMAND_ADVANCE = 'A'
};

typedef struct robot_coordinates {
   int x_position;
   int y_position;
} robot_coordinates_t;

typedef struct robot_grid_status {
   bearing_t bearing;
   robot_coordinates_t grid;
} robot_grid_status_t;

/* ------------------------------------------------------------------------- */
/* Initialises robot status grid with default values                         */
/* ------------------------------------------------------------------------- */
robot_grid_status_t robot_init(void);

/* ------------------------------------------------------------------------- */
/* Initialises robot status grid with supplied values                        */
/* ------------------------------------------------------------------------- */
robot_grid_status_t robot_init_with_position(int bearing, int x, int y);

/* ------------------------------------------------------------------------- */
/* Perform robot action - TURN RIGHT                                         */
/* ------------------------------------------------------------------------- */
void robot_turn_right(robot_grid_status_t * robot);

/* ------------------------------------------------------------------------- */
/* Perform robot action - TURN LEFT                                          */
/* ------------------------------------------------------------------------- */
void robot_turn_left(robot_grid_status_t * robot);

/* ------------------------------------------------------------------------- */
/* Perform robot action - ADVANCE                                            */
/* ------------------------------------------------------------------------- */
void robot_advance(robot_grid_status_t * robot);

/* ------------------------------------------------------------------------- */
/* Parses command list performing robot action for each command              */
/* ------------------------------------------------------------------------- */
void robot_simulator(robot_grid_status_t * robot, const char *commands);

#endif

Community comments

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

ajborla's Reflection

Good fun !