Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to simulate the operation of a simple robot . the robot moves in

ID: 3555647 • Letter: W

Question

Write a program to simulate the operation of a simple robot . the robot moves in four directions :forward , right , left. the job of the robot is to move items and place it in the right slots in each station. there are 8 stations plus the pick up station . the pick up station is the initial start where the robot picks the items . 8 items can be placed on each station .The nearest station must be filled before placing items on the next station. the items are marked with identification or serial numbers. the items with odd serial number go to the right and the items with even number go the left .the last slot, number 7 is reserved for heavy items which are moret han 700 kg. the serial number is a five digit number , the MSBdigit 5 indicates that the product must be placed in the fifth station which is keeping the product at 20 degree F.

Explanation / Answer

#include <stdio.h>

#define ROBOT 26
#define WALL 27
#define NONE -1

#define MAX_ROWS 80
#define MAX_COLS 80

#define EAST 0
#define NORTH 1
#define WEST 2
#define SOUTH 3

void scan_state(
int *pnrows,
int ncols[MAX_ROWS],
int object[MAX_ROWS][MAX_COLS],
int *pdirection
);

void print_state(
int nrows,
int ncols[MAX_ROWS],
int object[MAX_ROWS][MAX_COLS],
int direction
);

int main( void )
{
int nrows; // number of rows in environment
int ncols[MAX_ROWS]; // number of columns in each row
int object[MAX_ROWS][MAX_COLS]; // object at each location
int direction; // which way the robot is facing

// scan the state of the environment
scan_state( &nrows, ncols, object,&direction );

// print the state of the environment
print_state( nrows, ncols, object, direction );

return 0;
}

void scan_state(
int *pnrows,
int ncols[MAX_ROWS],
int object[MAX_ROWS][MAX_COLS],
int *pdirection
)
{
int ch;
int r,c,rr,rc;

r = 0;
c = -1;

while (( ch = getchar() ) != '(' ) {
c++;
if ( ch == ' ' ) {
ncols[r++] = c;
c = -1;
}
else if ( ch == '*' ) {
object[r][c] = WALL;
}
else if ((ch >='A' )&&( ch <= 'Z')){
object [r][c]= ch - 'A';
}
else if ( ch == '^' ) {
object [r][c] = ROBOT;
*pdirection = NORTH;
}
else if ( ch == '>' ) {
object [r][c] = ROBOT;
*pdirection = EAST;
}
else if ( ch == 'v' ) {
object [r][c] = ROBOT;
*pdirection = SOUTH;
}
else if ( ch == '<' ) {
object [r][c] = ROBOT;
*pdirection = WEST;
}
else {
object [r][c] = NONE;
}
}

//Give the robot its own cordinates.
if (object[r][c] == ROBOT ) {
r_r=r;
r_c=c;
}

while (( ch = getchar() ) != ')' ) {
c++;
if ( ch == ' ' ) {
ncols[r++] = c;
c = -1;
}
if ( ch == 'L' ) {
if ( *pdirection == EAST ) {
*pdirection = NORTH;
}
else if ( *pdirection == NORTH ) {
*pdirection = WEST;
}
else if ( *pdirection == WEST ) {
*pdirection = SOUTH;
}
else if ( *pdirection == SOUTH ) {
*pdirection = EAST;
}
}
if (ch == 'R' ) {
if ( *pdirection == EAST ) {
*pdirection = SOUTH;
}
else if ( *pdirection == SOUTH ) {
*pdirection = WEST;
}
else if ( *pdirection == WEST ) {
*pdirection = NORTH;
}
else if ( *pdirection == NORTH ) {
*pdirection = EAST;
}
}
if (ch == 'F' ) {
if ( *pdirection == EAST ) {
object[r_r+1][r_c] = ROBOT;

}
if ( *pdirection == SOUTH ) {
object[r_r][r_c-1] = ROBOT;

}
if ( *pdirection == WEST ) {
object[r_r-1][r_c] = ROBOT;

}
if ( *pdirection == NORTH ) {
object[r_r][r_c+1] = ROBOT;

}
}

}

//give the robot its own cordinates.
if (object[r][c] == ROBOT ) {
object[rr][rc] = ROBOT;
}

while (( ch = getchar() ) != ')' ) {
c++;
if ( ch == ' ' ) {
ncols[r++] = c;
c = -1;
}
else if ( ch == 'L' ) {
if ( *pdirection == SOUTH ) {
*pdirection = EAST;
}
else {
*pdirection++;
}
}
else if (ch == 'R' ) {
if ( *pdirection == EAST ) {
*pdirection = SOUTH;
}
else {
*pdirection--;
}
}
}

*pnrows = r;
}


void print_state(
int nrows,
int ncols[MAX_ROWS],
int object[MAX_ROWS][MAX_COLS],
int direction
)
{
int r,c;

for( r=0; r < nrows ; r++ ) {
for( c=0; c < ncols[r]; c++ ) {
if (object[r][c] == WALL) {
printf( "*" );
}
if (object[r][c] == NONE) {
printf( " " );
}
if ((object [r][c]>= 0)&&(object [r][c]<= 25)){
printf("%c",'A'+object [r][c]);
}

if ( object[r][c] == ROBOT ) {
if (direction == NORTH) {
printf("^");
}
if (direction == EAST) {
printf(">");
}
if (direction == SOUTH) {
printf("v");
}
if (direction == WEST) {
printf("<");
}
}

}
printf( " " );
}


}

class Robot {
   static MIN_X, MAX_X, MIN_Y, MAX_Y;
   int x, y;

public:
   Robot() { x = 0; y = 0; }
   Robot(int x, int y) { this->x = x; this->y = y; }

   int getX() { return x; }
   int getY() { return Y; }

   bool move(int east, int south) {
       int _x = x + east, _y = y + south;
       if( MIN_X <= _x && _x <= MAX_X && MIN_Y <= _y && _y <= MAX_Y ) {
           x = _x;
           y = _y;
           return true;
       }
       return false;
   }

   bool moveNorth() { return move(0, -1); }
   bool moveSouth() { return move(0, 1); }
   bool moveEast() { return move(1, 0); }
   bool moveWest() { return move(-1, 0); }
   bool moveNorthEast() { return move(1, -1); }
   bool moveNorthWest() { return move(-1, -1); }
   bool moveSouthEast() { return move(1, 1); }
   bool moveSouthWest() { return move(-1, 1); }
};

Robot::MIN_X = 1;
Robot::MAX_X = 19;
Robot::MIN_Y = 1;
Robot::MAX_Y = 19;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote