• Advertisement

Archived

This topic is now archived and is closed to further replies.

how do i initialize an array if its part of a class?

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

high. im working on a pac-man clone, and im using 32x32 tiles. anyway, i have an array called map[19][25] which is an element for each tile to fill an 800x600 screen. right now i have it as a global variable, but i want to put it inside of one of my classes. the problem is, i want to assign values to it when i initialize it, IE, i want to do this
int map[19][25] = 
{{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{ 1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1},
{ 1,0,1,1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,0,1},
{ 1,0,1,1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,0,1},
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,1},
{ 1,0,1,1,0,1,0,1,1,1,0,1,1,1,0,1,1,1,0,1,0,1,1,0,1},
{ 1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1},
{ 1,1,1,1,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,0,1,1,1,1},
{ 0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0},
{ 1,1,1,1,0,1,0,1,1,1,1,0,0,0,1,1,1,1,0,1,0,1,1,1,1},//10

{ 0,0,0,0,0,0,0,0,1,1,0,0,0,3,0,1,1,0,0,0,0,0,0,0,0},
{ 1,1,1,1,0,1,1,0,1,1,0,0,0,0,0,1,1,0,1,1,0,1,1,1,1},
{ 0,0,0,1,0,1,1,0,1,1,1,1,1,1,1,1,1,0,1,1,0,1,0,0,0},
{ 1,1,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,1,1},
{ 1,0,0,0,0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,0,0,0,1},
{ 1,0,1,0,0,0,0,0,0,1,0,1,1,1,0,1,0,0,0,0,0,0,1,0,1},			
{ 1,0,1,1,1,0,1,1,1,1,0,1,1,1,0,1,1,1,1,0,1,1,1,0,1},
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
i know how to use constructors, but how can i do something like map[what goes here?][what goes here?] = {{0,1,0,10} {more more more just as you saw above ^^^}} i just dont understand how i could assign values to every element in an array, AFTER it was initialized/declared. thanks for any help.

Share this post


Link to post
Share on other sites
Advertisement

int map[][];

map[5][6] =

{{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},

{ 1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1},

{ 1,0,1,1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,0,1},

{ 1,0,1,1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,0,1},

{ 1,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,1},

{ 1,0,1,1,0,1,0,1,1,1,0,1,1,1,0,1,1,1,0,1,0,1,1,0,1},

{ 1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1},

{ 1,1,1,1,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,0,1,1,1,1},

{ 0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0},

{ 1,1,1,1,0,1,0,1,1,1,1,0,0,0,1,1,1,1,0,1,0,1,1,1,1},//10


{ 0,0,0,0,0,0,0,0,1,1,0,0,0,3,0,1,1,0,0,0,0,0,0,0,0},

{ 1,1,1,1,0,1,1,0,1,1,0,0,0,0,0,1,1,0,1,1,0,1,1,1,1},

{ 0,0,0,1,0,1,1,0,1,1,1,1,1,1,1,1,1,0,1,1,0,1,0,0,0},

{ 1,1,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,1,1},

{ 1,0,0,0,0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,0,0,0,1},

{ 1,0,1,0,0,0,0,0,0,1,0,1,1,1,0,1,0,0,0,0,0,0,1,0,1},

{ 1,0,1,1,1,0,1,1,1,1,0,1,1,1,0,1,1,1,1,0,1,1,1,0,1},

{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},

{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};

Share this post


Link to post
Share on other sites
class whatever
{
int** map;
}

constructor(int map_width, int map_height)
{
map = new int[map_width][map_height];
}

and then methinks you can init in the same way

Or you can do the following.....
static int g_map[19][25] = your initial map

and keep a variable int** m_pmap in the class.
in the constructor m_pmap = g_map

[edited by - psamty10 on March 6, 2004 11:56:03 PM]

Share this post


Link to post
Share on other sites
You''re probably just filling the map array as quick way to see results, but you should really just write a map file loader and put it in a file, then you can call loadFile() in your constructor if you like. Its easy enough and you could even use a simple text file.

Ravyne, NYN Interactive Entertainment
[My Site][My School][My Group]

Share this post


Link to post
Share on other sites
Just write the values to a text file and read it in when your class constructor is called.

Share this post


Link to post
Share on other sites

  • Advertisement