Vector comparison out of range

I'm making a simple game and I'm having trouble creating boundaries for the players movements. The map is a 2D vector. When I compare the vector with the players current position sometimes I get an error during run. When the error isn't occurring the behavior of the boundaries is bizarre. The error tells me that the vector is out of range.


Here is the where the map is created. It's within its own class constructor.
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
26
27
28
29
30
31
32
33
34
35
vector< vector<char> > map_parts;
map_parts.resize(25);
for ( int i = 0; i < 25; i++ )
{
    map_parts[i].resize(80);
}

//check if file open worked
ifstream map_reader ( "C:/Users/Ryan Melendez/Desktop/C++/programs/Curses/City.txt" );
if ( ! map_reader )
{
    printw( "Error.\n" );
}

//save map to memory
while ( ! map_reader.eof() )
{
    for ( int i = 0; i < 25; i++ )
    {
        for ( int j = 0; j < 80; j++ )
        {
            map_reader >> noskipws >> map_parts[i][j];
        }
    }
}
map_reader.close();

//print map
for ( int i = 0; i < 25; i++ )
{
    for ( int j = 0; j < 80; j++ )
    {
        printw( "%c", map_parts[i][j] );
    }
}


And here is where the vector is being compared to determine whether or not the player is colliding with a wall. This code is written as a method within a different class. The first case has a simple IF test to see if the coordinate to the left is a wall.
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
26
27
28
29
30
31
32
33
34
//Move player
void player_class::movePlayer()
{
    int temp = getch();
    switch (temp)
    {
        case KEY_LEFT:
            if (map_parts[player.x - 1][player.y] == '|' || '_')
            {
                break;
            }
            move(player.y, player.x);
            addch(' ');
            player.x--;
            break;
        case KEY_UP:
            move(player.y, player.x);
            addch(' ');
            player.y--;
            break;
        case KEY_RIGHT:
            move(player.y, player.x);
            addch(' ');
            player.x++;
            break;
        case KEY_DOWN:
            move(player.y, player.x);
            addch(' ');
            player.y++;
            break;
    }
    move(player.y, player.x);
    addch(player_char);
}
I don't see anything in movePlayer that attempts to keep the player within the bounds of the map. Even if the player is within the bounds of the map on KEY_LEFT, map_parts[player.x-1][player.y] may be outside it.
Topic archived. No new replies allowed.