Sign in to follow this  

2D Array as a 1D Array (Conversion problems)

This topic is 4054 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, Relatively easy topic, I know, but I'm pretty fried and could use a fresh pair of eyes on this. I'm working on a project that reads in a maze from a file, does path planning, and outputs the result. No biggie. I've already got Dijkstra working and A* is an easy add-on to that base. My problem is I'm storing the map in a 1D array and my conversion is wrong somewhere as it's causing the maps to be draw incorrectly. Here is how a maze looks in the text file:
// S = Start, E = End, 1 = Wall, 0 = Empty tile
S000100E
00100000
00010000
00000000
00000111
00100000
01100000
10000000

Here is how it's drawing when I just look through and draw each value in the 1D array:
S0000001
00000010
01000110
00100000
10000000
00001000
00001000
X0001000

Obviously not correct. Here is where I read in the maze from the file and place it into the 1D Array:
	for(int a = 0; a < (m_iMapWidth * m_iMapHeight); a++)
	{
		OpenFile >> Buffer;

		while(Buffer == ' ' || Buffer == '\n')
		{
			OpenFile >> Buffer;
		}

		Buffer = (char)toupper((int)Buffer);

		if(Buffer == 'S')
		{
				m_tStartPoint.x = a / m_iMapWidth;
				m_tStartPoint.y = a % m_iMapWidth;
				m_pMap[a] = 'S';
				continue;
		}
		else if (Buffer == 'E')
		{
				m_tEndPoint.x = a / m_iMapWidth;
				m_tEndPoint.y = a % m_iMapWidth;
				m_pMap[a] = 'X';
				continue;
		}

		m_pMap[a] = Buffer;
	}

And here is the access method I use to pull a value out of the 1D array using 2D values:
char CMap::GetMapTile(int x, int y)
{
	if(x < 0 || x >= m_iMapWidth)
		return -1;
	if(y < 0 || y >= m_iMapHeight)
		return -1;

	return m_pMap[x * m_iMapWidth + y];

}

I appreciate the help. I'm going to play with this after I've had some rest, but I figured I'd see if anyone just spots something obvious before I hit the grind stone again. Thanks.

Share this post


Link to post
Share on other sites
Assuming you intend for your 2D array to be row major, try replacing this:
m_tStartPoint.x = a / m_iMapWidth;
m_tStartPoint.y = a % m_iMapWidth;
With this:
m_tStartPoint.x = a % m_iMapWidth;
m_tStartPoint.y = a / m_iMapWidth;
And this:
return m_pMap[x * m_iMapWidth + y];
With this:
return m_pMap[y * m_iMapWidth + x];
Another option would be to use Boost MultiArray.

Share this post


Link to post
Share on other sites

This topic is 4054 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this