Im reading a text file like this:
wwwwwwwwwwwwwww
wffbbbbbbbbbffw
wf1bwbwbwbwbwfw
wbbbbbbbbbbbbbw
wbwbwbwbwbwbwbw
wbbbbbbbbbbbbbw
wbwbwbwbwbwbwbw
wbbbbbbbbbbbbbw
wbwbwbwbwbwbwbw
wbbbbbbbbbbbbbw
wbwbwbwbwbwbwbw
wbbbbbbbbbbbbbw
or
fffffffffffffff
wffffffffffffff
fwfffffffffffff
fffffffffffffff
fffffffffffffff
fffffffffffffff
ff1ffffwfffffff
fffffffwfffffff
fffffffwfffffff
fffffffwfffffff
fffffffffffffff
fff2fffffffffff
fffffffffffffff
fffffffffffffff
fffffffffffffff
wffffffffffffff
fwfffffffffffff
fffffffffffffff
fffffffffffffff
fffffffffffffff
fffffffwfffffff
fffffffwfffffff
fffffffwfffffff
fffffffwfffffff
fffffffffffffff
fffffffffffffff
fffffffffffffff
fffffffffffffff
fffffffffffffff
Then I get the x and the y of this file by doing a strlen of the first string for y and the number of lines for x :
void Map::get_size_map(char *dat_name)
{
std::ifstream ifs;
int x;
int y;
char c;
x = 0;
y = 0;
c = 0;
ifs.open (dat_name, std::ifstream::in);
if(ifs.is_open())
{
while(!ifs.eof())
{
c = ifs.get();
if(y == 0 && c != '\n')
x++;
if(c == '\n')
y++;
}
}
ifs.close();
this->x_map = x;
this->y_map = y;
}
Now in my program I wanna access let's say the [4][2] position of my map, for that I made a function returning the correct place in my 1d array with my func like this :
int Map::fix_Pos(int x, int y)
{
int x_r;
int y_r;
x_r = x * this->x_map;
return (x_r + y);
}
It works well for the first text(map) but for the second I get strange behavior on the first move when I start to move inside the map
fix_posreturn, the index of [4, 2] if all the values were in a 1D array??