Advertisement
MegaTor

Starting on Deal or No Deal

Sep 8th, 2011
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <ctime>
  5. #include <iomanip>
  6. #include <stdlib.h>
  7.  
  8. using namespace std;
  9.  
  10. string deal, choice, boxToKeep;
  11.  
  12. const int n = 22;
  13.  
  14. vector<int>sumTable;
  15. vector<double>boxes;
  16. vector<double>money;
  17.  
  18. double values[n] = {0.01, 0.1, 0.5, 1, 5, 10, 50, 100, 250, 500, 750, 1000,
  19. 3000, 5000, 10000, 15000, 20000, 35000, 50000, 75000, 100000, 250000};
  20.  
  21. double bottomFive[5], top[5], medium[10];
  22. int selected[n] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  23.  
  24. double amount = 0.00, totAmount = 0.00;
  25.  
  26. int bottoF = 0, topF = 0, mediumT = 0, firstRound, secondRound, thirdRound, fourthRound, fifthRound, sixthRound, gCount = 0, val, new_val = 1, sum = 0, total = 0, nextRound = 1, offer = 0, picked=0;
  27.  
  28. void printBoxes()
  29. {
  30.     for(int i = 0; i<n; i++)
  31.     {
  32.         if(i>0 && (i%3 == 0))
  33.             cout << setw(7) << (sumTable[i]+1) << endl;
  34.         else if(i >= 0)
  35.             cout << setw(7) << (sumTable[i]+1);
  36.     }
  37. }
  38.  
  39. int wantToDeal()
  40. {
  41.     if(total != 0)
  42.     {
  43.         cout << "\n\nDo you want to make a Deal or No Deal?\nType either Deal, or No Deal for your choice: ";
  44.         getline(cin, deal);
  45.         for(int i = 0; i<=(int)deal.size(); i++)
  46.         {
  47.             if(deal[i] == ' ')
  48.                 deal.erase(i, 1);
  49.             else if(deal.size() > 1)
  50.                 deal.erase(deal.size()-1, 1);
  51.         }
  52.         if(deal[0] == 'D' || deal[0] == 'd' || sixthRound == 0)
  53.         {
  54.             nextRound = 0;
  55.         }
  56.         if(deal == "N" || deal == "n")
  57.         {
  58.                 // do nothing
  59.         }
  60.      }
  61.     return nextRound;
  62. }
  63.  
  64. void traverse()
  65. {
  66.     for(int i = 0; i<(int)money.size(); i++)
  67.     {
  68.         for(int j=0; j<(int)money.size(); j++)
  69.         {
  70.             if(money[i] == bottomFive[j])
  71.                 bottoF++;
  72.             if(money[i] == medium[j])
  73.                 mediumT++;
  74.             if(money[i] == top[j])
  75.                 topF++;
  76.         };
  77.     }
  78. }
  79.  
  80. int bankersOffer()
  81. {
  82.     traverse();
  83.     totAmount -= amount;
  84.     offer = 0.05 * (totAmount);
  85.  
  86.     return offer;
  87. }
  88.  
  89. int main()
  90. {
  91.     totAmount = 0;
  92.     for(int i = 0; i<n; i++)
  93.         totAmount += values[i];
  94.  
  95.     for(int i = 0, j = 21; i<5; i++, j--)
  96.     {
  97.         bottomFive[i] = values[i];
  98.         top[j] = values[j];
  99.     }
  100.  
  101.     for(int i = 0; i<n; i++)
  102.         selected[i] = 0;
  103.  
  104.     for(int i = 7; i<17; i++)
  105.     {
  106.         medium[i] = values[i];
  107.     }
  108.  
  109.     firstRound = secondRound = thirdRound = fourthRound = fifthRound = sixthRound = 1, gCount = 0;
  110.     for(int i=0; i<n; i++)
  111.     {
  112.         sumTable.push_back(-1);
  113.     }
  114.  
  115.     srand((unsigned)time(NULL));
  116.     sum = rand()%n;
  117.  
  118.     for(int i=0; i<n; i++)
  119.     {
  120.         for(int j=0; j<n; j++)
  121.         {
  122.             // check if number is used, making an array for used numbers
  123.             while(sum == sumTable[j])
  124.             {
  125.                 sum = rand()%n;
  126.                 j = 0;
  127.             }
  128.         }
  129.         sumTable[i] = sum;
  130.         sum = rand()%n;
  131.     }
  132.  
  133.     printBoxes();
  134.     cout << endl << endl;
  135.  
  136.     for(int i=0; i<n; i++)
  137.     {
  138.         boxes.push_back(values[sumTable[i]]);
  139.     }
  140.  
  141. /*  for(int i=0; i<n; i++)
  142.     {
  143.         cout << setw(3) << boxes[i] << endl;
  144.     }
  145. */
  146.     while(!picked)
  147.     {
  148.         cout << "Please choose a box that you want to keep: ";
  149.         getline(cin, boxToKeep);
  150.         for(int i = 0; i<(int)boxToKeep.size(); i++)
  151.         {
  152.             val = atoi(boxToKeep.c_str());
  153.  
  154.             if(selected[val-1] == 0 && (val >= 1 && val <= 22) )
  155.             {  
  156.                 selected[val-1] = 1;
  157.                 cout << "\nYou have chosen box number " << val /*<< " which contains " << boxes[val-1]<< ".\n\n"*/ << endl << endl;
  158.                 picked = 1;
  159.             }
  160.             else
  161.             {
  162.                 // ask over again
  163.             }
  164.         }
  165.     }
  166.  
  167.     while(nextRound)
  168.     {
  169.         while(firstRound && nextRound)
  170.         {
  171.             cout << "Please choose a box from 1 - 22 to open, and then press Enter: ";
  172.             getline(cin, choice);
  173.             for(int i = 0; i<(int)choice.size(); i++)
  174.             {
  175.                 if(choice[i] == ' ')
  176.                     choice.erase(i, 1);
  177.             }
  178.             val = atoi(choice.c_str());
  179.             if(selected[val-1] == 0)
  180.             {  
  181.                 money.push_back(val-1);
  182.                 selected[val-1] = 1;
  183.                 cout << "You choose box number " << val << " which contains " << boxes[val-1]<< ".\n\n";
  184.                 gCount++;
  185.                 total++;
  186.                 amount += boxes[val-1];
  187.             }
  188.             else
  189.             {
  190.                 // ask over again
  191.             }
  192.  
  193.             if( gCount == 5)
  194.             {
  195.                 firstRound = 0;
  196.                 cout << "\nFirst round is done.\n\nYou have opened a total of " << gCount << " boxes which helds a total amount of " << amount << ".\n";
  197.  
  198.                 cout << "\n\nThe banker offers you " << bankersOffer() << ".\n\n";
  199.  
  200.                 // Wants the player to quit or continue?
  201.                 wantToDeal();
  202.             }
  203.         }
  204.  
  205.         amount = gCount = 0;
  206.  
  207.         while(secondRound && nextRound)
  208.         {
  209.             cout << "Please choose a box from 1 - 22 to open, and then press Enter: ";
  210.             getline(cin, choice);
  211.             for(int i = 0; i<(int)choice.size(); i++)
  212.             {
  213.                 if(choice[i] == ' ')
  214.                     choice.erase(i, 1);
  215.             }
  216.  
  217.             val = atoi(choice.c_str());
  218.             if(selected[val-1] == 0)
  219.             {  
  220.                 money.push_back(val-1);
  221.                 selected[val-1] = 1;
  222.                 cout << "You choose box number " << val << " which contains " << boxes[val-1]<< ".\n\n";
  223.                 gCount++;
  224.                 total++;
  225.                 amount += boxes[val-1];
  226.             }
  227.             else
  228.             {
  229.                 // ask over again
  230.             }
  231.  
  232.             if( gCount == 3)
  233.             {
  234.                 secondRound = 0;
  235.                 cout << "\nSecond round is done.\n\nYou have opened a total of " << gCount << " boxes which helds a total amount of " << amount << ".\n";
  236.  
  237.                 bankersOffer();
  238.  
  239.                 // Wants the player to quit or continue?
  240.                 wantToDeal();
  241.             }
  242.         }
  243.  
  244.         amount = gCount = 0;
  245.  
  246.         while(thirdRound && nextRound)
  247.         {
  248.             cout << "Please choose a box from 1 - 22 to open, and then press Enter: ";
  249.             getline(cin, choice);
  250.             for(int i = 0; i<(int)choice.size(); i++)
  251.             {
  252.                 if(choice[i] == ' ')
  253.                     choice.erase(i, 1);
  254.             }
  255.  
  256.             val = atoi(choice.c_str());
  257.             if(selected[val-1] == 0)
  258.             {  
  259.                 money.push_back(val-1);
  260.                 selected[val-1] = 1;
  261.                 cout << "You choose box number " << val << " which contains " << boxes[val-1]<< ".\n\n";
  262.                 gCount++;
  263.                 total++;
  264.                 amount += boxes[val-1];
  265.             }
  266.             else
  267.             {
  268.                 // ask over again
  269.             }
  270.  
  271.             if( gCount == 3)
  272.             {
  273.                 thirdRound = 0;
  274.                 cout << "\nThird round is done.\n\nYou have opened a total of " << gCount << " boxes which helds a total amount of " << amount << ".\n";
  275.            
  276.                 bankersOffer();
  277.  
  278.                 // Wants the player to quit or continue?
  279.                 wantToDeal();
  280.             }
  281.         }
  282.  
  283.         amount = gCount = 0;
  284.  
  285.         while(fourthRound && nextRound)
  286.         {
  287.             cout << "Please choose a box from 1 - 22 to open, and then press Enter: ";
  288.             getline(cin, choice);
  289.             for(int i = 0; i<(int)choice.size(); i++)
  290.             {
  291.                 if(choice[i] == ' ')
  292.                     choice.erase(i, 1);
  293.             }
  294.  
  295.             val = atoi(choice.c_str());
  296.             if(selected[val-1] == 0)
  297.             {  
  298.                 money.push_back(val-1);
  299.                 selected[val-1] = 1;
  300.                 cout << "You choose box number " << val << " which contains " << boxes[val-1]<< ".\n\n";
  301.                 gCount++;
  302.                 total++;
  303.                 amount += boxes[val-1];
  304.             }
  305.             else
  306.             {
  307.                 // ask over again
  308.             }
  309.  
  310.             if( gCount == 3)
  311.             {
  312.                 fourthRound = 0;
  313.                 cout << "\nFourth round is done.\n\nYou have opened a total of " << gCount << " boxes which helds a total amount of " << amount << ".\n";
  314.                
  315.                 bankersOffer();
  316.  
  317.                 // Wants the player to quit or continue?
  318.                 wantToDeal();
  319.             }
  320.         }
  321.  
  322.  
  323.         amount = gCount = 0;
  324.  
  325.         while(fifthRound && nextRound)
  326.         {
  327.             cout << "Please choose a box from 1 - 22 to open, and then press Enter: ";
  328.             getline(cin, choice);
  329.             for(int i = 0; i<(int)choice.size(); i++)
  330.             {
  331.                 if(choice[i] == ' ')
  332.                     choice.erase(i, 1);
  333.             }
  334.  
  335.             val = atoi(choice.c_str());
  336.             if(selected[val-1] == 0)
  337.             {  
  338.                 money.push_back(val-1);
  339.                 selected[val-1] = 1;
  340.                 cout << "You choose box number " << val << " which contains " << boxes[val-1]<< ".\n\n";
  341.                 gCount++;
  342.                 total++;
  343.                 amount += boxes[val-1];
  344.             }
  345.             else
  346.             {
  347.                 // ask over again
  348.             }
  349.  
  350.             if( gCount == 3)
  351.             {
  352.                 fifthRound = 0;
  353.                 cout << "\nFifth round is done.\n\nYou have opened a total of " << gCount << " boxes which helds a total amount of " << amount << ".\n";
  354.                
  355.                 bankersOffer();
  356.  
  357.                 // Wants the player to quit or continue?
  358.                 wantToDeal();
  359.             }
  360.         }
  361.  
  362.         amount = gCount = 0;
  363.  
  364.         while(sixthRound && nextRound)
  365.         {
  366.             cout << "Please choose a box from 1 - 22 to open, and then press Enter: ";
  367.             getline(cin, choice);
  368.             for(int i = 0; i<(int)choice.size(); i++)
  369.             {
  370.                 if(choice[i] == ' ')
  371.                     choice.erase(i, 1);
  372.             }
  373.  
  374.             val = atoi(choice.c_str());
  375.             if(selected[val-1] == 0)
  376.             {  
  377.                 money.push_back(val-1);
  378.                 selected[val-1] = 1;
  379.                 cout << "You choose box number " << val << " which contains " << boxes[val-1]<< ".\n\n";
  380.                 gCount++;
  381.                 total++;
  382.                 amount += boxes[val-1];
  383.             }
  384.             else
  385.             {
  386.                 // ask over again
  387.             }
  388.  
  389.             if( gCount == 3)
  390.             {
  391.                 sixthRound = 0;
  392.                 cout << "\nSixth round is done.\n\nYou have opened a total of " << gCount << " boxes which helds a total amount of " << amount << ".\n";
  393.  
  394.                 bankersOffer();
  395.  
  396.                 // Wants the player to quit or continue?
  397.                 wantToDeal();
  398.             }
  399.         }
  400.  
  401.         // For later: give the player the oportunity to switch the remaining two boxes and open the one on the "table"
  402.     }
  403.  
  404.     cin.get();
  405.     return 0;
  406. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement