当前位置:   iOS > 正文

数组元素的总和返回错误的值

c++,ios,go,DevBox,在线流程图,编程,编程问答,程序员,开发者工具,开发工具,json解析,二维码生成,unix时间戳,在线开发工具,前端开发工具,开发人员工具,站长工具

我正在尝试编写一个二十一点游戏.直到最后一部分它进展顺利.我的代码如下:

#include 
#include 
#include 
#include 
using namespace std;

//Declarations
void makedeck(int deck[]);
int getTopCard(int deck[]);
void addCardToHand(int hand[], int drewCard);
void firstHand(int pHand[], int dHand[], int deck[]);
void printHands(int pHand[], int dHand[]);
void showCard(int card);
int getHandValue(int hand[]);


int main()
{

    int pHand[10]= {0}; //Player's Hand
    int dHand[10]= {0}; //Dealer's Hand
    int deck[52];      //Deck

    makedeck(deck);    //Generating an ordered deck
    random_shuffle(&deck[0],&deck[52]); //Shuffling the deck
    firstHand(pHand,dHand,deck); //Dealing first hands.
    printHands(pHand,dHand); // Printing hands


    return 0;
}

/*Purpose of this function is to create 4 different suits with 14 different cards in all of them, a complete deck*/
void makedeck(int deck[])
{
    int a=0;
    for(int x=101; x<114; x++)
    {
        deck[a]=x;
        a++;
    }
    for(int x=201; x<214; x++)
    {
        deck[a]=x;
        a++;
    }
    for(int x=301; x<314; x++)
    {
        deck[a]=x;
        a++;
    }
    for(int x=401; x<414; x++)
    {
        deck[a]=x;
        a++;
    }

}

/*This function returns a topcard integer as value, that is the card at top of deck, then nullifies it from deck to prevent
it from being drawn again.*/
int getTopCard(int deck[])
{

    int topcard;
    for(int x=0; x<52; x++)
    {
        if(deck[x]!=0)
        {
            topcard=deck[x];
            deck[x]=0;
            break;
        }
    }
    return topcard;
}

/*This function draws four cards from top of deck and deals them respectively to the order of rules*/
void firstHand(int pHand[], int dHand[], int deck[] )
{

    addCardToHand(pHand , getTopCard(deck) );
    addCardToHand(dHand , getTopCard(deck) );
    addCardToHand(pHand , getTopCard(deck) );
    addCardToHand(dHand , getTopCard(deck) );

}

/*This function is for further Hit actions. Adds the drawnCard to the desired hand. The value of drawnCard is returned
from the getTopCard() function*/
void addCardToHand(int hand[], int drawnCard)
{

    for(int x=0; x<=10; x++)
    {
        if(hand[x]!=0)
        {
            continue;
        }
        if(hand[x]==0)
        {
            hand[x]=drawnCard;
            break;
        }
    }
}

/*This function determines the values of cards. Since suits that reside in the 100s part are not relevant to the value
of the cards and are only required for seperating two cards with same value, in order to remove them from the information
that we'll print, we use % on card values*/
void showCard(int card)
{
    if(card==0) cout << " ";
    else
        switch(card%100)
        {

        case 1:
            cout << "A ";
            break;
        case 11:
            cout << "J ";
            break;
        case 12:
            cout << "Q ";
            break;
        case 13:
            cout << "K ";
            break;
        default:
            cout << card%100 << " ";
        }
}

/*Prints both hands*/
void printHands(int pHand[],int dHand[])
{
    cout << "Dealer's hand: ";
    for(int y=0; y<10; y++)
    {
        showCard(dHand[y]);
    }
    cout << getHandValue(dHand) << endl;

    cout << endl << "Player's hand: ";
    for(int x=0; x<10; x++)
    {
        showCard(pHand[x]);
    }
    cout << getHandValue(pHand) << endl;
}

/*Sums all the values of cards in the hand and returns it as an integer*/
int getHandValue(int hand[])
{
    int sum=0;
    for(int x=0; x<10; x++)
    {
        if(hand[x]%100==11) sum+=10;
        if(hand[x]%100==12) sum+=10;
        if(hand[x]%100==13) sum+=10;
        else sum+=hand[x]%100;
    }
    return sum%100;
}

现在,当我执行它时,我得到以下内容:

Dealer's hand: K 3         13

Player's hand: 5 Q         27

经销商的手是正确的,但是因为5 + Q应该是15而不是27,所以有一些错误会导致错误的打印,但到目前为止我找不到它.有人可以帮忙吗?



1> JSF..:

if在需要的地方使用过else if

    if(hand[x]%100==11) sum+=10;
    if(hand[x]%100==12) sum+=10;
    if(hand[x]%100==13) sum+=10;
    else sum+=hand[x]%100;

应该

    if(hand[x]%100==11) sum+=10;
    else if(hand[x]%100==12) sum+=10;
    else if(hand[x]%100==13) sum+=10;
    else sum+=hand[x]%100;

如果你还想正确处理ace:

/*Sums all the values of cards in the hand and returns it as an integer*/
int getHandValue(int hand[])
{
    bool ace=false;
    int sum=0;
    for(int x=0; x<10; x++)
    {
        if(hand[x]%100==1) ace=true;
        if(hand[x]%100>=11) sum+=10;
        else sum+=hand[x]%100;
    }
    if ( sum<12 && ace ) sum+=10;
    return sum;
}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/iOS/detail/8610
推荐阅读
相关标签
  

闽ICP备14008679号