fork download
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <cstdlib>
  4. #include <climits>
  5. #include <cmath>
  6.  
  7. //This doesn't actually seem to work when the starting bid isn't 1.
  8. //TODO: Find out why!!
  9.  
  10. unsigned long long moneyOnHand = 300; //Default start with cool million!
  11. unsigned long long moneyOnHandMax = moneyOnHand;
  12. unsigned long long moneyOnHandMaxRound = 0;
  13.  
  14. unsigned long long moneyOnHandLastRound = moneyOnHand;
  15.  
  16. unsigned long long totalGamesPlayed = 0;
  17.  
  18.  
  19. unsigned long long martingale(unsigned long long bidAmount);
  20. void logRound(unsigned long long roundNum, unsigned long long money);
  21.  
  22. int main(int argc, char* argv[])
  23. {
  24. srand (time(NULL));
  25. int initialBid = 1;
  26. if ( argc == 2 )
  27. {
  28. moneyOnHand = atoi(argv[1]);
  29. }
  30. else if ( argc == 2)
  31. {
  32. moneyOnHand = atoi(argv[1]);
  33. initialBid = atoi(argv[2]);
  34. }
  35. moneyOnHandMax = moneyOnHand;
  36.  
  37.  
  38. unsigned long long numRounds=0;
  39. //Play until I bust.... Or overflow a unsigned long long at $18,446,744,073,709,551,615 (2^64-1)
  40. while ( ( moneyOnHand > 0 ) && ( moneyOnHand >= initialBid ) )
  41. {
  42. unsigned long long bidAmount = martingale(initialBid);
  43. numRounds++;
  44. if ( moneyOnHand > moneyOnHandMax )
  45. {
  46. moneyOnHandMax = moneyOnHand;
  47. moneyOnHandMaxRound = numRounds;
  48. }
  49.  
  50. //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.
  51. //Printing to stdout is slow.
  52. bool tookALoss = moneyOnHand < moneyOnHandLastRound;
  53. if ( moneyOnHand == 0 || numRounds % moneyOnHand == 0 || moneyOnHand % numRounds == 0 || tookALoss )
  54. {
  55. //If we just took a loss, lets print the previous one too if we haven't already.
  56. bool printedLastRound = (numRounds - 1) % moneyOnHandLastRound == 0 || (moneyOnHandLastRound % numRounds - 1) == 0;
  57. if ( ! printedLastRound && tookALoss )
  58. {
  59. logRound( numRounds - 1, moneyOnHandLastRound );
  60. std::cout << "Oops! The bid is $" << bidAmount
  61. << " but you only have $" << moneyOnHand
  62. << " on hand because you lost $" << bidAmount - 1
  63. << " after losing " << (unsigned long long)log2l((long double)bidAmount/initialBid)
  64. << " games in a row." << std::endl;
  65.  
  66. }
  67. logRound( numRounds, moneyOnHand );
  68. }
  69.  
  70. if ( moneyOnHand == ULLONG_MAX )
  71. {
  72. std::cout << "Oops! We broke the bank! YOLO!! Let's buy all the yachts!" << std::endl;
  73. return 0;
  74. }
  75. moneyOnHandLastRound = moneyOnHand;
  76. }
  77. 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;
  78. return 0;
  79. }
  80.  
  81. void logRound(unsigned long long roundNum, unsigned long long money)
  82. {
  83. std::cout << "Round " << roundNum << ": Money = $" << money << std::endl;
  84. }
  85.  
  86. unsigned long long martingale(unsigned long long bidAmount)
  87. {
  88. if ( moneyOnHand >= bidAmount )
  89. {
  90. if ( rand() % 2 == 0 )
  91. {
  92. moneyOnHand += bidAmount;
  93. }
  94. else
  95. {
  96. moneyOnHand -= bidAmount;
  97. bidAmount = martingale(bidAmount * 2);
  98. }
  99. totalGamesPlayed++;
  100. }
  101. return bidAmount;
  102. }
  103.  
Success #stdin #stdout 0s 15240KB
stdin
300
stdout
Round 1: Money = $301
Round 2: Money = $302
Round 3: Money = $303
Round 4: Money = $304
Round 5: Money = $305
Round 6: Money = $306
Round 10: Money = $310
Round 12: Money = $312
Round 15: Money = $315
Round 20: Money = $320
Round 25: Money = $325
Round 30: Money = $330
Round 50: Money = $350
Round 60: Money = $360
Round 75: Money = $375
Round 100: Money = $400
Round 150: Money = $450
Round 271: Money = $571
Oops! The bid is $512 but you only have $60 on hand because you lost $511 after losing 9 games in a row.
Round 272: Money = $60
Round 275: Money = $63
Oops! The bid is $64 but you only have $0 on hand because you lost $63 after losing 6 games in a row.
Round 276: Money = $0
Whelp, you have played 540 games, and you are broke. Should have stopped on round 271 when you had $571. Hope you didn't bet the yacht. Better luck next time!