Advertisement
icorrelate

Bingo.cpp

Jul 22nd, 2017
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.     //Initialize a multi dimensional array for card.
  4.     string card[6][5] = {
  5.            {"B", "I", "N", "G", "O"},
  6.            {"0", "0", "0", "0", "0"},
  7.            {"0", "0", "0", "0", "0"},
  8.            {"0", "0", "Free", "0", "0"},
  9.            {"0", "0", "0", "0", "0"},
  10.            {"0", "0", "0", "0", "0"}
  11.            };
  12.    
  13. //Method to print card on screen
  14. void printCard(){
  15.    
  16.     for(int row = 0; row < 6; row++){
  17.         for(int col = 0; col < 5; col++){
  18.                 cout << "\t" << card[row][col];
  19.         }
  20.         cout << endl;
  21.     }    
  22.        
  23. }
  24.  
  25. //Method to generate random integer with range [min, max]
  26. int random(int min, int max)
  27. {
  28.    static bool first = true;
  29.    if ( first )
  30.    {  
  31.       srand(time(NULL));
  32.       first = false;
  33.    }
  34.    return min + rand() % (max - min);
  35. }
  36.  
  37. int main(){
  38.     //sentinel value for retain / generate
  39.     string repeat = "y";
  40.    
  41.     while(repeat == "y"){                  
  42.         //loop from top to bottom , starting from 2nd row
  43.         for(int col = 0; col < 5; col++){
  44.             for(int row = 1; row < 6; row++){      
  45.                 if(row == 3 && col == 2)                    //This is for FREE , i want to go to the next loop if the row = 3 and col = 2 *starting from 0
  46.                     continue;
  47.                                  
  48.                 int number = random(  (col * 15) + 1   ,((col+1) * 15));        //Generate random number. ex
  49.                
  50.                 //col = 0
  51.                 //col * 1 + 1 = 1 ==== minimum of first col
  52.                 //(col + 1) * 5 = 15 === maximum of first col ,
  53.                 // col till 4,
  54.                
  55.                 //sentinel value for existence
  56.                 bool exist = false;
  57.                 for(int nrow = 1; nrow < 6; nrow++)
  58.                 {
  59.                     if(nrow == 3 && col == 2)               //skip the middle free
  60.                     continue;
  61.                    
  62.                     if(number == stoi(card[nrow][col])){     //if generated num = other number , set sentinel val to true
  63.                        exist = true;
  64.                         break;  
  65.                     }
  66.                 }
  67.                
  68.                 if(exist)                                   //if it exist , go back to the current row
  69.                     row--;
  70.                 else
  71.                     card[row][col] = to_string(number);     //set the number to the generatedrandom number
  72.             }                    
  73.         }
  74.         printCard();                                        //print the card after generating it
  75.        
  76.         cout << "Do you want to generate again? [y / n]: ";     //ask the users if he / she wants to continue , if n, break the while loop.
  77.         cin >> repeat;
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement