Advertisement
Willium_Bob_Cole

WBC Lottery Program Complete

Dec 16th, 2012
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 21.98 KB | None | 0 0
  1. #include <string>
  2. #include <sstream>
  3. #include <iostream>
  4. #include <time.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <windows.h>
  8. #include <conio.h>
  9.  
  10.  
  11. using namespace std;
  12.  
  13.  
  14.  
  15. //----------------------------A FANTASTICAL LOTTERY GAME BY THE AMAZING MIGUEL------------------------------
  16. //    --------------------------------------------------------------------------------------------------
  17. //            ------------------------COMES WITH FUNCTIONS AS STANDARD--------------------------
  18. //                        --------------------------------------------------------
  19. //                                        ------------------------
  20.  
  21.  
  22.  
  23. /*
  24.  
  25. This is a simple lottery game by Michael Brandon. It is made to demonstrate the use of arrays and functions.
  26. Key features include:
  27. -The ability to choose 1-10 lines per ticket
  28.  
  29. -Dynamically allocating the memory for array storing the user's ticket,
  30. and deleting that array from memory before creating a new one when changing the length
  31.  
  32. -The option of re-using the same numbers or creating a new ticket. Re-using does not delete the array
  33. and the player cannot re-use a ticket if their wallet does not contain enough money for that many lines.
  34.  
  35. -Input goes to string first, then checks if it can convert into the integer variable it is needed for.
  36. This prevents the program breaking into an infinite loop if the user accidentally types a character into an integer.
  37.  
  38. -Array checking for both the ticket (per line) and the balls (including the bonus ball), that prevent any duplicate numbers
  39. from happening when they should not, and that they are generated between specific values, defined by a single global variable,
  40. and as mentioned above, prevents characters from being input, and prevents the user or the machine creating a value greater or
  41. lower than the amount allowed, referencing a global variables that mean I can generate numbers between 1-49 as intended, or 1-500.
  42. These values are also used when requesting input from the player, so changing the range of valid numbers still outputs correct
  43. information to the player.
  44.  
  45. -Bubble sort applied to each line on the players ticket, meaning that the player's numbers print in ascending order per line.
  46.  
  47. -Very few global variables, and only ONE that is ever affected by the running of the program,
  48. makes it much easier to debug, and easy to edit the value of the globals to make the game easier or harder.
  49.  
  50. -Sleep funtion at certain places to make it easier to follow the progress of the game and also to add tension. The ball generator
  51. also uses the random function to pause a random amount within a certain range to increase inpredictability. All pauses reference a
  52. single global variable, meaning I can scale or entierly remove all pauses in the game by editing a single value.
  53.  
  54. -Compare function that compares the 6 numbers on each line with the random numbers and the bonus ball, and assigns a score to a
  55. score array, as long as the number of lines the player is playing with. 10 points are won for a normal match, plus 1 point for
  56. matching a bonus ball. So a line with two matches would score 20, and four number plus a bonus ball would score 41. These scores
  57. are simply read by a number of if statements, and adds the winnings, if any, together, and tells the user how much they won,
  58. before adding the winnings into their wallet.
  59.  
  60. */
  61.  
  62.  
  63.  
  64. //chickity-check my globals yo
  65.  
  66. const int BetLow   (1);     //minimum guess and ball value
  67. const int BetHigh (49);     //maximum guess and ball value
  68.  
  69.  
  70. const char Pound = 156; //to use pound symbol in cout
  71.  
  72. unsigned int Wallet (10);   //Global wallet amount, only affected after user prints a whole ticket (removes £1 for each line) and when collecting winnings, if any
  73.  
  74. //                              _________________________________________
  75. const int Payout2 (1); //2 balls                                         |
  76. const int Payout3 (10);   //3 balls                                      |
  77. const int Payout4 (100);      //4 balls                                  |
  78. const int Payout4B (500);         //4 balls and bonus                    |===P-p-pick up your p-prizes!!
  79. const int Payout5 (2500);             //5 balls                          |        (this does not follow official
  80. const int Payout5B (190000);              //5 balls and bonus            |            Lottery prizes, as you would
  81. const int Payout6 (4000000);                  //JACKPOT DING DING DING __|               never win a thing otherwise!)
  82.  
  83.  
  84. const float pause (1000); //default time to pause, can be adjusted per pause using operators, default 1 second
  85.  
  86.  
  87. bool running = true; //flag used to end program
  88.  
  89. //----------------------------------------ENTER NAME--------------------------------------------
  90. string EnterName()
  91. {
  92.     string input;
  93.  
  94.     cout << "Please enter your name:\n";
  95.  
  96.     cin.clear();
  97.     cin >> input;
  98.  
  99.     return (input);
  100. }
  101.  
  102.  
  103. //----------------------------------------CHOOSE NUMBER OF LINES--------------------------------
  104. int Ticket()
  105. {
  106.     int Lines = 0; //set number of lines to default of 0 upon entering this function
  107.  
  108.     int flag = 1; //break loop when unflagged
  109.  
  110.  
  111.     do //loop breaks when number is found to be valid and unchecks the flag
  112.     {
  113.         cout << "\n\nHow many lines would you like to play with? (1-10)\n";
  114.  
  115.             string input = "";
  116.             cin.sync();//does nothing ><
  117.             cin >> input;
  118.             stringstream myStream(input);
  119.  
  120.             if(myStream >> Lines)
  121.             {
  122.                 if(Lines < 1 || Lines > 10) //feedback to player if they pick less than 1 or more than 10 lines
  123.                 {
  124.                     cout << "Must be between 1 and 10!\n\n";
  125.                 }
  126.  
  127.                 else if(Lines > Wallet && Wallet > 1) //feedback if player cannot afford this many lines
  128.                 {
  129.                     cout << "You do not have enough money for this many lines! You can afford up to " << Wallet << " lines!\n\n";
  130.                 }
  131.  
  132.                 else if(Lines > Wallet && Wallet == 1) //as above, correcting pluralisation for just £1
  133.                 {
  134.                     cout << "You do not have enough money for this many lines! You can only afford " << Wallet << " line!\n\n";
  135.                 }
  136.  
  137.                 else
  138.                 {
  139.                     flag = 0;
  140.                 }
  141.             }
  142.  
  143.             else
  144.             {
  145.                 cout << "Must be a valid number between 1 and 10!\n\n";
  146.             }
  147.     }
  148.     while ((Lines < 1 || Lines  > 10 || Lines > Wallet) && (flag = 1));
  149.  
  150.  
  151.     return Lines;
  152. }
  153.  
  154.  
  155. //----------------------------------------CHOOSE SIX NUMBERS PER LINE, BUBBLE SORT, AND PRINT---
  156. int Input(const int Lines, int **Guess)
  157. {
  158.     cout << "\n\nPlease choose your lucky numbers: (" << BetLow << "-" << BetHigh << ")\n";
  159.     Sleep(pause / 2);
  160.  
  161.     for (int Line = 0; Line < Lines; Line++)//loop per line
  162.     {
  163.         cout << "Line " << Line + 1 << " of " << Lines << ":\n";
  164.  
  165.         for(int n = 0; n < 6; n++)//loop per number on a line
  166.         {
  167.             bool duplicate = false;//flag for duplicates, will re-request input until this stays false
  168.  
  169.             do
  170.             {
  171.                 string input = "0";
  172.  
  173.                 cout << n + 1 << ")\t";
  174.  
  175.                 cin >> input; //cin goes to input string first
  176.                 stringstream myStream(input); //puts input string into myStream of type stringstream
  177.  
  178.                 if (myStream >> Guess[Line][n]) //if stringstream detects that myStream can go into the array as an int type variable...
  179.                 {                               //then loop continues as normal
  180.                     if (Guess[Line][n] < BetLow)    //can't bet below 1
  181.                     {
  182.                         cout <<"Too low, try again! (" << BetLow << "-" << BetHigh << ")\n\n";
  183.                         Sleep(pause / 2);
  184.                     }
  185.  
  186.                     else if (Guess[Line][n] > BetHigh)  //can't bet over 49
  187.                     {
  188.                         cout << "Too high, try again! (" << BetLow << "-" << BetHigh << ")\n\n";
  189.                         Sleep(pause / 2);
  190.                     }
  191.  
  192.                     if(n > 0)//checking for dupluicates, does not need to occur on first value
  193.                     {
  194.                         duplicate = false;//first set flag to false
  195.  
  196.                         for(int count = 0; count < n; count++)//for each number on this line, upto the number before THIS one...
  197.                         {
  198.                             if(Guess[Line][n] == Guess[Line][count])//if This number matches checked number...
  199.                             {
  200.                                 duplicate = true;//flag for re-entry is set to true
  201.                                 cout << "You already have this number, please try again!\n\n";
  202.                                 Sleep(pause / 2);
  203.                             }
  204.                         }
  205.                     }
  206.                 }
  207.  
  208.                 else //else, stringstream CAN'T put myStream into the array...
  209.                 {
  210.                     cout << "\nInvalid number, please try again\n\n";//when invalid, number will not be between the required values, so loop for this number will not break
  211.                     Sleep(pause / 2);
  212.                 }
  213.  
  214.             }
  215.             while((Guess[Line][n] < BetLow || Guess[Line][n] > BetHigh) || (duplicate == true));
  216.  
  217.         }
  218.     }
  219.  
  220.  
  221.     //--------------------------------BUBBLE SORT CODE-------------------------------------
  222.     for(int Line = 0; Line < Lines; Line++)//for each line
  223.     {
  224.         bool swap = true;//flag, for detecting if a swap occured each iteration
  225.  
  226.         do
  227.         {
  228.             swap = false;   //sets flag to false so loop only runs once, or until no swaps are detected.
  229.  
  230.             for (int sort = 0; sort < 5; sort++)
  231.             {
  232.  
  233.  
  234.                 if(Guess[Line][sort] > Guess[Line][sort+1])     //if this number is greater than the next
  235.                 {
  236.                     int temp;
  237.                     temp = Guess[Line][sort+1];             //swap elements using temp data storage
  238.                     Guess[Line][sort+1] = Guess[Line][sort];
  239.                     Guess[Line][sort] = temp;
  240.                     swap = true;               //indicates that a swap occurred.
  241.                 }
  242.             }
  243.         }
  244.         while(swap);
  245.     }
  246.  
  247.  
  248.  
  249.     //------print ticket-------
  250.     cout << "\n\nYou picked:\n";
  251.     Sleep(pause / 4);
  252.  
  253.     for (int Line = 0; Line < Lines; Line++)
  254.     {
  255.         for(int n = 0; n < 6; n++)
  256.         {
  257.             cout << "\t-" << Guess[Line][n] << "-";
  258.         }
  259.         cout << endl;
  260.     }
  261.     Sleep(pause * 2);
  262.  
  263.  
  264.     }
  265.  
  266.  
  267.  
  268. //----------------------------------------RANDOM NUMBER GENERATOR-------------------------------
  269. void Generate(int Ball[], int &BonusBall)
  270. {
  271.     Sleep(pause * 1);
  272.     cout << "Lets get those balls rolling...\n\n\t";
  273.     srand ( time(NULL) );//sets random seed based on time
  274.  
  275.     bool duplicate = false;//flag for duplicates, provides same function as when used in Input
  276.  
  277.     //-----generate 6 random balls------
  278.     for(int n = 0; n < 6; n++)
  279.     {
  280.         Sleep(pause * (rand() % 2 + 1.5));//pause a random amount of time
  281.  
  282.         do
  283.         {
  284.             Ball[n] = rand() % BetHigh + BetLow;
  285.             duplicate = false;
  286.  
  287.             if(n > 0)//check for duplicates except one first ball
  288.             {
  289.                 for(int count = 0; count < n; count++)
  290.                 {
  291.                     if(Ball[n] == Ball[count])
  292.                     {
  293.                         duplicate = true;
  294.                     }
  295.                 }
  296.             }
  297.         }
  298.         while((Ball[n] < BetLow || Ball[n] > BetHigh) || (duplicate == true));
  299.  
  300.         cout << "*" << Ball[n] << "*\t";
  301.     }
  302.  
  303.     do
  304.     {
  305.         BonusBall = rand() % BetHigh + BetLow;//generate bonus ball
  306.         duplicate = false;
  307.  
  308.         for(int n = 0; n < 6; n++)//check bonus does not match any of the other balls
  309.         {
  310.             if(Ball[n] == BonusBall)
  311.             {
  312.                 duplicate = true;
  313.             }
  314.         }
  315.     }
  316.     while(duplicate == true);
  317.  
  318.     Sleep(pause / 1.75);
  319.  
  320.     cout << "\n\t\t\t\tAnd the bonus is.....\t";
  321.  
  322.     Sleep(pause * (rand() % 5 + 2));//longer random pause before printing bonus ball
  323.  
  324.     cout << "*" << BonusBall << "*\n\n";
  325.  
  326. }
  327.  
  328.  
  329. //----------------------------------------COMPARE RANDOM BALLS WITH GUESSES PER LINE------------
  330. void Compare(int Lines, int **Guess, int Ball[], int BonusBall, int Matches[])
  331. {
  332.     for(int Line = 0; Line < Lines; Line++)
  333.     {
  334.         Matches[Line] = 0; //resets score per line to avoid winning on a line that you won on last round
  335.  
  336.         for(int n = 0; n < 6; n++)
  337.         {
  338.             if(Guess[Line][n] == BonusBall)
  339.             {
  340.                 Matches[Line] = Matches[Line] + 1;//matching bonus ball scores 1 point
  341.             }
  342.  
  343.             for(int b = 0; b < 6; b++)
  344.             {
  345.                 if(Guess[Line][n] == Ball[b])
  346.                 {
  347.                     Matches[Line] = Matches[Line] + 10;//matching regular ball scores 10 points
  348.                 }
  349.             }
  350.         }
  351.  
  352.  
  353.  
  354.  
  355.      /* if(Matches[Line] > 0) //can print score for each winning line, useful for debugging but user doesnt know what the score means...
  356.         {
  357.             cout << "\nYou scored " << Matches[Line] << " on line " << Line + 1 << " of " << Lines << "!";
  358.         }*/
  359.     }
  360. }
  361.  
  362.  
  363. //----------------------------------------PAYOUT------------------------------------------------
  364. void Payout(int Lines, int *Matches)
  365. {
  366.     int Winnings = 0;//default winnings to 0
  367.  
  368.     for(int Line = 0; Line < Lines; Line++)//score for each line
  369.     {
  370.         //score will be of double digit format, ie two balls will score 20, and four balls and bonus ball will score 41
  371.  
  372.         if(Matches[Line] > 0)
  373.         {
  374.             if(Matches[Line] == 20 || Matches [Line] == 21)//2 matches (or 2 and bonus)
  375.             {
  376.                 Winnings = Winnings + Payout2;
  377.             }
  378.  
  379.             else if(Matches[Line] == 30 || Matches [Line] == 31)//3 matches (or 3 and bonus)
  380.             {
  381.                 Winnings = Winnings + Payout3;
  382.             }
  383.  
  384.             else if(Matches[Line] == 40)//4 matches
  385.             {
  386.                 Winnings = Winnings + Payout4;
  387.             }
  388.  
  389.             else if(Matches[Line] == 41)//4 matches and bonus ball
  390.             {
  391.                 Winnings = Winnings + Payout4B;
  392.             }
  393.  
  394.             else if(Matches[Line] == 50)//5 matches
  395.             {
  396.                 Winnings = Winnings + Payout5;
  397.             }
  398.  
  399.             else if(Matches[Line] == 51)//5 matches and bonus ball
  400.             {
  401.                 Winnings = Winnings + Payout5B;
  402.             }
  403.  
  404.             else if(Matches[Line] == 60)//6 matches == JACKPOT WINNAAAHHH
  405.             {
  406.                 Winnings = Winnings + Payout6;
  407.             }
  408.         }
  409.     }
  410.  
  411.     Sleep(pause * 1.5);
  412.  
  413.     if(Winnings != 0)
  414.     {
  415.         cout << "\n\nYou have won " << Pound << Winnings << "!! Congratulations!!\n\n";
  416.         Wallet = Wallet  + Winnings;
  417.         Sleep(pause * 2);
  418.         cout << "You now have " << Pound << Wallet << " in your wallet to play with!\n\n";
  419.     }
  420.  
  421.     else if(Winnings == 0 && Wallet > 0)
  422.     {
  423.         cout << "\nSorry, you have not won this time.\n\n";
  424.         Sleep(pause * 2);
  425.         cout << "You still have " << Pound << Wallet << " left in your wallet to play with.\n\n";
  426.         Sleep(pause);
  427.     }
  428.  
  429. }
  430.  
  431.  
  432. //----------------------------------------MAIN (LOOP FROM NUMBER OF LINES TO PAYOUT)-------------------------------------------------------------------------
  433. int main ()
  434. {
  435.     SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 11); //set text colour
  436.  
  437.     string Name = EnterName();
  438.     //start game by asking for player's name(calls EnterName and sets local Name variable to the value returned by the function)
  439.     Sleep(pause / 2);
  440.  
  441.  
  442.  
  443.     cout << "\nWelcome, " << Name << "!\n\tYou have " << Pound << Wallet << " in your wallet to play with!\n\n";
  444.     Sleep(pause * 1.5);
  445.  
  446.     do    //------------------------------------MAIN GAME LOOP---------------------------------------------
  447.     {
  448.         int Lines = Ticket(); //calls function for player to choose how many lines they want on this ticket
  449.         //Lines takes value returned from Ticket function, and is then passed to the following functions to
  450.         //the line length and therefore how each function will operate.
  451.  
  452.  
  453.         int **Guess = new int*[Lines];    //declares the 2D array in heap memory
  454.  
  455.         for(int i = 0; i < Lines; i++)      //sets the size of the array based on user input Lines
  456.         {
  457.             Guess[i] = new int[6];
  458.         }
  459.  
  460.  
  461.         Input(Lines, Guess);//calls the Input function so the user can set values, 6 numbers per number of lines
  462.  
  463.         int *Matches = new int[Lines];//declares array for winning score per line
  464.  
  465.         bool repeat = false;//declares and then sets repeat flag to false
  466.  
  467.         string input;//declares local input string, all cin data goes into this, then is checked if it can go into the int variable that needs it
  468.  
  469.  
  470.         do//loop this if the user wants to re-use the same ticket
  471.         {
  472.             Wallet = Wallet - Lines; //subtract £1 for each line played
  473.  
  474.             Sleep(pause / 2);
  475.             cout << "\nYou have " << Pound << Wallet << " left in your wallet.\n\n";
  476.  
  477.             int Ball[6] = {0};//declare balls array and set to 0
  478.             int BonusBall = 0;//declare bonusball variable
  479.  
  480.             if(repeat == true) // Reprint ticket it repeat is detected
  481.             {
  482.                 Sleep(pause);
  483.  
  484.                 cout << "\nYour numbers are still:\n";
  485.  
  486.                 for (int Line = 0; Line < Lines; Line++)
  487.                 {
  488.                     for(int n = 0; n < 6; n++)
  489.                     {
  490.                         cout << "\t-" << Guess[Line][n] << "-";
  491.                     }
  492.                     cout << endl;
  493.                 }
  494.                 Sleep(pause * 2);
  495.             }
  496.  
  497.  
  498.             Generate(Ball, BonusBall); //call the random ball generator to set values to Ball array and BonusBall
  499.  
  500.  
  501.             Compare(Lines, Guess, Ball, BonusBall, Matches); //call the Compare function to check for matches and set score per line
  502.  
  503.  
  504.             Payout(Lines, Matches);//call the Payout function to check score per line and total the winnings as needed.
  505.  
  506.  
  507.             if(Wallet > 0 && Wallet >= Lines)//asks if player wants to reuse the same ticket, IF they can afford it
  508.             {
  509.                 Sleep(pause * 1.5);
  510.                 bool valid = true;
  511.                 do
  512.                 {
  513.                     cout << "Would you like to play again with the same numbers? (Y/N)\n";
  514.                     input = "";
  515.  
  516.                     cin >> input;
  517.  
  518.                     if(input == "yes" || input == "Yes" || input == "y" || input == "Y" || input == "true" || input == "True" || input == "1")
  519.                     {
  520.                         repeat = true;
  521.                         valid = true;//accept answer, break loop, re using the same ticket
  522.                     }
  523.  
  524.                     else if(input == "no" || input == "No" || input == "n" || input == "N" || input == "false" || input == "False" || input == "0")
  525.                     {
  526.                         repeat = false;
  527.                         valid = true;//accept answer, break loop without using same ticket
  528.                     }
  529.  
  530.                     else
  531.                     {
  532.                         cout << "\nPlease answer with a yes or no, Y or N, true or false, or 1 or 0.\n\n";
  533.                         valid = false; //do not accept invalid answers
  534.                         Sleep(pause);
  535.                     }
  536.                 }
  537.                 while(valid == false);
  538.             }
  539.         }
  540.         while((repeat == true && Wallet > 0) && (Wallet > Lines));
  541.  
  542.  
  543.         delete Guess; //clear Guess array from memory if changing ticket size, to avoid memory leak
  544.         delete Matches; //clear Matches array from memory if changing ticket size, to avoid memory leak
  545.  
  546.  
  547.  
  548.         //end game when out of money--------------
  549.         if(Wallet <= 0)
  550.         {
  551.             running = false;
  552.             Sleep(pause / 2);
  553.             cout << "\n\n\nYou ran out of money. Thanks for playing!\n\n";
  554.             Sleep(pause * 2);
  555.         }
  556.     }
  557.     while(running==true);
  558.  
  559.     return 0;
  560. }
  561.  
  562.  
  563.  
  564.  
  565. /*
  566.  
  567. I hope you enjoy playing my game and that you find my code interesting and informative.
  568. I believe I have written a very high standard of program, that is well structured, uses functions effectively,
  569. demonstrates knowledge of stack and heap memory and when and how to appropriately use them, as well as keeping
  570. in mind the actual mechanics of gameplay, such as the use of pauses to improve readability and excitement.
  571. I believe my code is now completely bug free, which took relatively little testing, as my functions made
  572. it extremely easy to find where and when something was broken.
  573.  
  574. The only issue I have with my program, is that I cannot PREVENT a user inputting data into the cin input buffer
  575. and as such, whilst the game is printing the ticket, the player can input data and press enter during the pasues, and it will
  576. input into the next request for input, before the player even knows about it. The only way I can find to prevent
  577. this requires disabling ALL input, which, if I forget to ENABLE input, then the user cannot use their computer again
  578. short of using ctrl-alt-delete, or else simply take their keyboard away or tieing the user's hands behind their back,
  579. none of which are appropriate solutions.
  580.  
  581. I have learned a great deal, considering that only a week ago, I only knew the basic fundamentals of declaring variables, performing
  582. equations and comparisons, and using input/output of information, as well as a limited understanding of loops.
  583. I look forward to learning more in the future, including visual programming, as well as applying my new knowledge and experience
  584. within scripting for pre existing game engines.
  585.  
  586.  
  587. -Michael Brandon
  588. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement