Guest User

bingo program

a guest
Dec 2nd, 2018
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <cctype>
  3. #include <iomanip>
  4. #include <ctime>
  5.  
  6. //  constants
  7. const int MAXLEN = 128;
  8. const int MINLEN = 127;
  9. const int MAX = 12;
  10. const int MIN = 1;
  11. const int RAND = 4;
  12.  
  13. //  Enumerators
  14. enum Commands {PLAY = 'P', QUIT = 'Q'};
  15.  
  16. // Typedef
  17. typedef char BUFFER[MAXLEN];
  18.  
  19. // structs
  20. struct Player {
  21.    
  22.     int win;
  23.     int loss;
  24.     int total;
  25.  
  26. };
  27.  
  28. //  Subroutine prototypes
  29.  
  30. //  get integer in range 1- 12
  31. int getIntInRange(int min, int max);
  32.  
  33. // Dynamically allocate a 2D array to fill with randoms
  34. int** allocateIntArray(int &size);
  35.  
  36. // Dynamically allocate an array for user to enter guess's
  37. int* allocUserIntArray(int &size);
  38.  
  39. // fill player array
  40. void fillUserArray(int* arr, int size);
  41.  
  42. // output user array
  43. void outputUserArray(int* arr, int size);
  44.  
  45. //  fill array with random values between 1 and 4
  46. void fillIntArray(int** arr, int size);
  47.  
  48. //  Output the array
  49. void outputIntArray(int** arr, int size);
  50.  
  51. //  The keep playing loop
  52. void keepPlaying();
  53.  
  54. // Handles the bulk of the decisions,
  55. // created this function so keepPlaying didn't get too big
  56. void arrayHandler();
  57.  
  58. // check to see if user guessed correctly
  59. void checkForMatch(int** arr1, int* arr2, int size);
  60.  
  61. // keep track of player score
  62. void updateScore(int &counter, int &win, int &loss);
  63.  
  64. //  Next level ASCII art introduction
  65. void displayOverview();
  66. /*
  67.     Init main routine
  68. */
  69. int main(){
  70.    
  71.     std::srand(time(nullptr));
  72.     displayOverview();
  73.     keepPlaying();
  74.  
  75.     // terminate
  76.     std::cout << "\nterminating gracefully\n" << std::flush;
  77.     return 0;
  78. }
  79. /*
  80.     Looping function, will continue until result is equal
  81.     QUIT.
  82. */
  83. void keepPlaying(){
  84.  
  85.     bool loop = false;
  86.     BUFFER input;
  87.     char result;
  88.     do{
  89.         std::cout << "\n\nType (P) to play or (Q) to quit: ";
  90.         std::cin.getline(input, MAXLEN);
  91.         for (int i = 0; i < MAXLEN - MINLEN; i++){
  92.             result = std::toupper(input[i]);
  93.         }
  94.         switch (result){
  95.             case PLAY:
  96.                 std::cout << "Let the game begin!!\n";
  97.                 arrayHandler();
  98.                 break;
  99.             case QUIT:
  100.                 loop = true;
  101.                 break;
  102.             default:
  103.                 std::cout << "\nERROR [ "  << result << " ] INVALID";
  104.         }
  105.     } while (!(loop));
  106. }
  107. /*
  108.     Made the function so keepPlaying doesnt
  109.     get too convoluted. Handles passing all arrays
  110.     to their corresponding functions
  111. */
  112. void arrayHandler(){
  113.  
  114.     int size;
  115.     int** array;
  116.     int* userArr;
  117.     size = getIntInRange(MIN, MAX);
  118.     array = allocateIntArray(size);
  119.     if (array != nullptr){
  120.         fillIntArray(array, size);
  121.         userArr = allocUserIntArray(size);
  122.         if (userArr != nullptr){
  123.             fillUserArray(userArr, size);
  124.             outputUserArray(userArr, size);            
  125.             outputIntArray(array, size);
  126.             checkForMatch(array, userArr, size);
  127.             delete [] array;
  128.             delete [] userArr;
  129.         }          
  130.     }
  131. }
  132. /*
  133.     Function to get integers for both ranges 1-12
  134.     and 1-4
  135. */
  136. int getIntInRange(int min, int max){
  137.    
  138.     int userInput;
  139.     bool isValid = false;
  140.     BUFFER input;
  141.     while (!(isValid)){
  142.         std::cout << "\nEnter a number between "
  143.                   << min << " and " << max << ": ";
  144.         std::cin.getline(input, MAXLEN);
  145.         userInput = atoi(input);
  146.         if (userInput < min || userInput > max){
  147.             std::cout << "\nERROR [ "  << userInput << " ] INVALID\n";
  148.         }
  149.         else{
  150.             isValid = true;
  151.         }
  152.     }
  153.     return userInput;
  154. }
  155. /*
  156.     Create 2d array dynamically to be filled randomly
  157. */
  158. int** allocateIntArray(int &size){
  159.  
  160.     int** array = nullptr;
  161.     do{
  162.         array = new int* [size];
  163.         for (int i = 0; i < size; i++){
  164.             array[i] = new int[size];
  165.         }
  166.         if (array == nullptr){
  167.             std::cout << "Allocation failed\n";
  168.         }
  169.     } while (array == nullptr);
  170.     return array;
  171. }
  172. /*
  173.     Dynamically allocate standard array to hold
  174.     user guess's
  175. */
  176. int* allocUserIntArray(int &size){
  177.  
  178.     int* inputArr = nullptr;
  179.     do {
  180.         inputArr = new int[size];
  181.         if (inputArr == nullptr){
  182.             std::cout << "Allocation failed\n";
  183.         }
  184.     } while (inputArr == nullptr);
  185.     return inputArr;
  186. }
  187. /*
  188.     fill player array
  189. */
  190. void fillUserArray(int arr[], int size){
  191.    
  192.     if (arr == nullptr){
  193.          std::cout << "Trying to fill null array\n";
  194.     }
  195.     std::cout << "\nPlease enter " << size << " numbers\n";
  196.     for (int i = 0; i < size; i++){
  197.         arr[i] = getIntInRange(MIN, RAND);
  198.     }
  199. }
  200. /*
  201.     output user array
  202. */
  203. void outputUserArray(int* arr, int size){
  204.    
  205.     if (arr == nullptr){
  206.         std::cout << "Trying to output null array\n";
  207.     }
  208.     std::cout << "\n  Your selections:\n" << std::endl;
  209.     for (int i = 0; i < size; i++){
  210.         std::cout << std::setw(4) << arr[i];
  211.     }
  212.     std::cout << std::endl;
  213. }
  214. /*
  215.     Fill the newly allocated array with randoms
  216.     between 1 and 4
  217. */
  218. void fillIntArray(int** arr, int size){
  219.    
  220.     if (arr == nullptr){
  221.         std::cout << "Trying to fill null array\n";
  222.     }
  223.     for (int i = 0; i < size; i++){
  224.         for (int j = 0; j < size; j++){
  225.             arr[i][j] = (rand() % RAND + 1);      
  226.         }
  227.     }
  228. }
  229. /*
  230.     Output the newly allocated 2d array in a grid
  231. */
  232. void outputIntArray(int** arr, int size){
  233.    
  234.     if (arr == nullptr){
  235.         std::cout << "Trying to output null array\n";
  236.     }    
  237.     for (int i = 0; i < size; i++){
  238.         std::cout << std::endl;  
  239.         for (int j = 0; j < size; j++){
  240.             std::cout << std::setw(2) << " | " << arr[i][j];
  241.         }
  242.         std::cout << " |";
  243.     }
  244.     std::cout << std::endl;
  245. }
  246. /*
  247.     check to see if user guessed correctly
  248. */
  249. void checkForMatch(int** arr1, int* arr2, int size){
  250.    
  251.     int d1Match = 0;
  252.     int d2Match = 0;
  253.     int rMatch = 0;
  254.     int cMatch = 0;
  255.    
  256.     // checking for row matches
  257.     for (int row = 0; row < size; row++){
  258.         if (arr2[row] == arr1[0][row]){
  259.             rMatch++;
  260.         }    
  261.     }
  262.     // check for column matches
  263.     for(int col = 0; col < size; col++){
  264.         if (arr2[col] == arr1[col][0]){
  265.                 cMatch++;
  266.         }
  267.     }
  268.     // check for diagonal top to bottom
  269.     for (int i = 0; i < size; i++){
  270.         if (arr2[i] == arr1[0][0]){
  271.             d1Match++;
  272.         }
  273.     }
  274.     // check for diagonal bottom to top?
  275.     for(int i = 0; i < size; i++){
  276.         if (arr2[size - i - 1] == arr1[size - 1][0]){
  277.             d2Match++;
  278.         }
  279.     }
  280.     if (rMatch == size){
  281.         std::cout << "\n  You matched on a row\n";
  282.     }if (cMatch == size){
  283.         std::cout << "\n  You matched on a column\n";
  284.     }if (d1Match == size){
  285.         std::cout << "\n  You matched top to bottom diagonally\n";
  286.     }if (d2Match == size){
  287.         std::cout << "\n  You matched bottom to top diagonally\n";
  288.     }else if (rMatch != size && cMatch != size && d1Match != size && d2Match != size){
  289.         std::cout << "\n  No matches detected: You Lose!!\n";
  290.     }
  291. }
  292. /*
  293.     Next Level ASCII Art and description of program
  294. */
  295. void displayOverview(){
  296.  
  297.     std::system("clear");
  298.  
  299.     std::cout << "Welcome to the lotto-bingo game\n"
  300.               << "You are going to guess some numbers\n"
  301.               << "Between 1 and 4, and I'm going to generate\n"
  302.               << "a bingo card for you. You will win if you match\n"
  303.               << "a row or a column!"
  304.               << std::endl;
  305. }
Advertisement
Add Comment
Please, Sign In to add comment