CSE110 Spring 2001

Homework #6 Hints


  1. The locations array keeps track of all of the objects of the game. You do so by assigning a unique array location to every game object. One way of doing this is by defining an enumerated type for the game objects and using this to index the array. For example, if there were only Ringo, the crab, and two barrels of waste, then the type might look like this:
    typedef enum {OBJ_RINGO, OBJ_CRAB, OBJ_DRUM1, OBJ_DRUM2} GameObj;
    
    If your Location type looks like this:
    typedef enum {LOC_OCTOPUS, LOC_SUB_REF, LOC_CRAB, LOC_RINGO_HAND} Location;
    
    Then in your main function, you will need a declaration and initializations that look as follows:
    Location locations[100];
    
    locations[(int)OBJ_RINGO] = LOC_OCTOPUS;
    locations[(int)OBJ_CRAB] = LOC_CRAB;
    locations[(int)OBJ_DRUM1] = LOC_SUB_REF;
    locations[(int)OBJ_DRUM2] = LOC_SUB_REF;
    
    This will need to be extended to include all game locations and objects.

    The Grab function moves an object from Ringo's current location to Ringo's hand, and the Throw function moves an object from Ringo's hand to Ringo's current location. Throwing the octopus needs to be handled as a special case, which you can go either in main or inside the body of Throw.

    Ringo himself has to be an object stored in the locations array since this is the only parameter passed to Grab and Throw - there's no other way to decide which objects are in the same place as Ringo. To simplify the interface, Ringo should only be able to grab one object, and he can only throw an object if he has one in his hand. Also, the Grab action is ambiguous if there is more than one object present, so you should just choose some object arbitrarily, perhaps the first one you find.

  2. To minimize repetition of code, it is useful to define more functions than the problem actual requires that you write. For instance, if you write a function DirectionStr that is defined as follows:
    string DirectionStr(char act)
    {
       string dirstr = "";
    
       /*   Find out the string corresponding to the direction.   */
       switch(act)
       {
       case 'n': dirstr = "north"; break;
       case 's': dirstr = "south"; break;
       case 'e': dirstr = "east"; break;
       case 'w': dirstr = "west"; break;
       }
       return dirstr;
    }
    
    and another function with prototype:
    string LocationStr(Location loc);
    
    that takes a Location and returns the string that should be printed when Ringo arrives at location loc, then you can generate all of the messages that are printed in response to Ringo's swimming in a single statement, that looks something like this:
      printf("You swim %s, out of the %s, into the %s.\n",
          DirectionStr(action),
          LocationStr(map[y][x]),
          LocationStr(map[y + northSouthMove][x + eastWestMove]));
    
    ... where northSouthMove and eastWestMove take on the values (1,0), (-1,0), (0,-1), or (0,1) depending on the direction that Ringo swims.

    Note that you will need to impose a coordinate system on the world in order to lay it out in the array. You can place the origin of the coordinate system wherever you want, but you must be sure to be consistent about how you use the coordinates throughout your program. The most natural choice of coordinate systems is to let one of the corners be the origin and let the x-coordinate run in the east-west direction and the y-coordinate run in the north-south direction. For example, say the lower left-hand corner is the origin, in which case the array location at (0,0) would have LOC_MANATEE (assuming that's the name you gave to that location in the your expanded Location type), and LOC_SEAHORSE would have coordinates (1,0), and LOC_OCTOPUS would have coordinates (2,2), et cetera. So your initialization of your map would look something like this:

    #define MAXSIZE_X 5
    #define MAXSIZE_Y 5
    
    ...
    
    Location map[MAXSIZE_X][MAXSIZE_Y];
    
    map[0][0] = LOC_MANATEE;
    map[1][0] = LOC_SEAHORSE;
    map[2][0] = LOC_SEACOWBOY;
    ...
    
    You will then be able to compute the location that is adjacent locations by searching for the coordinates of Ringo's current location and then adding 1 to the x coordinate if he's going to go east, subtracting 1 from the y coordinate if he's going to go south, et cetera.

    Note that as is always the case for arrays, you need to be careful to not go exceed to array boundaries (by using a negative index or an index that goes off the end of the array). In this case, this means you need to do something reasonable if the user tries to move off the edge of the map. The sample solution simply has Ringo stay where he is.

  3. There are no hints available for this problem.