0

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

1
  • What should fix_pos return, the index of [4, 2] if all the values were in a 1D array?? Commented May 28, 2014 at 14:18

2 Answers 2

1
int Map::fix_Pos(int x, int y) { int x_r; int y_r; x_r = x * this->x_map; return (x_r + y); } 

Actually you multiply you're horizontal line (which is X) and add your horizontal line (which is Y). By doing you'll get the opposite position of you're map.

Try to multiply your number of vertical line (Y) with the size of your line and add the rest of you're horizontal line.

Try this :

int Map::fix_Pos(int x, int y) { return (y * this->x_map + x); } 
Sign up to request clarification or add additional context in comments.

Comments

0

I think it should be one of the following (depending of your representation):

int Map::fix_Pos(int x, int y) const { return y * this->x_map + x; } 

or

int Map::fix_Pos(int x, int y) const { return x * this->y_map + y; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.