Can you use a string variable name as an array identifier?

Ok, I'm not sure if this is even possible with c++, but I've had no luck with searching online. Before I begin, I've only had one computer programming class in school so my skills aren't too impressive. I am currently working on an independent game that runs in a terminal window. I originally had one very large array of chars that was my "world" or "map". But the screen flickered briefly each time the player moved. I assumed it was because of the time it took for the program to run through the entire array, which was about [200][800]. So I then made many smaller arrays in order to prevent this flickering. I now had 144 arrays that make up a 12x12 grid. I'm using two int variables to keep up with what board the player is currently in. My problem is, I don't want to have to write the same code 144 times for each array. If I want to add anything new in, I have to write it for every case. I'm already using a string variable that I test with multiple if statements to decided which array needs to be changed. I would like to know if there is anyway to write my code so that my string variable name can be used as the identifier for an array. I hope this question makes sense. If there is a way to do this, could someone please point me in the direction of some reliable information. Thanks!

1
2
3
4
5
6
7
8
9
char arrayOne[10][10];
char arrayTwo[10][10];
char arrayThree[10][10];

string currentZone;

for(int i = 0; i < 10; i++)
    for(int j = 0; j < 10; j++)
    cout << currentZone[i][j];
Instead of multiple separate arrays, you might use a 3D array. However, multi-dimention arrays can become cumbersome to work with, so you might consider defining a class or struct to contain your grid, then have an array of those objects.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

using namespace std;

const int gsize = 10;

struct grid {

    char array[gsize][gsize];
    
    void init(char ch)
    {
        for (int i = 0; i < gsize; i++)
            for (int j = 0; j < gsize; j++)
                array[i][j] = ch;
    }
    
    void display()
    {
        for (int i = 0; i < gsize; i++)
        {
            for (int j = 0; j < gsize; j++)
                cout << array[i][j];
            cout << endl;
        }
    }
    
};


int main()
{
    grid zones[3];
    
    zones[0].init('*');
    zones[1].init('+');
    zones[2].init('#');
    
    int currentZone = 1;
    
    zones[currentZone].display();
    
}

Output:
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
If I use this method, the array would only consists of one single char in every spot. Here is a media fire link to the download for my source code. It's very lengthy, but hopefully you will see what I'm trying to do. http://www.mediafire.com/view/yn0eu8h64cuc44d/LegendIndev.cpp
I want to be able to use different chars in each array. And I also want to have moving chars that would represent npc's or the player. All of the arrays are the same, just skip past all of that.
Of course. I understood that your actual code didn't look like this. I simply posted it as an example. Please feel free to modify (or indeed ignore) it as you wish.
Last edited on
How would I modify it though? I'm sorry if I'm missing something obvious. You use a char as a parameter that you then use to fill the array. How would I use a function like that which initializes an array with multiple chars in different places.
Well, I just took a glance at your full code. It is, as you yourself said, very long.
I didn't have time to read every line yet. Some of the lengthiness may be a simple consequence of what your code needs to do. However, mostly it looks long because lots and lots of separate arrays are used, and there has to be individual blocks of code to deal with each one.

If instead there was an array to contain all the individual arrays, at least some of the code would get shorter and simpler, I'm sure. As I suggested before, either a 3D array or an array of objects would be a possible way to redesign the code.

From my point of view it's unfortunate that you got so far with this particular design, as I don't feel like wading through thousands of lines of code here (at least not unless it was part of my paid job) in order to re-write it. Nothing personal intended by that.

I completely understand. And don't think I've spent hours and hours on this one program. I simply made a basic model and copy/pasted it many times. Could you give me an example of a how a 3D array would work? We barely touched on 2D arrays in my class, so I've had to do a lot of research on my own. And as you can probably imagine, wading through site after site can still leave someone without a good answer. That's why I'm trying the forums here.
Thank you for taking the time that you did to help me. I really appreciate it.
Well, I've had another look at the code, even attempted to run itn, in order to try to understand how it is supposed to operate, but it didn't compile.

Because I couldn't really get it to run (well I did by commenting out huge sections of the code with /* */, I'm not sure what behaviour the program is supposed to exhibit.

When I first glanced at the code, I thought each of the 144 arrays was different, thus giving some sort of animation, but all I see here is lots of apparently identical arrays. I'm very much wondering whether a single array would not be better. Or at most, two arrays, one to contain the outline, and a second version which might have various characters added to it.

In other words, I'm thinking in terms of a somewhat different approach (with the aim of simplifying and shortening the code) but in order to know whether that is feasible, I need a clearer picture of what it is you are trying to do.

One thing I do suggest. Get rid of system("cls") and "pause". If you want a nice responsive program, these will just slow it down horribly, as well as being generally regarded as bad practice.

Something you may find useful is to reposition the cursor to the top left corner of the screen, then you don't need to clear the screen, as each fresh output will overwrite whatever was there before.
For windows:
1
2
3
4
5
6
7
8
9
10
void gotoxy(int x, int y)
{
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

	_COORD pos;
	pos.X = x;
	pos.Y = y;

	SetConsoleCursorPosition(hConsole, pos);
}
The best way to explain how I want the game to function is to compare it to the first Legend of Zelda game. It is supposed to be a map with 144 different areas. Each array is its own area, and I eventually plan on customizing them so that they aren't all the same. When the player crosses from one area to another, the game will display only the zone that the player is currently in. I was going to make one large array, but the screen flickered every time I moved. I assumed it was due to the size of the array, and the amount of time it took the program to cycle through the code. I don't get a flicker when I use many smaller arrays, but I have the added hassle of writing everything 144 times.

Here is the other version which contains one large array. You can only travel through one boxed area, but you should see the flickering that I'm talking about. I would much rather use this method because It would cut out an enormous amount of what I consider unnecessary code.
http://www.mediafire.com/view/599gam0ffcf5u71/LegendIndev1.0.cpp

If you know why it acts this way, I would love to know. I hope I'm wrong. Maybe it's a fixable problem. Also, I don't really understand how to read through the last bit of code you posted. I've never seen HANDLE before. But it sounds like you are suggesting to repeatedly cout the updated array without ever clearing the old one. The reason for the pause is so that the screen doesn't flicker while the player doesn't move. The loop cycles as quickly as possible, and it does not wait for input.

Thanks again for your help.
Last edited on
Also, the reason I use the array tempArray is to try to make it so that the screen won't flicker, but it did not work.
I've still not had much time to look at this fully, but I wanted to catch up a little by clarifying some of the ideas I mentioned previously.
The original code specified a whole lot of individual arrays, individually initialised with identical content. Just that first part of the code without any additional logic took about 4750 lines of code.

The first step I would take would be to have just a single copy of the array contents, and then copy it to each of the arrays at the start. Here's an example showing that, but bear in mind this is just a first step and we can do much better:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <windows.h>

using namespace std;

const int high = 29;
const int wide = 80;

char ZeroZero[high][wide] = {
    "                               x^^^^^^^^^^^^^^^x                               ",
    "                               x               x                               ",
    "  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx               xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "xxx                                                                         xxx",
    "<                                                                             >",
    "<                                                                             >",
    "<                                                                             >",
    "<                                                                             >",
    "<                                                                             >",
    "xxx                                                                         xxx",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx               xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  ",
    "                               x               x                               ",
    "                               xvvvvvvvvvvvvvvvx                               "
};


char OneOne[high][wide];
char OneTwo[high][wide];
char OneThree[high][wide];
char OneFour[high][wide];
//---------- 137 lines omitted here ---------------------
char TwelveTen[high][wide];
char TwelveEleven[high][wide];
char TwelveTwelve[high][wide];

void init();
void copy(char dest[][wide], char src[][wide]);
void show(char arr[][wide]);

int main()
{
    init();
    show(FourThree);

    return 0;
}

void init()
{
    // initialise all the arrays.
    copy(OneOne, ZeroZero);
    copy(OneTwo, ZeroZero);
    copy(OneThree, ZeroZero);
    copy(OneFour, ZeroZero);
//--------------------- 137 lines omitted here ---------------
    copy(TwelveTen, ZeroZero);
    copy(TwelveEleven, ZeroZero);
    copy(TwelveTwelve, ZeroZero);
}

void copy(char dest[][wide], char src[][wide])
{
    for (int row=0; row<high; row++)
        for (int col=0; col<wide; col++)
            dest[row][col] = src[row][col];
}

void show(char arr[][wide])
{
    for (int row=0; row<high; row++)
        cout << arr[row] << endl;
}
Last edited on
Now the above code is still very long - I actually missed out 274 lines of the program in order to get it to fit on this forum.

Now let's replace those 144 separate 2D arrays with a single 3D array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <windows.h>

using namespace std;

const int high = 29;
const int wide = 80;
const int size = 144;

char ZeroZero[high][wide] = {
    "                               x^^^^^^^^^^^^^^^x                               ",
    "                               x               x                               ",
    "  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx               xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "xxx                                                                         xxx",
    "<                                                                             >",
    "<                                                                             >",
    "<                                                                             >",
    "<                                                                             >",
    "<                                                                             >",
    "xxx                                                                         xxx",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  x                                                                         x  ",
    "  xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx               xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  ",
    "                               x               x                               ",
    "                               xvvvvvvvvvvvvvvvx                               "
};


char board[size][high][wide];

void init();
void copy(char dest[][wide], char src[][wide]);
void show(char arr[][wide]);

int main()
{
    init();
    show(board[5]);

    return 0;
}

void init()
{
    // initialise all the arrays.
    for (int i=0; i<size; i++)
        copy(board[i], ZeroZero);
}

void copy(char dest[][wide], char src[][wide])
{
    for (int row=0; row<high; row++)
        for (int col=0; col<wide; col++)
            dest[row][col] = src[row][col];
}

void show(char arr[][wide])
{
    for (int row=0; row<high; row++)
        cout << arr[row] << endl;
}

Now that does the job of initialising all 144 arrays, and displaying one of them as an example.

In practice I'd probably make the board into a class rather than a bare character array, but that's another topic really.
Last edited on
Topic archived. No new replies allowed.