2

此代码的目的是玩二十一点的模拟游戏。“庄家”是自动化的,将向自己发两张牌,并在它赢/破或击中 17 时停止。然后用户抽牌直到他/她满意。然后确定获胜者。

我遇到了一堵砖墙,因为代码编译得很好,但是当它运行时,它要么工作(我所说的工作,我的意思是不能按预期工作,但它会运行),要么它会崩溃。

我不知道这怎么会只在某些时候而不是所有时间发生,我需要一些帮助。

这是我的代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 52
#define LIMIT 21 

enum faces{Ace = 0, Jack = 10, Queen, King};
char * facecheck(int d); 
void shuffle( int deck[]);
int draw(int deck[SIZE]); 
void printcards(int hand[], int numCards);
int dealer(int deck[]);  
int player(int deck[]);
int value(int yourhand[]); 
int victory(int d, int p);
int i, numCards = 1;
int top = 52;
int preValue = 0; 
int count = 2; 
int main() 
{ 
    int deck[SIZE], i, a;
    int d, p; 
    char suits[4][9] = 
    {
        "Hearts",
        "Diamonds",
        "Clubs",
        "Spades"};


    srand( time( NULL ) ) ;

    for(i = 0; i<SIZE; i++)
    {
        deck[i] = i;
    };

    shuffle(deck);
    d = dealer(deck);
    p = player(deck);
    victory(d, p);



    return 0; 
}  

char * facecheck(int d)
{
    static char * face[] = 
    {
        "Ace",
        "Jack",
        "Queen",
        "King" };

    if(d == Ace)
        return face[0];
    else
    {
        if(d == Jack) 
            return face[1];
        else
        {
            if(d == Queen)
                return face[2];
            else 
            { 
                if(d == King)
                    return face[3];
            }
        }
    }
}



void shuffle( int deck[]) 
{
     int i, j, temp; 

     for(i = 0; i < SIZE; i++)
     {
           j = rand() % SIZE; 
           temp = deck[i];
           deck[i] = deck[j];
           deck[j] = temp;
           }
     printf("The deck has been shuffled \n"); 
} 

int draw(int deck[SIZE])
{
    int numCards = 1;
    int i;  
    int hand[numCards];
    int card;
    for(i = 0; i < numCards && top > 0; i++)
    {
        card = deck[top-1];     
        hand[i] = card; 
        top--;   
    }

    return card;

}

void printcards(int hand[], int numCard)
{   
    char suits[4][9] = 
    {
        "Hearts",
        "Diamonds",
        "Clubs",
        "Spades"};

    for(i = 0; i < numCard; i++) 
    {
     int card = hand[i];     
    if(card%13 == 0 || card%13 == 10 || card%13 == 11 || card%13 == 12)
        printf("%s ", facecheck(card%13) );
    else 
        printf("%d ", card%13+1);
    printf("of %s \n", suits[card/13]);
    }
}

int dealer(int deck[])
{
    int x;
    int a; 
    int yourhand[10];
    int handIndex = 0;
    int cardLimit;
    int dealerValue;

        yourhand[handIndex] = draw(deck);
        yourhand[handIndex] = draw(deck);
        printf("The Dealers second card is:"); 
        printcards(yourhand, handIndex+1);

        cardLimit = value(yourhand);

        do
        {
         if(cardLimit == LIMIT)
         {
             printcards(yourhand, handIndex+1);
             dealerValue = cardLimit;
             return dealerValue;
         }

         if(cardLimit > LIMIT)
         {
             printcards(yourhand, handIndex+1);
             dealerValue = cardLimit;
             return dealerValue;
         }
         if(cardLimit == 17)
         {
             printcards(yourhand, handIndex+1);
             dealerValue = cardLimit;
             return dealerValue;
         }
         if(cardLimit <= 16)
         {
            yourhand[handIndex] = draw(deck);
            cardLimit = value(yourhand);
         }
        }
         while(cardLimit <= LIMIT); 
         handIndex++;


}

int player(int deck[])
{
    int x;
    int a; 
    int yourhand[10];
    int cardLimit;
    int playerValue;
    int handIndex = 2;

    yourhand[handIndex] = draw(deck);
    yourhand[handIndex] = draw(deck);
    cardLimit = value(yourhand);
    printf("Your hand is: /n"); 
    printcards(yourhand, handIndex+1);
    printf("%d /n" , cardLimit); 

    do
        {
         if(cardLimit == LIMIT)
         {
             printcards(yourhand, handIndex+1);
             playerValue = cardLimit;
             return playerValue;
         }

         if(cardLimit > LIMIT)
         {
             printcards(yourhand, handIndex+1);
             playerValue = cardLimit;
             return playerValue;
         }
         else
         {
             printf("What would you like to do: Press 1 to Hit. 2 to Stay. \n"); 
             scanf("%d" , &x);
             if(x == 1)
         {
            yourhand[handIndex] = draw(deck);
            cardLimit == value(yourhand);
            }
            else
            {
                printf("Player choses to stay \n");
                return playerValue;
            }
        }
        }
        while(cardLimit <= LIMIT); 

        handIndex++;
}

int value(int yourhand[])
{
    int faceValue = 10;
    int cardValue[count]; 
    int aceValue = 11;
    int card[count]; 
    int value; 
    int curvalue; 


      for(i = 0; i < count; i++) 
      {
      card[i] = yourhand[i];
      }

     for(i = 0; i < count; i++) 
     { 
         cardValue[i] = card[i]%13; 
     } 

     if(cardValue[0] >= 10 && cardValue[1] >= 10)
     {
         value = 20;
     }
     if(cardValue[0] < 10 && cardValue[1] < 10) 
     {
         value = cardValue[0] + cardValue[1];
     }
     if(cardValue[0] >= 10 && cardValue[1] < 10) 
     {
         value = faceValue + cardValue[1];
     }
     if(cardValue[0] < 10 && cardValue[1] >= 10) 
     {
         value = faceValue + cardValue[0];
     }
     if(cardValue[0] == 0 && cardValue[1] == 0) 
     {
         value = 12; 
     }       
     if(cardValue[0] == 0 && cardValue[1] >= 10) 
     {
         value = 21;
     }
     if(cardValue[1] == 0 && cardValue[0] >= 10) 
     {
         value = 21;
     }        
     if(cardValue[0] == 0 && cardValue[1] < 10) 
     {
         value = aceValue + cardValue[1];
     } 
     if(cardValue[1] == 0 && cardValue[0] < 10) 
     {
         value = aceValue + cardValue[0];
     } 

     preValue = value;

     if(count > 2) 
     { 
        if(cardValue[count] != 0) 
        {
               value = curvalue; 
               value = preValue + curvalue;
        }   
        else
        {
            if(cardValue[count] + preValue > LIMIT) 
            {
                 value = preValue + 1; 
            }
            else
            {
                value = cardValue[count] + aceValue;
            }
        }
     }
     count++; 
     return value; 
}

int victory(int d, int p)
{
    if(d > p) 
    printf("Dealer Wins \n"); 
    else
    printf("Player Wins"); 
}
4

1 回答 1

3

在您的代码中

int dealer(int deck[])
{
    int handIndex = 0;
    int yourhand[10];
    yourhand[handIndex] = draw(deck);
    yourhand[handIndex] = draw(deck);

你永远不会改变handIndex,所以这两个分配是对同一个元素的(即第二个绘制覆盖第一个)

    cardLimit = value(yourhand);

现在,已经写入了yourhand[0]两次,并且没有初始化任何其他元素,您调用valuewhich 期望yourhand[0] 并被 yourhand[1]初始化。

这应该在 valgrind 下可见(您正在从未初始化的内存中读取随机值)。

于 2013-05-09T23:01:04.040 回答