Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create RandomEvent helper function #140

Closed
tagniam opened this issue Oct 27, 2020 · 1 comment
Closed

Create RandomEvent helper function #140

tagniam opened this issue Oct 27, 2020 · 1 comment

Comments

@tagniam
Copy link
Owner

tagniam commented Oct 27, 2020

Here is how we currently calculate random damage (from GiantCrab.cpp):

int GiantCrab::ReturnRiskAttackDamage() {
    int selector = Common::RandomInt(0, 5);
    switch(selector) {
    case 0:
    case 1:
    case 2:
    case 3:
        return 0;
        break;
    case 4:
    case 5:
        return 20;
        break;
    default:
        return 0;
        break;
    }
}

This basically means:

  • 4/6 chance of doing 0 damage
  • 2/6 chance of doing 20 damage

We'd like to create a helper method to avoid doing this. This is what I had in mind:

We'd have to create a function with the signature:

template<class T>
T RandomEvent(int denominator, vector<int> numerators, vector<T> outcomes)

where it must be that sum(numerators) == denominator and numerators.size() == outcomes.size(). The function takes in generic outcomes, so that even things like functions could be decided randomly.

With this helper function, the GiantCrab function could be rewritten as:

int GiantCrab::ReturnRiskAttackDamage() {
    // 4/6 chance of doing 0 damage
    // 2/6 chance of doing 20 damage

    int denominator = 6;
    vector<int> numerators{4, 2};
    vector<int> outcomes{0, 20};
    return RandomEvent<int>(denominator, numerators, outcomes);
}

This helper function should be placed in the Common class. If you have another design in mind, or know any existing STL libraries that can do this, let me know - we can probably do better than this 😛

@DJ73
Copy link
Contributor

DJ73 commented Oct 27, 2020

I'd like to work on this.
I'm sure that there might be better ways to do this depending from case to case but it sounds like a good solution for a generalised situation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants