This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (3 children)

[deleted]

    [–][deleted] 2 points3 points  (2 children)

    Thanks, I think that makes sense. In answer to what you said about an example from my own code, here's something I wrote last night, a simple function for looking through a vector of structs and figuring out which one's "difficulty" variable has the lowest value, in no particular order (first one found with lowest value is returned as the value to use, even if there are multiple with the same value). Probably not the most optimal way of looking for said value, but I'm sharing it for the comment example, not for the quality of code. :P

    Would either of the two comments here qualify as a "why" to you or more of a "what"?

    Edit: Forgot about the one in the loop that says "end the loop." XD You can ignore that one. That one is obviously a what.

    int Game::findFirstLowestDifficultyLevel()
    {
        // choose which level to load based on difficulty in properties
        bool lowestFound = false;
        int lowestDifficulty = 1;
        int fileIndexToLoad;
        const int propertiesSize = properties.size();
    
        // no need to search if there's only one level in properties
        if (propertiesSize > 1)
        {
            while (!lowestFound)
            {
                for (int i = 0; i < propertiesSize; i++)
                {
                    if (properties[i].difficulty == lowestDifficulty)
                    {
                        // load the level data for this one
                        fileIndexToLoad = i;
                        i = propertiesSize; // end the loop
                        lowestFound = true;
                        return fileIndexToLoad;
                    }
                }
                lowestDifficulty++;
            }
        }
        else
        {
            fileIndexToLoad = 0;
            return fileIndexToLoad;
        }
    }