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