Sha256: 43f7673ea9f88ea65d15e4b64123e300f3411e96b013175cfc5d06e979730804

Contents?: true

Size: 1.48 KB

Versions: 235

Compression:

Stored size: 1.48 KB

Contents

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

RobotGridStatus_t robot_init(void)
{
   return (robot_init_with_position
           (Default_Bearing, Default_X_Coordinate, Default_Y_Coordinate));
}

RobotGridStatus_t robot_init_with_position(int bearing, int x, int y)
{
   RobotGridStatus_t robot = { bearing, {x, y} };

   if ((bearing < Heading_North) || (bearing >= Heading_Max)) {
      robot.bearing = Default_Bearing;
   }
   return robot;
}

void robot_turn_right(RobotGridStatus_t * robot)
{
   robot->bearing = (robot->bearing + 1) % Heading_Max;
}

void robot_turn_left(RobotGridStatus_t * robot)
{
   robot->bearing = ((robot->bearing - 1) + Heading_Max) % Heading_Max;
}

void robot_advance(RobotGridStatus_t * robot)
{
   switch (robot->bearing) {
   case Heading_North:
      robot->grid.y_position++;
      break;

   case Heading_East:
      robot->grid.x_position++;
      break;

   case Heading_South:
      robot->grid.y_position--;
      break;

   case Heading_West:
      robot->grid.x_position--;
      break;

   default:
      break;
   }
}

void robot_simulator(RobotGridStatus_t * robot, const char *commands)
{
   for (unsigned long index = 0; index < strlen(commands); index++) {
      switch (commands[index]) {
      case Command_Left:
         robot_turn_left(robot);
         break;

      case Command_Right:
         robot_turn_right(robot);
         break;

      case Command_Advance:
         robot_advance(robot);
         break;

      default:
         break;
      }
   }
}

Version data entries

235 entries across 235 versions & 1 rubygems

Version Path
trackler-2.2.1.45 tracks/c/exercises/robot-simulator/src/example.c
trackler-2.2.1.44 tracks/c/exercises/robot-simulator/src/example.c
trackler-2.2.1.43 tracks/c/exercises/robot-simulator/src/example.c
trackler-2.2.1.42 tracks/c/exercises/robot-simulator/src/example.c
trackler-2.2.1.41 tracks/c/exercises/robot-simulator/src/example.c
trackler-2.2.1.40 tracks/c/exercises/robot-simulator/src/example.c
trackler-2.2.1.39 tracks/c/exercises/robot-simulator/src/example.c
trackler-2.2.1.38 tracks/c/exercises/robot-simulator/src/example.c
trackler-2.2.1.37 tracks/c/exercises/robot-simulator/src/example.c
trackler-2.2.1.36 tracks/c/exercises/robot-simulator/src/example.c
trackler-2.2.1.35 tracks/c/exercises/robot-simulator/src/example.c
trackler-2.2.1.34 tracks/c/exercises/robot-simulator/src/example.c
trackler-2.2.1.33 tracks/c/exercises/robot-simulator/src/example.c
trackler-2.2.1.32 tracks/c/exercises/robot-simulator/src/example.c
trackler-2.2.1.31 tracks/c/exercises/robot-simulator/src/example.c
trackler-2.2.1.30 tracks/c/exercises/robot-simulator/src/example.c
trackler-2.2.1.29 tracks/c/exercises/robot-simulator/src/example.c
trackler-2.2.1.28 tracks/c/exercises/robot-simulator/src/example.c
trackler-2.2.1.27 tracks/c/exercises/robot-simulator/src/example.c
trackler-2.2.1.26 tracks/c/exercises/robot-simulator/src/example.c