简体   繁体   English

c ++程序取卡号

[英]c++ progam to take card number

I'm working on the following game in C++:我正在用 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.用户输入一个数字 (n),程序随机抽取一组在全局数组 main 顶部声明的卡片。 It then outputs a random numbers of (n) cards each time.然后它每次输出随机数量的 (n) 张卡片。

The problem is that the number, eg '3 Hearts', appears more than one time.问题是数字,例如“3 Hearts”,出现了不止一次。 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.您可以通过 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.这意味着如果你抽取任何一张牌,因此将cards_remaining 减少1,你将无法再抽取俱乐部之王。 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--;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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