Thread: Help with moving robot in a maze

  1. #1
    Registered User
    Join Date
    Sep 2006

    Help with moving robot in a maze

    I am working on a problem, and i am having some difficulty starting it. If anyone has time , it would be greatly appreciated if they had a quick look at my code, and what i need to do. Here is what im trying to do:

    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.

    Attached is an exampleAttachment 6806

    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.)

    Here is what i have done and where i need help:

     #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
    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() ) != '(' ) {
        if ( ch == '\n' ) {
          ncols[r++] = c;
          c = -1;
    // else if { ...
     // ... Not sure what goes in here ...
     // }
     *pnrows = r;
    void print_state(
                     int nrows,
                     int ncols[MAX_ROWS],
                     int object[MAX_ROWS][MAX_COLS],
                     int direction
      // need help here too plz
    Thanks alot for your help.
    Last edited by Salem; 09-25-2006 at 11:41 AM. Reason: Snip email address

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Melbourne, Australia
    I have to be honest, it looks like you have just posted a homework assignment, tried to add a couple of lines of code and then expected us to fill in the (giant) gaps. I would suggest writing a whole lot more code and then asking us for help with specific problems you encounter.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > // else if { ...
    > // ... Not sure what goes in here ...
    Well you need an if() for each kind of symbol which can appear, like

    etc etc

    In particular, when you read the robot symbol, you also work out the direction in which it is facing.

    Work on the scan_state and print_state together. Printing should produce the same result as your input, if you don't do any moving.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! Robot Programming
    By Gizzle in forum C Programming
    Replies: 23
    Last Post: 09-28-2006, 08:32 AM
  2. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM
  3. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-25-2002, 12:00 AM
  4. Maze game, complete with maze editor and an example maze!
    By Brian in forum A Brief History of
    Replies: 2
    Last Post: 01-20-2002, 03:27 AM
  5. Replies: 5
    Last Post: 09-08-2001, 10:18 AM
Website Security Test