Tuesday, October 25, 2011

c0dehammer v0.1a - Combining "216" with Rerolls

I am putting 216 in quotations because as we have discovered earlier... computers do not like fractions. The purpose of this lesson, is to combine what we have learned about rerolls and to combine that into the probability calculator we have already built. The easiest way to go about this is to copy and paste our code from the reroll calculator, make it its own function within the probability calculator and then get it to work. So without hesitation here it is:

#include <iostream>
using namespace std;

float reroll(float x)
{
    char a;
  
    cout << "Reroll? (y/n): ";
    cin >> a;

    if (a == 'y')
    {
        x = x / 6.0;
        x = x + ((1 - x) * x);
        return x;
    }

    else
    {
        x = x / 6.0;      
        return x;
    }
}  
  
int main()
{
      
    float hit, wound, save, result = 0.0;
  
    cout << "216 Probability Calculator" << endl;
  
    cout << "# of sides needed to Hit: ";
    cin >> hit;
    hit = reroll(hit);

    cout << "# of sides needed to Wound: ";
    cin >> wound;
    wound = reroll(wound);

    cout << "# of sides needed to Fail Save: ";
    cin >> save;
    save = save / 6.0;
  
    result = (hit * wound * save);
  
    cout << "Probability: " << result << endl;
  
  
    return 0;
}   
The first thing you will notice is this section here:

float reroll(float x)
{
    char a;
  
    cout << "Reroll? (y/n): ";
    cin >> a;

    if (a == 'y')
    {
        x = x / 6.0;
        x = x + ((1 - x) * x);
        return x;
    }

    else
    {
        x = x / 6.0;      
        return x;
    }
}    
It is almost exactly like our previous reroll calculator, with a few exceptions and less fluff. Although we could have used some IF statements after every input for hit, wound, and save, and then pasted in our piece of code, it makes much more sense to do the work only once. What happens is after we input each selection in the main() function we call up our reroll function.

cout << "# of sides needed to Hit: ";
cin >> hit;
hit = reroll(hit);
What this essentially does  is assign the value of hit to equal the the value returned by the reroll, however reroll also needs to know what we entered to hit, to come up with the modified answer. So we input hit, and then call up the function of reroll using the variable of hit, which returns the modified value of hit, and assigns it to the variable of hit overwriting what we put in. Sounds confusing yes, but trust me it makes sense. 

float reroll(float x)
What this indicated is that in order to run the function reroll() it needs an input value. This is where the reroll(float x) comes into play. The float before that indicated that the number returned will also be a float (in this case a percentage).

char a;
  
    cout << "Reroll? (y/n): ";
    cin >> a;
This is the next set of code we will concern ourselves with. In short we create a char variable (which is one single character) name a. We ask the user if they would like to reroll, they enter in "y" or "n" to the variable a. Well actually we will learn that it only matters if you enter in "y", this isnt yet user friendly and yes you can crash the program. Our next concern is this section in here:

if (a == 'y')
    {
        x = x / 6.0;
        x = x + ((1 - x) * x);
        return x;
    }

    else
    {
        x = x / 6.0;      
        return x;
    }
What we have done here is rather simple. If a equals a 'y' then we proceed with our reroll adjustment, and then return that result to the main() function. If it does not it divides x by 6. Well you might ask why we bother doing that. By dividing it by 6 we can turn it into a percentage value, and then we no longer have to divide our hit * wound * save by 216. If we did not do that then we would have to create a variable within main() to be our divisor. The calculation will still work with mixed percentages and fractions, but the divisor will change.

To Hit(%) * To Wound (fraction) * To Save (fraction) / 36
To Hit(%) * To Wound (%) * To Save (fraction) / 6

... and so on and so forth. Its just more logical to convert everything to one value type within a program. On paper do either or, or both.

The next noticeable thing will be this:

cout << "# of sides needed to Fail Save: ";
cin >> save;
save = save / 6.0;
No I have not gone crazy. This reroll will not work right with the save. Well it does, just opposite. For example: Our opponent has a 4+ save, which means we enter a 3 to fail. If he gets a reroll he now has a 75% chance of failing as opposed to a 50% chance. Its backwards. But that augmentation will be for the next post. Hopefully I have explained this well enough for you. Good Luck.

No comments:

Post a Comment