Originally Posted by
pktcperlc++java
I fail to see why. as i understand it, the program accessing this array is using the array as a map.
Well it helps if you pay attention to the thread. Let's start at the top:
Originally Posted by
cdonlan
Hey guys im writing a program that uses a speadsheet like map. I want to check when the person goes off the map but i dont know how to compare 2D arrays like I did 1Ds
So to start, they've got a guy wandering around some place, and they want to do one test to see if he's in bounds or not. Salem posted a method which works just fine. Then there's your "solution":
Originally Posted by
pktcperlc++java
Code:
if(array[x][y]=={{x coordinate to compare},{y coordinate to compare}}){
//statements
}//end if
Now let's assume there is a function called "move", which takes an object to move, and a direction to move it. We need to, in said function, make sure we don't walk off the edge of the world. (Because that sounds similar to what they're trying to do.
Originally Posted by
pktcperlc++java
This woudl suggest that you already know the bounds of the array
to see whether or not the person is on a section of the map, my comparison would work fine.
No, as I've stated, it won't work. They were asking how to check to see if they've run out of bounds. Let's say, for the sake of this illustration, that they have indeed run out of bounds on the X coordinate:
Code:
if( array[x][y] ={{x coordinate to compare},{y coordinate to compare}}){
Now the first thing that if statement does is access the element located at array[x][y]. If x is already out of bounds, as I have stated, you're toast.
Originally Posted by
pktcperlc++java
you jsut have a nested for loop to cycle through each of the array elements
you could set up a boolean 2d aray to state where the person is
Why? When there are so many better ways to do this...
Code:
int move( struct Person *me, int direction )
{
int success = 0;
switch( direction )
{
case DirNorth: if( me->ycoord - 1 >= YMin ) me->ycoord--; success=1; break;
case DirEast: if( me->xcoord + 1 < XMax ) me->xcoord++; success=1; break;
case DirSouth: if( me->ycoord + 1 < YMax ) me->ycoord++; success=1; break;
case DirWest: if( me->xcoord - 1 >= XMin ) me->xcoord--; success=1; break;
}
return success;
}
...
if( move( me, direction ) )
{
output( "You mosey off to the %s.\n", directions[direction].word );
}
else
{
output( "You find your way blocked.\n" );
}
And there we will never run out of bounds. Now I've made some assumptions that are sure to boggle your mind, so I'll just ignore your pending reply when it relates to those bits of code.
And for one more final bit of clarity: You test the value before you try accessing that member of the array. Or rather, you don't, but you should.
Quzah.