Checking all values within a multidimensional array

I have a program that requires a user to input a start and end location within a grid, and then calculates the most efficient route between the two points with consideration for walls, or impassable gridsquares (Think of it as an exercise in AI pathing.)
However I'm having trouble in that if a user enters a start position that is filled by an impassable square, the program has no qualms setting that as their position. (A feature I don't really want of course)
My location checking code and an example grid declaration is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
bool Player::checkPlayerStartInput(int nPlayerInput, char cCoord)
{
     if(cCoord == 'x')
     {
               if(nPlayerInput < 1 || nPlayerInput > nMatrixSizeX )
               {
                               return false;
               }
               else
               {
                   return true;
               }
     }
     else if(cCoord == 'y')
     {
               if(nPlayerInput < 1 || nPlayerInput > nMatrixSizeY)
               {
                               return false;
               }
               else
               {
                   return true;
               }
     }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
char cScenario[nMatrixSizeX][nMatrixSizeY] = 
    {
    {'*','*','*','*','*','*','o','*','*','*','*'},
    {'*','*','*','*','*','*','o','*','*','*','*'},
    {'*','*','*','*','o','o','o','*','*','*','*'},
    {'*','*','*','*','o','o','o','*','*','*','*'},
    {'*','*','*','*','o','o','o','*','*','*','*'},
    {'o','o','o','*','*','*','o','*','*','*','*'},
    {'o','*','o','*','*','*','*','*','*','*','*'},
    {'o','*','*','*','*','*','*','*','*','*','*'},
    {'o','*','*','*','*','*','o','*','*','*','o'},
    {'o','o','o','*','*','*','o','o','o','o','o'},
    {'o','o','o','*','*','*','o','o','o','o','o'}
    };


Does anyone know of an efficient way I can check the contents of an array element before assigning a value to it?
Last edited on
You have nPlayerInput for the x coordinate and nPlayerInput for the y coordinate. So you can check the element that has these indexes

int x = nPlayerInput; // value for 'x'
int y = nPlayerInput; // value for 'y'

if ( cScenario[x][y] == some_value ) { /*...some code ...*/ }
thanks
Topic archived. No new replies allowed.