Alright folks, you may all be wondering what this about and I will tell you. I recently started getting back into Warhammer 40k, and discovered this magical new concept known as Mathhammer. Mathhammer is basically calculated the probabilities of a given outcome within the confines of the Warhammer rules. I find this concept very interesting, and wish to take it on step forward -creating our own basic set of programs to do the calculations for us. It will all be open source and your free to modify it as you see fit. Note, I am not a professional programmer, nor have I even dabbled in it for years, and my understanding of the Warhammer rules may be off time to time, feel free to point these mistakes out!
Without further ado, lets go over what is known as the basic 216 rule. There is a wonderful explanation of it
here. Basically, three dice are rolled (to hit, to wound, to save,) in order to get a basic probability of the outcome.
(h * w * s) / 216 = x
h = # of sides of the d6 needed to successfully hit
w = # of sides of the d6 needed to successfully wound
s = # of sides of the d6 needed for the opponent to fail the save
x = the probability as a decimal
Therefore if a model needed a 3+ to hit, 4+ to wound, and the opponent saved on a 5+:
h = 4
w = 3
s = 4
(4 x 3 x 4) / 216 = 0.22 (repeating) or 22%
I wont go to much further into how this works, as there are plenty of resources already available to understand it. What I want to do is to turn this into a program. We will be using C++, because I am using Linux I have a built in C++ compiler. If you are not using Linux you will have to download a compiler.
Dev C++ works with Windows and is free, I am not familiar with Mac compilers but it should not be hard to find one. First I will give you the code:
#include <iostream>
using namespace std;
int main()
{
int hit, wound, save;
float result = 0;
cout << "216 Probability Calculator" << endl;
cout << "# of sides needed to Hit: ";
cin >> hit;
cout << "# of sides needed to Wound: ";
cin >> wound;
cout << "# of sides needed to Fail Save: ";
cin >> save;
result = (hit * wound * save) /216.0;
cout << "Probability: " << result << endl;
return 0;
}
Now if you dont understand C++ this will probably not make a lot of sense to you, but no worries as I will explain what is going on in this program.
#include <iostream>
using namespace std;
int main() {
return 0;
}
This is the basic framework of the program, for our purposes it not entirely important to know what everything does, just that this is how we set it up. If compiled this program will run, but will not do anything. The main() function is basically that, its the function that is executing the program, and whatever is inside those parenthesis. Now that we have that out of the way lets concentrate and getting this program to do something
#include <iostream>
using namespace std;
int main()
{
cout << "216 Probability Calculator";
return 0;
}
What this will do if compiled is display:
216 Probability Calculator on the screen. Well thats all fine and dandy but dosent really DO anything for us, other then teaches us that the cout command will print whatever is in those quotations on the screen for us. If you want a better explanation I encourage you to look up some C++ tutorials. Ok well lets look at the next stage in the code, and see if we can make sense of it.
#include <iostream>
using namespace std;
int main()
{
int hit, wound, save;
float result = 0;
cout << "216 Probability Calculator" << endl;
cout << "# of sides needed to Hit: ";
cin >> hit;
cout << "# of sides needed to Wound: ";
cin >> wound;
cout << "# of sides needed to Fail Save: ";
cin >> save;
return 0;
}
We know that the cout is going to print whatever is inside those quotations on the screen, but what about this new cin command? I am sure you have assumed that if cout puts stuff on the screen, cin must surely be putting something in the screen. Well not totally wrong but to understand it better we will start with variables. Variables are exactly how you remember them from math class, variables are "x". In programming languages however there can be many different types of variables.
int hit, wound, save;
float result = 0;
This is us declaring our variables. We are creating integer variables (ie whole numbers, but I will explain the difference later). In this case we are creating three integer variables hit, wound, and save. It would be the same as programming this:
int hit;
int wound;
int save;
But instead of wasting all that time, we can just separate them by commas. We also have another type of variable known as a float, and have named this result. I am not going to go too far in to explaining the difference between a float and an integer other then a float will allow us to have a decimal value whereas an integer will not (remember whole numbers). You will notice result also has a " = 0" after it. This is us giving result a value of zero, the same as saying "x = 0" in a math equation. It is also important to note that you must use semicolons after every line of code, it tells the computer to "move on" to the next set of instructions. Now that we have our variables, which are much like our 216 equation (hit x wound x save)/216 = result. We can start to make a little more sense of whats happening in this program. But how do we enter our information?
This is where the cin command comes into play. You will notice:
cout << "# of sides needed to Hit: ";
cin >> hit;
Appears each time for each variable we need. So the computer displays "# of sides needed to Hit:" which prompts us to enter something. Well this dosent happen on its own, we need the cin command to allow us to type something in. In this case we see our variable hit (dont worry too much about >> just know that they go either way depending on whether its cin or cout). Basically what the computer is telling us to do is to input the value for hit. Simple.
Now that we have an idea of whats going on, and how we are giving our variables a value we need to do something with them! Well we know the basic formula that we want to solve, but how do we accomplish it?
result = (hit * wound * save) /216.0;
cout << "Probability: " << result << endl;
This should look kind of familiar. What we are doing is very similar to what we did earlier when we assigned the variable result a value of zero. This time however we are assigning it a value that is equal to our variables hit, wound, and save multiplied by each other and divided by 216. Now that we have the answer to our question how do we display it? The next line of code should be somewhat understandable. We know the computer will display "Probability: " on the screen, but what about the rest? result is our variable (well its our answer), and endl is simply telling the computer to start a new line after this one is displayed. The << is just separating what is going on in that line of code, for lack of a better term.
It is important to make sure you put 216.0 instead of 216, or you will end up with a 0 instead of a decimal (it basically tells the computer to expect a decimal number). You will also notice if you want to try, if you change the variable of result from a float to an integer (int), you will also end up with a zero. You COULD use modulus (ie the remainder of a division) but that is a whole different show.
Well that is a long explanation for a program we could have done in 10 seconds with a calculator. So why bother? Well you shouldn't to be honest, which is why we are going to expand this idea to discover some information we cant attain with a calculator. Hopefully you give it a try, and play with it a bit! Peace.