Skip to main content
GameDev.net gamedev.net
🔒 Locked

Arrays of Strings?

Started by flip5577 Jun 30, 2004 at 11:55 PM 9 replies 1.9k views
Original Post
flip5577
flip5577
Im making a text version of pacman, the way ive designed the level is to be an array of strings. Example. the top line of the maze is all blocks so i made an array of chars that looks like this char line1[30] = BBBBBBBBBBBBBBBBBBBBBBBBBBBBB; The B stands for BLOCK. So i have completely converted the pacman maze into text. I wrote a function that converts the letters into characters that represent what they mean. My problem is, how do i make an array of strings? Like an Array that holds an Array of chars?
Larry CharlesCentra Entertainment Studios
Fruny
Fruny
std::vector > array ?
std::vector array ?
char array[nrows][ncols] ?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Alpha_ProgDes
Alpha_ProgDes
char multi[2][1] = {0};
char one[1] = 'c';
char two[1] = 'h';

char multi[0][0] = &one[0];
char multi[1][0] = &two[0];

...i believe.

play with it and see [smile]
Beginner in Game Development?  Read here. And read here.  
Fruny
Fruny
Alpha_ProgDes - no, that cannot work. You can't store a pointer into a char variable.

The easiest way for him is something like
const char data[][30] ={  "BBBBBBBBBBBBBBBBBBBBBBBBBBBBB",  "B                           B",  "B                           B",  "B                           B",  "B                           B",  "B                           B",  "BBBBBBBBBBBBBBBBBBBBBBBBBBBBB",};


Though he'll have to account for the extra NUL byte at the end of each C string when counting his columns. Which means that a vector of strings is probably even better.
std::vector<std::string> data;void build_map(std::vector<std::string>& data){  data.clear();  data.push_back("BBBBBBBBBBBBBBBBBBBBBBBBBBBBB");  data.push_back("B                           B");  data.push_back("B                           B");  data.push_back("B                           B");  data.push_back("B                           B");  data.push_back("B                           B");  data.push_back("BBBBBBBBBBBBBBBBBBBBBBBBBBBBB");}
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Pactuul
Pactuul
it might be easier to go with a 2d array of ints
where 1 = block, 2 = empty, 3 = pacman, etc

if the level sizes are constant and known before hand you could just write


int board[SIZE_X][SIZE_Y];


or if you just wanted to work with chars, (which I suggest instead of strings)

char board[SIZE_X][SIZE_Y];

you could also create them on the fly:

char** board;

then init it like so:
board = new char[SIZE];
for(i=0; i board = new char;

-Pac "The thing I like about friends in my classes is that they can't access my private members directly." "When listening to some one tell about their problem (whether it's code or not), don't listen to what went right or wrong, but what they assumed....."
Fruny
Fruny
Quote:
Original post by Pactuul
you could also create them on the fly:

char** board;

then init it like so:
board = new char[SIZE];
for(j=0; j board[j] = new char;


Your code is incorrect.

board = new char*[ROWS];for(int j=0; j<ROWS; ++j)	board[j] = new char[COLS];


Will work. And, please, please, please, when suggesting that solution, do rembember to also always explain how to do the cleanup afterwards.

for(int j=0; j<ROWS; ++j)	delete[] board[j];delete[] board;


The [] after delete are not optional.

Using C++ vectors is infinitely simpler and safer, and just as efficient.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Fruny
Fruny
Behold the wisdom of the C++ FAQ Lite (@parashift.com)

[16.15] How do I allocate multidimensional arrays using new?

[16.16] But the previous FAQ's code is SOOOO tricky and error prone! Isn't there a simpler way?

[34.1] Why should I use container classes rather than simple arrays?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
flip5577
flip5577
The array of ints helps but im still at a time saving problem

It took me about 40 min to text out the pacman maze and get it right. Is there a way i can set a whole array of ints to equal a bunch of numbers without doing each number manually?

for example

int row1[30]=12322312321111213211113323232;

?
Larry CharlesCentra Entertainment Studios
Fruny
Fruny
int row1[30]= { 1,2,3,2,2,3,1,2,3,2,1,1,1,1,2,1,3,2,1,1,1,1,3,3,2,3,2,3,2 };
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Jannes
Jannes
Hi,

To ur original post:

If u just need blocked/none blocked tiles.

I would suggest using a vector of boolean variables or a bitmask. A row or line of 10 tiles would be defined like this:

DWORD line1 = 0x0111000101

i think this is most efficient way.

u could easily load and save different maps using binary file format.
Fruny
Fruny
Quote:
Original post by Jannes
DWORD line1 = 0x0111000101

i think this is most efficient way.


While that does reduce the amount of data, it does increase the amount of work necessary to access that data. Unless you have a really huge data set, bit packing is not a good idea: 64kB already gives you a 256*256 map, 1MB gives you a 1024*1024 map. And even then, instead of using a scalar variable like that, you would use a vector which is packed.

Quote:
u could easily load and save different maps using binary file format.


If he's struggling with arrays, he's probably not ready to deal with binary IO.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.