alesbe / sorting-visualizer

Sorting visualizer made with SFML and C++ 📊
MIT License
17 stars 11 forks source link

Loading a sortType that doesn't exists stops the program #2

Closed alesbe closed 2 years ago

alesbe commented 2 years ago

Describe your problem:

When you try to select a sorting algorithm that does not exists and load it, the program just stops and can't do anything. Restarting the program solves the issue.

Steps to reproduce:

Possible fix (optional):

Make sure that sortType it's associated to a sorting algorithm in sf::Keyboard::Up and sf::Keyboard::Down events, in main.cpp

OS

ghost commented 2 years ago

There's great temptation to over-engineer a solution to this. I can handle this too I believe, if you would like me to.

alesbe commented 2 years ago

I was thinking about reusing Utils::getSortType() to solve this, right now the function returns a string with the name of the algorithm, used to display it when changing the algorithms with the arrows.

My first thought was to reuse it by checking if the length of the return (string) is higher than 2, because the number of algorithms will probably be less than 99, and use that as a validation when starting the sort process or changing the algorithm.

Also adding a different function or variable containing the number of algorithms will be another step into adding new algorithms.

I don't want to over complicate this, but maybe there are better approaches to solve this issue!

ghost commented 2 years ago

I had similar thoughts of using a sentinel value, and then hiding that with a simple function to check. c:

Something similar to:

const std::string kNoSort = "<NO ALGORITHM>";

std::string Utils::getSortType(int sortType) {
    switch (sortType)
    {
    case 0:
        return "Bubble sort";
    // and further cases...
    default:
            return kNoSort;
    }
}

bool Utils::hasNextSortType(int sortType) {
    return getSortType(sortType + 1) != kNoSort;
}

While in main.cpp

// Change sort type (increase)
case sf::Keyboard::Up:
    if (sortType >= 0 && Utils::hasNextSortType(sortType)) {
        // omitted...
    }
    break;

What are your thoughts?

alesbe commented 2 years ago

Awesome! Go ahead if you want

alesbe commented 2 years ago

PR #5 fixed this issue, now you can't select a non-existent algorithm, closing this issue.

Thanks!