// 3dice_football.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
//function declarations
vector<int> get_single_roll_instances();
int get_roll_result(int, int, int, int, int, int);
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> single_roll_instances;
vector<double> single_roll_probabilities;
double td_prob = 0;
double to_prob = 0;
//call get_single_roll_instances and get the number of combinations for each roll
single_roll_instances = get_single_roll_instances();
//set size of probability vector to size of instances vector
single_roll_probabilities.resize(single_roll_instances.size());
//divide each instance by total combinations (216) and store the probabilities
cout << "Calculating single roll probabilities" << endl;
for ( unsigned int i = 0; i < single_roll_instances.size(); i++ ){
single_roll_probabilities = (float) single_roll_instances / 216;
}
int count = 0;
int total_count = 0;
int roll_result = 0;
double roll_prob = 0;
//loop through every combination of rolls
cout << "Looping through all 11,390,625 roll combinations..." << endl;
cout << "This may take some time." << endl;
for ( int i = 0; i < 15; i++ ){
for ( int j = 0; j < 15; j++ ){
for ( int k = 0; k < 15; k++ ){
for ( int l = 0; l < 15; l++ ){
for ( int m = 0; m < 15; m++ ){
for ( int n = 0; n < 15; n++ ){
//find the probability that the roll occurs
roll_prob = single_roll_probabilities * single_roll_probabilities[j] * single_roll_probabilities[k] * single_roll_probabilities[l] * single_roll_probabilities[m] * single_roll_probabilities[n];
//find the result of the roll
roll_result = get_roll_result( i, j, k, l, m, n);
//add the probabilities to totals
if ( roll_result == 0 ){
td_prob += roll_prob;
}
else{
to_prob += roll_prob;
}
count++;
total_count++;
if ( count == 100000 ){
cout << "Processing roll number: " << total_count << endl;
count = 0;
}
}
}
}
}
}
}
cout << "Total TD probability: " << td_prob << endl;
cout << "Total TO probability: " << to_prob << endl;
cout << "Total probability: " << td_prob + to_prob << endl;
cout << "Process Completed." << endl;
system ("pause");
return 0;
}
vector<int> get_single_roll_instances(){
vector<int> instances;
instances.resize(15);
for ( int i = 1; i < 7; i++ ){
for ( int j = 1; j < 7; j++ ){
for ( int k = 1; k < 7; k++ ){
int to = i + j;
//touchdown roll
if ( i == j && j == k ){
instances[12]++;
}
//turnover roll
else if ( (to == 2 || to == 3) && k == 6 ){
instances[13]++;
}
//penalty roll
else if ( to < k ){
instances[14]++;
}
//yardage roll
else{
to -= k;
instances[to]++;
}
}
}
}
return instances;
}
int get_roll_result(int i, int j, int k, int l, int m, int n){
int roll[] = { i, j, k, l, m, n };
int yards = 0, first_down_roll = 0;
for ( int a = 0; a < 6; a++ ){
//touchdown roll? return 0
if ( roll[a] == 12 ){
return 0;
}
//turnover roll? return 1
else if ( roll[a] == 13 ){
return 1;
}
//calculate yardage
else{
if ( roll[a] == 14 ){
yards -= 1;
}
else{
yards += roll[a];
}
}
//first down?
if ( a < 3 ){
if ( first_down_roll == 0 && yards >= 10 ){
first_down_roll = a + 1;
}
}
//turnover on downs?
if ( a == 2 && yards < 10 ){
if ( first_down_roll == 0 ){
return 1;
}
}
if ( first_down_roll > 0 ){
int downs = a - first_down_roll;
if ( downs == 2 && yards < 20 ){
return 1;
}
}
//touchdown?
if ( yards >= 20 ){
return 0;
}
}
if ( yards >= 20 ){
return 0;
}
else{
return 1;
}
}