#include <iostream>
#include <stdlib.h>
#include <cstdlib>
#include <climits>
#include <cmath>
//This doesn't actually seem to work when the starting bid isn't 1.
//TODO: Find out why!!
unsigned long long moneyOnHand = 300; //Default start with cool million!
unsigned long long moneyOnHandMax = moneyOnHand;
unsigned long long moneyOnHandMaxRound = 0;
unsigned long long moneyOnHandLastRound = moneyOnHand;
unsigned long long totalGamesPlayed = 0;
unsigned long long martingale(unsigned long long bidAmount);
void logRound(unsigned long long roundNum, unsigned long long money);
int main(int argc, char* argv[])
{
srand (time(NULL));
int initialBid = 1;
if ( argc == 2 )
{
moneyOnHand = atoi(argv[1]);
}
else if ( argc == 2)
{
moneyOnHand = atoi(argv[1]);
initialBid = atoi(argv[2]);
}
moneyOnHandMax = moneyOnHand;
unsigned long long numRounds=0;
//Play until I bust.... Or overflow a unsigned long long at $18,446,744,073,709,551,615 (2^64-1)
while ( ( moneyOnHand > 0 ) && ( moneyOnHand >= initialBid ) )
{
unsigned long long bidAmount = martingale(initialBid);
numRounds++;
if ( moneyOnHand > moneyOnHandMax )
{
moneyOnHandMax = moneyOnHand;
moneyOnHandMaxRound = numRounds;
}
//This is just a lazy way to show mostly the early and late rounds with random bits inbetween and at times we couldn't make the bid.
//Printing to stdout is slow.
bool tookALoss = moneyOnHand < moneyOnHandLastRound;
if ( moneyOnHand == 0 || numRounds % moneyOnHand == 0 || moneyOnHand % numRounds == 0 || tookALoss )
{
//If we just took a loss, lets print the previous one too if we haven't already.
bool printedLastRound = (numRounds - 1) % moneyOnHandLastRound == 0 || (moneyOnHandLastRound % numRounds - 1) == 0;
if ( ! printedLastRound && tookALoss )
{
logRound( numRounds - 1, moneyOnHandLastRound );
std::cout << "Oops! The bid is $" << bidAmount
<< " but you only have $" << moneyOnHand
<< " on hand because you lost $" << bidAmount - 1
<< " after losing " << (unsigned long long)log2l((long double)bidAmount/initialBid)
<< " games in a row." << std::endl;
}
logRound( numRounds, moneyOnHand );
}
if ( moneyOnHand == ULLONG_MAX )
{
std::cout << "Oops! We broke the bank! YOLO!! Let's buy all the yachts!" << std::endl;
return 0;
}
moneyOnHandLastRound = moneyOnHand;
}
std::cout << "Whelp, you have played " << totalGamesPlayed << " games, and you are broke. Should have stopped on round " << moneyOnHandMaxRound << " when you had $" << moneyOnHandMax << ". Hope you didn't bet the yacht. Better luck next time!" << std::endl;
return 0;
}
void logRound(unsigned long long roundNum, unsigned long long money)
{
std::cout << "Round " << roundNum << ": Money = $" << money << std::endl;
}
unsigned long long martingale(unsigned long long bidAmount)
{
if ( moneyOnHand >= bidAmount )
{
if ( rand() % 2 == 0 )
{
moneyOnHand += bidAmount;
}
else
{
moneyOnHand -= bidAmount;
bidAmount = martingale(bidAmount * 2);
}
totalGamesPlayed++;
}
return bidAmount;
}