简体   繁体   中英

c++ progam to take card number

I'm working on the following game in C++:

The user enters a number (n) and the program takes a random collection of cards that are declared at the top of main in global arrays. It then outputs a random numbers of (n) cards each time.

The problem is that the number, eg '3 Hearts', appears more than one time. I made a function to correct that, however it didn't solve the issue.

Can someone take a look at the following code and help me find what could be causing the problem?

#include <iostream>
#include <time.h>
#include <string>
using namespace std;

string type[4] = {"Hearts" , "Diamonds" , "Spades" , "Clubs"};
string num[13] = {"1","2","3","4","5","6","7","8","9","10","J","Q","K"};

int random(int x){
    return rand()%x;
}

bool isDrawn(int);
void DrawCard();
int card_remaining = 52;
bool card_is_drawn[52] = {false};

int main(){

    while(1){
    cout<<"\n Enter A Card Number  :   ";
    int n;
    cin>>n;
        if(card_remaining <= 0){
            card_remaining = 52;
            cout<<" No More Cards  ,  Refreshing ...\n";
            cout<<"  Refresh Done ! Try Again if you Want \n";
            for(int i=0;i<52;i++)
                card_is_drawn[i] = false;
        }
        else{
                for(int i=0;i<n;i++){
                DrawCard();
                }
        }
            
    }

    cout<<endl;
    system("PAUSE");
    return 0;
}
void DrawCard(){
    bool check_1 = false;
    int card;
    while(!check_1){
        card = random(card_remaining);
        if(!isDrawn(card))
        check_1 = true;
    }
    if(check_1)
        cout << num[card%13]<<"   OF   " << type[card/13] << endl ;

    card_remaining--;
}
bool isDrawn(int x){
    if(card_is_drawn[x] == false){
        card_is_drawn[x] = true;
        return true;
    }
    return false;
}

Check the function

bool isDrawn(int x){
    if(card_is_drawn[x] == false){
        card_is_drawn[x] = true;
        return true;
    }
    return false;
}

You might want to exchange the both return values. That means:

if(card_is_drawn[x] == false) {
    ...
    return false;    //since the card was NOT drawn;

And at the end of the function:

return true;    //since the if-clause evaluated as false what means that the card was drawn;

By the way:

You get your random card by rand()%cards_remaining. That means if you draw ANY card and therefore reduce cards_remaining by one you won't be able to draw the King of Clubs anymore. And going on like this you will loose cards from the 'end' of your deck.

should be:

void DrawCard(){
    if (card_remaining <= 0)    // no cards left? can't draw
        return;

    bool check_1 = false;
    int card;
    while(!check_1){
        card = random(52);  // here 52
        if (isDrawn(card))  // here, if the card HAS been drawn
            check_1 = true;
    }
    if(check_1)
        cout << num[card%13]<<"   OF   " << type[card/13] << endl ;

    card_remaining--;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM