
#include <iostream>
using namespace std;
#include <math.h>
#include <vector>
#include "MersenneTwister.h"

#define DEG2RAD                                 0.017453292519943295769236907684886
#define M_PI      3.1415


#define GREEN 0x100
#define RED   0x200
#define BLACK 0x400

int money      = 0;
int houselimit = 500;
int startbet   = 0;
int runlen     = 6;
int numrolls   = 1000000;

vector<int> rolls;
MTRand RAND;

int return_color( int num )
{
	int cols[] = { GREEN, 
		RED,BLACK,RED,
		BLACK,RED,BLACK,
		RED,BLACK,RED,
		BLACK,BLACK,RED,
		BLACK,RED,BLACK,
		RED,BLACK,RED,
		RED,BLACK,RED,
		BLACK,RED,BLACK,
		RED,BLACK,RED,
		BLACK,BLACK,RED,
		BLACK,RED,BLACK,
		RED,BLACK,RED };
	
	return cols[num];
}

int roll( bool print )
{
	int num = RAND.randInt(36);
	return num;
}

void do_bet( int run_color )
{
	if ( run_color == GREEN )
	{
		cout << "wow " << runlen << " 0's in a row " << endl;
		return;
	}
	int bet_on_color = (run_color==RED) ? BLACK : RED;
	int current_bet = startbet;

	while ( 1 )
	{
		if ( money - current_bet < 0 )
			return;
		money -= current_bet;
		rolls.push_back( roll(true) );

		if ( return_color( rolls.back() ) == bet_on_color )
		{
			money += current_bet*2;
			return;
		}
		current_bet *= 2;
		if ( current_bet > houselimit )
		{
			return;
		}
	}
}

struct Group
{
	int startbet;
	int id;
	int maxcash;
};

int main(int argc, char** argv)
{
	vector<Group> groups;
	for ( int j=0; j<1000; j++ )
	{
		for ( int k=0; k<2; k++ )
		{
			Group newgroup;
			startbet = ( k == 0 ) ? 200 : 500;
			newgroup.startbet = startbet;
			newgroup.id = j;
			RAND.seed( 1000+j );
			int max_cash = 0;
			money        = houselimit;
			int i;
			for ( i=0; i<numrolls; i++ )
			{
				rolls.push_back( roll(false) ); // its gonna be more than X rolls since do_bet() also rolls, excluding the break

				bool start_to_bet = true;
				int current_col = return_color( rolls.back() );
				for ( int ii=i-1; ii>i-1-runlen && ii>=0; ii-- ) // yes this is the "gambler's fallacy"
				{
					if ( return_color( rolls[ii] ) != current_col )
						start_to_bet = false;
				}
				if ( start_to_bet && i > 5 ) // > 5 so I ignore the run at the start
					do_bet( current_col );

				max_cash = max( max_cash, money );

				if ( money <= 0 || money < startbet )
				{
//					cout << j << ( (k==0) ? "ante=$10" : "ante=$50" ) << " lost all money " << i << " max cash = $" << max_cash << endl;					
					break;
				}
			}
			if ( i>=numrolls )
				cout << j << ( (k==0) ? "ante=$10" : "ante=$50" ) << " WOW youre a WINNER from the casino " << i << " max cash = $" << max_cash << endl;

			newgroup.maxcash = max_cash;
			groups.push_back( newgroup );
		}
	}

	int tots[] = {0,0};
	for ( int j=0; j<groups.size(); j+=2 )
	{
		int dif = groups[j].maxcash - groups[j+1].maxcash;
		if ( dif > 0 )
			cout << groups[j].startbet << " " << groups[j].maxcash - groups[j+1].maxcash << endl;
		else if ( dif< 0 )
			cout << groups[j+1].startbet << " " << groups[j+1].maxcash - groups[j].maxcash << endl;
		else
			cout << "tie" << endl;
		tots[0] += groups[j].maxcash;
		tots[1] += groups[j+1].maxcash;
	}
	cout << "final = " << tots[0] << " " << tots[1] << " " << float(tots[1]) / float(tots[0]) << endl;
}

