0

I'm doing a C++ tutorial that teaches the language through game development. Within this tutorial there's a piece of code which I do not understand how it works.

First an enum class is declared and an array is initialized:

enum class side {LEFT,RIGHT, NONE};
side branchPositions[NUM_BRANCHES]; / which is a const variable and has the value of 6

Within the main function I have this code:

        for (int i = 0; i < NUM_BRANCHES; i++)
        {
            float height = i * 150;

            if (branchPositions[i] == side::LEFT)
            {
                branches[i].setPosition(610, height);
                branches[i].setRotation(180);
            }
            else if (branchPositions[i] == side::RIGHT)
            {
                branches[i].setPosition(1330, height);
                branches[i].setRotation(0);
            }
            else
            {
                branches[i].setPosition(3000, height);
            }
        }

What it does is, it updates the position of the branch sprites.

When running the code, I get the following result: example 1

After the following function is added and called

void updateBranches(int seed)
{
for (int j = NUM_BRANCHES - 1; j > 0; j--)
{
branchPositions[j] = branchPositions[j - 1];
}

srand((int)time(0) + seed);
int r = (rand() % 2);

switch (r)
{
case 0:
branchPositions[0] = side::LEFT;
break;
case 1:
branchPositions[0] = side::RIGHT;
break;
default:
branchPositions[0] = side::NONE;
break;
}

}

the branches get distributed randomly, like so: enter image description here

Now, I do not get why this is. I do understand why the branch at position 0 is either left, right or not visible due to the switch statement. But I don't understand how the for loop in the function interacts with the array and why this leads to the behavior shown in image 2.

I also do not understand what values are stored in the array and what the connection with the enum class is.

Could somebody please clarify ? Thanks

5
  • Do you ever initialize the branchPositions array before calling updateBranches? You may just be reading garbage from memory. Commented Mar 5, 2020 at 16:38
  • yes it's initialized before running the main loop and outside of the main function to have global scope. Commented Mar 5, 2020 at 16:42
  • Also a couple unrelated comments: 1) The default case in updateBranches is unreachable. 2) Using time(0) + seed defeats the purpose of being able to specify a seed at all - seeds are used to make your random numbers reproducible, but the time(0) counteracts that. Commented Mar 5, 2020 at 16:43
  • Can you edit your post to include the code that does that? Commented Mar 5, 2020 at 16:44
  • there's a typo which makes the default unreachable, it should be (rand()%5) instead of 2 Commented Mar 5, 2020 at 16:46

1 Answer 1

1

The first for loop in updateBranches will move the branches up the array, so that the branch that was in branch[4] will be stored in branch[5], all the way down to storing branch[0] into branch[1]. Then branch[0] is replaced with a new randomly chosen branch. Except that srand should only be called once, and not every time the function is called. And it should use % 3, not % 2, since % 2 will only give you values of 0 or 1.

Sign up to request clarification or add additional context in comments.

4 Comments

great, that clarifies one part. But what values are actually stored in the array when its first initialized? Looking at image 1 it seems that it"s only "Left". I assumed that it should be [left,right,none,left, right,none] b
@natzer The declaration for branchPositions shown will zero initialize it if it is a global variable (which will set everything to side::LEFT). Do you ever initialize its values to something else before you start your game loop?
no, only the for loop of the updateBranches function is running over it prior to the main loop
thanks for all the answers. I got it now. Actually the array and it's values are only secondary to the branch positioning. The only thing that influences the position is the updateBranches function which continiously pops out branch[0] and replaces it with a random one. By calling the function multiple times, all branches are replaced by a random one (as explained by 1201ProgramAlarm).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.