Jump to content
  • Advertisement
Sign in to follow this  
sharpnova

N-dimensional arrays

This topic is 3783 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

The code below is my program which uses the prim algorithm to generate a two-dimensional maze using dynamic memory allocation. (The size of the maze is specified in the command line arguments)
#include <iostream>
#include <ctime>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;


struct cell
{
	bool n,s,w,e,inset;
};

int main( int argc, char *argv[] )
{
	ofstream OutputFile; // the file the IN array will be saved to
	OutputFile.open("maze.dat");

	int xdim, ydim; // x and y spans
	//xdim = atoi(argv[1]);
	//ydim = atoi(argv[2]);
	
	xdim=atoi(argv[1]);
	ydim=atoi(argv[2]);
	cell** IN;
	IN = new cell*[xdim];
	for(int i = 0; i < xdim; i++)
    {
		IN = new cell[ydim];
    }
	
	cell** EX;
	EX = new cell*[xdim];
	for(int i = 0; i < xdim; i++)
	{
		EX = new cell[ydim];
	}

	cell** ADJ;
	ADJ = new cell*[xdim];
	for(int i = 0; i < xdim; i++)
	{
		ADJ = new cell[ydim];
	}
	
	int NUMADJ = 0;//number of cells still in the ADJ set

	// initially sets everything to false except the fact that everything is in the EX set
	for(int i=0;i<xdim;i++)
	{
		for(int j=0;j<ydim;j++)
		{
			IN[j].n = false;
			EX[j].n = false;
			ADJ[j].n = false;
			IN[j].s = false;
			EX[j].s = false;
			ADJ[j].s = false;
			IN[j].w = false;
			EX[j].w = false;
			ADJ[j].w = false;
			IN[j].e = false;
			EX[j].e = false;
			ADJ[j].e = false;
			IN[j].inset = false;
			EX[j].inset = true;
			ADJ[j].inset = false;
		}
	}


	//picks a random cell to be in the IN set. and it's neighbors to be in the ADJ set
	srand(time(0));
	int x = rand()%xdim;
	int y = rand()%ydim;
	EX[x][y].inset = false;
	IN[x][y].inset = true;
	if(x>0)
	{
		EX[x-1][y].inset = false;
		ADJ[x-1][y].inset = true;
		NUMADJ++;
	}
	if(x<(xdim-1))
	{
		EX[x+1][y].inset = false;
		ADJ[x+1][y].inset = true;
		NUMADJ++;
	}
	if(y>0)
	{
		EX[x][y-1].inset = false;
		ADJ[x][y-1].inset = true;
		NUMADJ++;
	}
	if(y<(ydim-1))
	{
		EX[x][y+1].inset = false;
		ADJ[x][y+1].inset = true;
		NUMADJ++;
	}
			
	//as long as there are cells in the ADJ set it picks a random cell in ADJ
	//put it in IN, put it's neighbors in ADJ, and connect it with a random cell in IN
	while(NUMADJ)
	{
		while(!(ADJ[x][y].inset))
		{	
			x = rand()%xdim;
			y = rand()%ydim;
		}
				
		ADJ[x][y].inset = false;
		NUMADJ--;
		IN[x][y].inset = true;
		if((x>0)&&EX[x-1][y].inset)
		{
				EX[x-1][y].inset = false;
				ADJ[x-1][y].inset = true;
				NUMADJ++;
		}
		if((x<(xdim-1))&&EX[x+1][y].inset)
		{
				EX[x+1][y].inset = false;
				ADJ[x+1][y].inset = true;
				NUMADJ++;
		}
		if((y>0)&&EX[x][y-1].inset)
		{
				EX[x][y-1].inset = false;
				ADJ[x][y-1].inset = true;
				NUMADJ++;
		}
		if((y<(ydim-1))&&EX[x][y+1].inset)
		{
				EX[x][y+1].inset = false;
				ADJ[x][y+1].inset = true;
				NUMADJ++;
		}
		
		
		//now we connect cell x,y with a random cell in IN that is adjacent to it
		int looop=1;
		while (looop)
		{
			int a = rand()%xdim;
			int b = rand()%ydim;
			if ( IN[a].inset && (a==(x-1)) && (b==y) )
			{
				IN[a].e = true;
				IN[x][y].w = true;
				looop = 0;
			}
			else if ( IN[a].inset && (a==(x+1)) && (b==y) )
			{
				IN[a].w = true;
				IN[x][y].e = true;
				looop = 0;
			}
			else if ( IN[a].inset && (a==x) && (b==(y-1)) )
			{
				IN[a].n = true;
				IN[x][y].s = true;
				looop = 0;
			}
			else if ( IN[a].inset && (a==x) && (b==(y+1)) )
			{
				IN[a].s = true;			
				IN[x][y].n = true;
				looop = 0;
			}
		}
	}
	//this writes the IN array to a file in an easy-to-parse manner

	for(int i=0;i<xdim;i++)
	{
		for(int j=0;j<ydim;j++)
		{
			if((i==(xdim-1))&&(j==(ydim-1))) break;
			OutputFile << i <<"x"<< j <<":"<< IN[j].n <<"."<< IN[j].s <<"."<< IN[j].w <<"."<< IN[j].e << " ";
		}
	}
	OutputFile << xdim-1 <<"x"<< ydim-1 <<":"<< IN[xdim-1][ydim-1].n <<"."<< IN[xdim-1][ydim-1].s <<"."<< IN[xdim-1][ydim-1].w <<"."<< IN[xdim-1][ydim-1].e;

	OutputFile.close();
	return 0;
}

This program works great and I even modified it to generate 3d mazes instead. This was quite easy. My question: How would I go about doing it n-dimensionally, where n was also defined in command line arguments by the user. I know of no way in which to handle an n-dimensional array and dynamically allocate it at run-time. side note: there is one way i could do this but it is messy and i'd honestly prefer something cleaner. i could just have a one dimensional array and do divisions and mods and handle remainders to find out where each element was in the higher-dimensional sense. this is so messy though. if there's no better way to do it than that then i'd rather abandon the problem and move onto something more productive

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Driv3MeFar
Use a 1 dimensional array and adjust your n indices to index into it accordingly, or just use a boost::multi_array.


Is your first suggestion basically the same as what I said?

also.. boost: multi_array doesn't seem to do what I want. all the calls to the array are still very dimension-specific. I don't even understand how you could make a call to an array of arbitrary dimensions.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!