954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Multi-dimensional array help

Back with another question concerning my RPG. I had the idea to do a multi-dimensional array to represent a map, with each element in the array being it's own square, Depending on where you are, you can move in any of the 4 directions, unless you are on an edge. Also, I wanted to have specific squares represent towns and such.

The only problem is I'm not very good with arrays. I can initialize it, but that's about it. I just need some suggestions to get me going in the right direction.

Ellisande
Junior Poster in Training
53 posts since Jun 2008
Reputation Points: 10
Solved Threads: 2
 

You could create a class that handles the boundaries of your multi-dimension array.

My question is this -- are you planning on having only 2 dimensions for the map or 3 in the event that you want to do some kind of map-switch?

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 
My question is this -- are you planning on having only 2 dimensions for the map or 3 in the event that you want to do some kind of map-switch?

Just 2, a chessboard with special squares (only bigger of course)

er, now that I think about it though, is it possible to have like dungeons or something that have their own specific map layouts?

Ellisande
Junior Poster in Training
53 posts since Jun 2008
Reputation Points: 10
Solved Threads: 2
 

Just 2, a chessboard with special squares (only bigger of course)

er, now that I think about it though, is it possible to have like dungeons or something that have their own specific map layouts?

Of course. It's your multi-dimension array, you can set it up any way you like.

You're going to want to do this the easy way... and when I mean the easy way I mean having your map already built in a .txt file then read the characters into a 2D char array...

#ifndef MAPXSIZE
#define MAPXSIZE 8
#endif
#ifndef MAPYSIZE
#define MAPYSIZE 8
#endif

typedef char MapArray[MAPXSIZE][MAPYSIZE] = {0};

MapArray firstMap, dungeon1Map, dungeon2Map; //... however many maps that you may need

//code to fill map with chars from .txt file
Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

Why would you want to fill up the array with chars though?

Ellisande
Junior Poster in Training
53 posts since Jun 2008
Reputation Points: 10
Solved Threads: 2
 
Why would you want to fill up the array with chars though?

You're trying to display your map on the screen correct?

Doing so with characters is your best bet unless you're using Windows API and you're forming an application.

You'll have to give me more details on your program - I'm thinking that you're making a text-based RPG to be seen in the Console screen, so the first thing that came to mind were pre-placed characters from a text file.

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

a char will hold a single character, which is just an 8-bit int.

what information are you trying to contain in any 'square'?

this doesnt seem very meaningful, to have one char value in any square...

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 
You'll have to give me more details on your program - I'm thinking that you're making a text-based RPG to be seen in the Console screen, so the first thing that came to mind were pre-placed characters from a text file.

Oh I see. Well my original intent was to start with just a basic coordinate system, with no visible map displayed on the screen. As you move, it would display what square you moved to by outputting your coordinates. And the last feature I wanted was to have certain squares be represented by towns (and eventually dungeons), so for now I want to keep it simple until I've learned more in C++.

---|---|---|---
---|---|---|---
---|---|---|---
---|x | t |---

x <- you are here. Then you're given the option to move north, east or west.
t <- town here. Step on this square and you'll get to do town things like store/inn.

Ellisande
Junior Poster in Training
53 posts since Jun 2008
Reputation Points: 10
Solved Threads: 2
 
Doing so with characters is your best bet unless you're using Windows API and you're forming an application.


this doesnt make any sense. what's any of this got to do with Windows API?

you've got an 8x8 matrix of single characters... what meaningful information can you have in such a thing?

you should make an 8x8 matrix of a structure that describes every important aspect of any board position.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

Oh I see. Well my original intent was to start with just a basic coordinate system, with no visible map displayed on the screen. As you move, it would display what square you moved to by outputting your coordinates. And the last feature I wanted was to have certain squares be represented by towns (and eventually dungeons), so for now I want to keep it simple until I've learned more in C++.

---|---|---|--- ---|---|---|--- ---|---|---|--- ---|x | t |---

x <- you are here. Then you're given the option to move north, east or west. t <- town here. Step on this square and you'll get to do town things like store/inn.

Then you just want to display the numbers and maybe display the numbers of the valid locations to move to next?

In that case I really don't think you need arrays or file i/o. You just need to have predefined coordinates for places.

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

Then you just want to display the numbers and maybe display the numbers of the valid locations to move to next?

In that case I really don't think you need arrays or file i/o. You just need to have predefined coordinates for places.

Yeah... actually now that I think about it, that sounds like it would work. I could do if(x=7, y=2){ type statements to represent towns, and increment/decrement x/y coordinates depending on which direction you pick.

I'm just not sure what the most efficient way to define the borders is, to make sure that the x/y coordinates won't get higher or lower than I want them to.

Ellisande
Junior Poster in Training
53 posts since Jun 2008
Reputation Points: 10
Solved Threads: 2
 

this doesnt make any sense. what's any of this got to do with Windows API?

you've got an 8x8 matrix of single characters... what meaningful information can you have in such a thing?

you should make an 8x8 matrix of a structure that describes every important aspect of any board position.

True. You can have a struct that holds the x and y information of the board.

Unfortunately I wasn't given enough information at the start other than the fact that the original poster wanted a map in a text-based RPG and wanted the characters capable of moving around in the array without going out of the bounds.

My first suggestion was a restricted array (or class) that would not allow the user to go out of bounds.

I also assumed that the original poster wanted to visually understand what was going on and had enough C++ experience to use file I/O and represent the map as a set of characters.

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

Yeah... actually now that I think about it, that sounds like it would work. I could do if(x=7, y=2){ type statements to represent towns, and increment/decrement x/y coordinates depending on which direction you pick.

I'm just not sure what the most efficient way to define the borders is, to make sure that the x/y coordinates won't get higher or lower than I want them to.

You don't need a 2D array of chars or anything else, just an array of structs and a starting-location for your character.

You'll probably also want to make a switch statement (or if statements) that handles the possible moves for the character.

typedef struct Location
{
      int x, y;
      Location(int col, int row) : x(col), y(row){}
};

Location Inn(4, 3), TrainingArea(2, 2), Gate(0, 4);
Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

Okay, I think I get it now, the only question I have is what does the : x(col), y(row) - on part mean?

Ellisande
Junior Poster in Training
53 posts since Jun 2008
Reputation Points: 10
Solved Threads: 2
 
Okay, I think I get it now, the only question I have is what does the : x(col), y(row) - on part mean?

Pre-initialization - because the assignment operator causes a copy of the object to be made during the assignment, which can cause the program to do more work than it should.

Pre-initializing is considered good programming practice.

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 
#include <iostream>

using namespace std;

	typedef struct Location
	{
		int x, y;
  		Location(int col, int row) : x(col), y(row){}
	};

Location Start(2,2);

int main(){
    cout<< Location.start;
	cin.get();
	}


I'm doing something wrong =\

Ellisande
Junior Poster in Training
53 posts since Jun 2008
Reputation Points: 10
Solved Threads: 2
 
#include <iostream>

using namespace std;

	typedef struct Location
	{
		int x, y;
  		Location(int col, int row) : x(col), y(row){}
	};

Location Start(2,2);

int main(){
    cout<< Location.start;
	cin.get();
	}

I'm doing something wrong =\

Location is just a typedef for Locations. You can use the word Location to create coordinate objects, for example--

Location Inn(3, 4); //creates a location struct named Inn with x set to 3 and y to 4


Furthermore, the variable/function "start" doesn't exist in Location, and you cannot call Location in that way since its just a definition for anything that is that type.

In your above example, Start IS a location struct, so you can call upon its coordinates in this way--

cout << Start.x << ", " << Start.y << endl;
Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

Aha! I see now, it's just as if I were using a variable in a class, except with the structure name. And the typedef struct Location is making a structure that has x,y coordinates.

With that said, it sounds like it will be simple enough to make a switch that lets you choose which direction you go, with an if inside it that checks if it is on the border or not.

Ellisande
Junior Poster in Training
53 posts since Jun 2008
Reputation Points: 10
Solved Threads: 2
 

Now the problem I'm having is how do I use a struct like Location Inn like you described above to give you different options depending on your location in the grid? I'm able to move around my grid from 1-10 in both the x/y directions, but that's all I've got so far.

Ellisande
Junior Poster in Training
53 posts since Jun 2008
Reputation Points: 10
Solved Threads: 2
 
Now the problem I'm having is how do I use a struct like Location Inn like you described above to give you different options depending on your location in the grid? I'm able to move around my grid from 1-10 in both the x/y directions, but that's all I've got so far.

You should have an if statement that takes into account many cases while you are moving (I'm assuming you're moving in a loop).

while(true /*or whichever condition you use to keep movement going*/)
{
       //...
       if(currentX == Start.x && currentY == Start.y)
       {
               //do something
       }       


       //...

}


Assuming that you want to do it the fairly easy way.

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You