By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,225 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

I need help Urgently.

P: 1
I have got a homework task to do and I have started the work but I cannot finish it.Can someone please help me finish the code. The help given is much appreciated.


The actual specifications are here:

The task for this project is to write a simulator program for a robot designed to move packages around in a warehouse environment.
The input to your program (from standard input) will contain a map of the environment in its original state, followed by a blank line, followed by a sequence of instructions to be performed by the robot.

The map specifies the size, shape and initial positions of all the packages in the environment, as well as the positions of the walls, and the initial position and orientation of the robot. The walls will be represented by the "star" character *. The robot will be represented by the characters ^, v, < or >, depending on which direction it is facing. There will be at most 26 "packages" in the environment, labeled with the letters A,B,C, etc. (note that packages may vary in size and shape).

The robot is capable of four basic operations, as follows:

L turn left
R turn right
F (try to) move forward
P print the state of the environment


The instructions for the robot will consist of an ordered sequence of these four basic instructions L,R,F,P enclosed in parentheses.

When it executes an L or R instruction, the robot remains in the same location and only its orientation changes. When it executes the F instruction, the robot attempts to move a single step in whichever direction it is facing.

If there is a package immediately in front of the robot when it tries to move forward, the package will move with the robot (in the same direction). If there are one or more packages immediately in front of that package, they will also move, as well the packages immediately in front of them, and so on. We assume the robot is strong enough to push any number of packages in front of it. Since the walls are immovable, however, there will be some situations where the robot tries to move forward but fails. This will happen if there is a wall immediately in front of the robot, or if there is a wall immediately in front of a package being pushed by the robot (either directly or indirectly). In these cases, the F instruction has no effect on the environment, and the robot continues to the next instruction. (Part of the challenge of the project is to determine which packages are being pushed, and whether or not a wall is being pushed.)

When a P instruction is executed, the current state of the environment should be printed, followed by a blank line. The robot leaves a trail behind it wherever it goes. So, when the environment is printed, places where there is no package or wall should be indicated by: a dot '.' if the robot has been there at some time during its path, and a blank space ' ' otherwise.

For example, given this 9-line input:

**************
********** *
* * BB *
* A *
* *** AA * *
* ^ A * *
************************

(FFFFFRFFFLFRFFFFRFFPLFFFRFRFFFFFFFFFFFFFP)

your program should produce the following 16-line output:
**************
********** ..... *
* ....* v *
* . A BB *
* *** . AA * *
* . A * *
************************

**************
********** ..... *
* ....* .... *
* ABB<........... *
* ***AA . * *
* A . * *
************************


The environment might not be rectangular, but you may assume that it is entirely surrounded by walls, so there is no danger of the robot falling off the edge of the environment. Packages may have any shape, but you can assume that all characters belonging to a single package are contiguous (i.e. connected to each other). You may assume the environment is no larger than 80 x 80 (including walls) and that each package consists of no more than 128 characters. (Note: the program can be written to handle environments and packages of arbitrary size, but we do not force you to do so.)



This is the actual file we need to implement. They have provided the template, we need to fill in the blanks.



/** \file homework.c

Modify this code to scan the state of the environment,
as specified in Homework Sheet 2, and print this state to standard output.


*/

#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;
}

/**
Scan the state of the environment from standard input.

The two-dimensional array object[][] is used
to return a map of the environment;<br>
if there is a package at location (r,c) then
object[r][c] will be an integer between
0 (representing 'A') and 25 (representing 'Z');
otherwise, it will have one of the special values
ROBOT, WALL or NONE.

The pointer pnrows is used to return the
total number of rows in the environment.

The array ncols[] is used to return the
number of columns in each row.

The pointer pdirection is used to return the
direction the robot is facing (NORTH, SOUTH, EAST or WEST)

@param pnrows number of rows (returned implicitly)
@param ncols[] number of columns for each row
@param object[][] object at each location
@param pdirection which way the robot is facing (returned implicitly)
*/
void scan_state(
int *pnrows,
int ncols[MAX_ROWS],
int object[MAX_ROWS][MAX_COLS],
int *pdirection
)
{
int ch;
int r,c;

r = 0;
c = -1;

while (( ch = getchar() ) != '(' ) {
c++;
if ( ch == '\n' ) {
ncols[r++] = c;
c = -1;
}
// else if { ...

// ... INSERT MORE CODE HERE ...

// }

}

*pnrows = r;
}

/**
Print the current state of the environment to standard output.

@param nrows number of rows in the environment
@param ncols[] number of columns in each row
@param object[][] object at each location
@param direction which way the robot is facing
*/
void print_state(
int nrows,
int ncols[MAX_ROWS],
int object[MAX_ROWS][MAX_COLS],
int direction
)
{
// ... INSERT CODE HERE ...
}
Sep 26 '06 #1
Share this Question
Share on Google+
1 Reply


100+
P: 293
D_C
A switch statement may be convenient for the instructions.
Expand|Select|Wrap|Line Numbers
  1. switch(ch)
  2. {
  3.   case 'L': up += right;
  4.             right -= up;
  5.             up += right
  6.             break;
  7.   case 'R': up -= right;
  8.              right += up;
  9.              up -= right;
  10.             break;
  11.   case 'F': if(not blocked)
  12.             {
  13.                x_pos += up;
  14.                y_pos += right;
  15.             }
  16.             break;
  17.   case 'P': // call print environment
  18.             break;
  19. }
Sep 27 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.