#include <iostream.h>
#include <time.h>
#include <iomanip.h>
#include <stdlib.h>
#include <stdio.h>

int const rounds_to_play = 30000;

//Function prototypes
int roll_dice(void);
void play_game();
void print_statistics();

//Statistical stuff
long int total_rounds = 0;
long int bankroll = 1000;
int longest_game = 0;
int total_won = 0;
int total_lost = 0;
int won[21] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int lost[21] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

void main()
{
	won[21] = lost[21] = 0;
	int spam;
	char dummy;

	//Seed the random number generator
	srand(time(NULL));

	int counter = 0;
	cout << "Leonard's Craps Odds Program\n\n"
		<< "This program simulates " << rounds_to_play << " rounds of craps and tabulates\n"
		<< "the results. Now you too can know how likely you are to win at craps!\n\n"
		<< "Please wait as Fast Jack plays a helluva lot of craps... ";
	for (counter = 1; counter <= rounds_to_play; counter++)
	{
		play_game();
	}

	cout << "done!\n\n"
	<< "Press enter to see the stats...";
	dummy = getchar();

	print_statistics();
}

void play_game()
{
	enum Status { CONTINUE, WON, LOST };
	Status status; //That'll make your shamrock shake!
	int game_length = 0;
	int roll, point = 0;

	roll = roll_dice();
	switch(roll)
	{
		case 7: case 11:
			status = WON;
			break;
		case 2: case 3: case 12:
			status = LOST;
			break;
		default:
			status = CONTINUE;
			point = roll;
			break;
	}
	while (status == CONTINUE) //Keep going!
	{
		game_length++;
		roll = roll_dice();
		if (roll == point)
			status = WON;
		else if (roll == 7)
			status = LOST;
	}

	total_rounds += game_length;

	if (game_length > longest_game)
		longest_game = game_length;

	if (game_length > 19)
		game_length = 20;
	if (status == WON)
	{
		total_won++;
		won[game_length]++;
	}
	else
	{
		total_lost++;
		lost[game_length]++;
	}
	return;
}

void print_statistics() //see also library functions print_lies and
												//print_damn_lies
{
	int counter;
	char dummy;

	cout << "Rounds" << setw(7) << "Won" << setw(8) << "Lost" << setw(13) << "% Won\n";

	for (counter = 0; counter <20; counter++)
	{
		cout << setw(6) << counter+1 << setw(7) << won[counter]
		<< setw(7) << lost[counter] << setw(13) << (double(won[counter])/double(won[counter]+lost[counter]) * 100) << endl;
	}
	cout << "   21+" << setw(7) << won[21] << setw(7) << lost[21] << setw(13) << (double(won[20])/double(won[20]+lost[20]) * 100) << endl;
	cout << "Hit enter again for more statistics.";

	dummy = getchar();

	cout << "Average game length: " << (double(total_rounds) / double(rounds_to_play)) << endl;
	cout << "One game went on for a whopping " << longest_game << " rounds!\n";
	cout << "Total games won:  " << total_won << endl;
	cout << "Total games lost: " << total_lost << endl;
	cout << "Probability of winning: " << (double(total_won)/double(rounds_to_play)) << endl << endl;
}

int roll_dice(void)
{
	int die1, die2;
	die1 = 1 + rand() % 6;
	die2 = 1 + rand() % 6;

	return (die1+die2);
}