Reputation: 512
So, I have some variables declared as follows:
int rect1Color;
int rect2Color;
int rect3Color;
...
int rect63Color;
int rect64Color;
I need to change each of these variables based on a loop that looks like this:
for (int i = 0; i < sizeof(playPos) / sizeof(char*); ++i) {
const char* TEMP = playPos[i];
if (TEMP != " x" && TEMP != " o" && TEMP != "xx" && TEMP != "oo") {
if (TEMP == " p") {
rect[i+1]Color = 1;
}
else {
rect[i+1]Color = 2;
}
}
else if (TEMP == " o" || TEMP == "oo") {
rect[i+1]Color = 3;
}
else if (TEMP == " x" || TEMP == "xx") {
rect[i+1]Color = 4;
}
}
That draws from this data set:
const char *playPos[64] {
" ", " o", " ", " o", " ", " o", " ", " o",
" o", " ", " o", " ", " o", " ", " o", " ",
" ", " o", " ", " o", " ", " o", " ", " o",
" ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ",
" x", " ", " x", " ", " x", " ", " x", " ",
" ", " x", " ", " x", " ", " x", " ", " x",
" x", " ", " x", " ", " x", " ", " x", " "
};
The data set and logic all work, I just can't find a simple way to set the values of the variables.
Upvotes: 0
Views: 75
Reputation: 512
The solution to my problem was turning my long list of ints into one vector/array.
So instead of:
int rect1Color;
int rect2Color;
int rect3Color;
...
int rect63Color;
int rect64Color;
I now have:
int rectColor1[64];
User "cigien":
Just use a vector<int> rectColors;
. Then the index i
corresponds to the i
th colour.
User "user4581301":
Side note: if you have a fixed number of variables known at compile time, consider using std::array
as well. Because the size is fixed there is less overhead than what's needed by the dynamically sized std::vector
Upvotes: 1