Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- //Initialize a multi dimensional array for card.
- string card[6][5] = {
- {"B", "I", "N", "G", "O"},
- {"0", "0", "0", "0", "0"},
- {"0", "0", "0", "0", "0"},
- {"0", "0", "Free", "0", "0"},
- {"0", "0", "0", "0", "0"},
- {"0", "0", "0", "0", "0"}
- };
- //Method to print card on screen
- void printCard(){
- for(int row = 0; row < 6; row++){
- for(int col = 0; col < 5; col++){
- cout << "\t" << card[row][col];
- }
- cout << endl;
- }
- }
- //Method to generate random integer with range [min, max]
- int random(int min, int max)
- {
- static bool first = true;
- if ( first )
- {
- srand(time(NULL));
- first = false;
- }
- return min + rand() % (max - min);
- }
- int main(){
- //sentinel value for retain / generate
- string repeat = "y";
- while(repeat == "y"){
- //loop from top to bottom , starting from 2nd row
- for(int col = 0; col < 5; col++){
- for(int row = 1; row < 6; row++){
- 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
- continue;
- int number = random( (col * 15) + 1 ,((col+1) * 15)); //Generate random number. ex
- //col = 0
- //col * 1 + 1 = 1 ==== minimum of first col
- //(col + 1) * 5 = 15 === maximum of first col ,
- // col till 4,
- //sentinel value for existence
- bool exist = false;
- for(int nrow = 1; nrow < 6; nrow++)
- {
- if(nrow == 3 && col == 2) //skip the middle free
- continue;
- if(number == stoi(card[nrow][col])){ //if generated num = other number , set sentinel val to true
- exist = true;
- break;
- }
- }
- if(exist) //if it exist , go back to the current row
- row--;
- else
- card[row][col] = to_string(number); //set the number to the generatedrandom number
- }
- }
- printCard(); //print the card after generating it
- cout << "Do you want to generate again? [y / n]: "; //ask the users if he / she wants to continue , if n, break the while loop.
- cin >> repeat;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement