0

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.

CootMoon
  • 522
  • 4
  • 22
  • 9
    Just use a `vector rectColors;`. Then the index `i` corresponds to the `i`th colour. – cigien Jun 04 '20 at 16:39
  • Ok. I am very new to using c++. But looking into vectors that dose seem way easier then what I was trying to do. Thank you for your help. @cigien – CootMoon Jun 04 '20 at 16:47
  • No problem. Happy to help. However, I strongly recommend finding a good [book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and trying to learn the language that way. It'll be much easier than using SO, that's for sure :) – cigien Jun 04 '20 at 16:49
  • @cigien ya, definitely. Was just stumped on this problem for a while and couldn't find the right words to google what I wanted to do. – CootMoon Jun 04 '20 at 16:53
  • 2
    Side note: if you have a fixed number of variables known at compile time, consider using [`std::array`](https://en.cppreference.com/w/cpp/container/array) as well. Because the size is fixed there is less overhead than what's needed by the dynamically sized `std::vector`.. – user4581301 Jun 04 '20 at 16:54
  • 1
    FWIW, I call this "variable variables". I think I got it from PHP. Doesn't matter though because C++ doesn't support it. But the term may have helped in your research. – Asteroids With Wings Jun 04 '20 at 17:28

1 Answers1

1

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 ith 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

CootMoon
  • 522
  • 4
  • 22