处理aces并在二十一点程序中找到段错误

时间:2010-04-11 23:00:53

标签: c gcc blackjack

这是我到目前为止所拥有的...... 我还没弄明白我将如何用ace来处理11/1情况,当玩家选择击中/站立的选项时,我会遇到段错误。

HELP !!!

更新代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define DECKSIZE 52
#define VALUE 9 
#define FACE 4
#define HANDSIZE 26

typedef struct {
    int value;
    char* suit;
    char* name;
}Card;

/*typedef struct {
    int value;
    char* suit;
    char* name;
}dealerHand;

typedef struct {
    int value;
    char* suit;
    char* name;
}playerHand;*/              //trying something different

Card cards[DECKSIZE];
/*dealerHand deal[HANDSIZE];        //trying something different
playerHand dealt[HANDSIZE];*/  

char *faceName[]={"two","three", "four","five","six", "seven","eight","nine",
          "ten", "jack","queen", "king","ace"};
char *suitName[]={"spades","diamonds","clubs","hearts"};

Card *deal[HANDSIZE];
Card *dealt[HANDSIZE];

void printDeck(){
    int i;
    for(i=0;i<DECKSIZE;i++){
        printf("%s of %s value = %d\n ",cards[i].name,cards[i].suit,cards[i].value);
        if((i+1)%13==0 && i!=0) printf("-------------------\n\n");
    }
}



void shuffleDeck(){
    srand(time(NULL));
    int this;
    int that;
    Card temp;
    int c;
    for(c=0;c<10000;c++){    //c is the index for number of individual card shuffles should be set to c<10000 or more
        this=rand()%DECKSIZE;
        that=rand()%DECKSIZE;
        temp=cards[this];
        cards[this]=cards[that];
        cards[that]=temp;
    }
}

/*void hitStand(i,y){   // I dumped this because of a segfault i couldn't figure out.
    int k;
    printf(" Press 1 to HIT or press 2 to STAND:");
    scanf("%d",k);
    if(k=1){
        dealt[y].suit=cards[i].suit;
        dealt[y].name=cards[i].name;
        dealt[y].value=cards[i].value;
        y++;
        i++;
    }
}
*/  



int main(){
    int suitCount=0;
    int faceCount=0;
    int i;
    int x;
    int y;
    int d;
    int p;
    int k;
    for(i=0;i<DECKSIZE;i++){   //this for statement builds the deck
        if(faceCount<9){
            cards[i].value=faceCount+2;
        }else{    //assigns face cards as value 10
            cards[i].value=10;
        }
        cards[i].suit=suitName[suitCount];
        cards[i].name=faceName[faceCount++];
        if(faceCount==13){           //this if loop increments suit count once
            cards[i].value=11;   //all faces have been assigned, and also
            suitCount++;         //assigns the ace as 11
            faceCount=0;
        }   //end building deck
    }

    /*printDeck();  //prints the deck in order
    shuffleDeck();  //shuffles the deck
    printDeck();    //prints the deck as shuffled
        This was used in testing, commented out to keep the deck hidden!*/

    shuffleDeck();
    x=0;
    y=0;
    for(i=0;i<4;i++){       //this for loop deals the first 4 cards,
        dealt[y]=&cards[i]; //first card to player, second to dealer, as per standard dealing practice.
        i++;
        y++;
        deal[x]=&cards[i];
        x++;
    }

    printf(" Dealer's hand is: %s of %s and XXXX of XXXX.   (Second card is hidden!)\n",deal[0]->name,deal[0]->suit,deal[1]->name,deal[1]->suit);
    printf(" Player's hand is: %s of %s and %s of %s.\n",dealt[0]->name,dealt[0]->suit,dealt[1]->name,dealt[1]->suit);

    printf(" the current value of the index i=%d\n",i); //this line gave me the value of i for testing

    d=deal[0]->value+deal[1]->value;
    p=dealt[0]->value+dealt[1]->value;
    if(d==21){
        printf(" The Dealer has Blackjack!  House win!\n");
    }else{
        if(d>21){
            printf(" The dealer is Bust!  You win!\n");
        }else{
            if(d>17){
                printf(" Press 1 to HIT or 2 to STAND: ");
                scanf("%d",&k);
                if(k==1){
                    dealt[y]=&cards[i];
                    y++;
                    i++;

                }
            }else{
                if(d<17){
                    printf(" Dealer Hits!");
                    deal[x]=&cards[i];
                    x++;
                    i++;
                }       
            }
        }
    }

    return 0;

}

3 个答案:

答案 0 :(得分:4)

要修复您的段错误,请使用scanf("%d",&k);而不是scanf("%d",k);(请注意我添加的&符号。您需要这样做,因为scanf的第二个参数是指向它的位置的指针应该存储被读入的内容。k本身不是指针 - 添加&获取指向k的指针。

对于处理aces,在什么条件下aces从11的值变为值1?您希望必须触摸代码中的哪些行才能启用它?我可以想出一种实现它的方法,它涉及一个变量,用于记录计算玩家总分数的A个数量,或者另一种方法是及时重新计算得分以便使用它。

答案 1 :(得分:4)

一般情况下:启用警告进行编译往往会有所帮助(默认情况下,gcc对你没什么帮助!)。

比较

$ gcc -o blackjack blackjack.c
$ 

使用:

$ gcc -Wall -o blackjack blackjack.c 
blackjack.c: In function 'main':
blackjack.c:124: warning: too many arguments for format
blackjack.c:139: warning: format '%d' expects type 'int *', but argument 2 has type 'int'
$ 

这会立即发现scanf()问题(和另一个问题!)。

答案 2 :(得分:1)

请参阅this question了解如何处理这种情况。

作为关于代码的一般提示;你有三次相同的结构(卡,经销商手,玩家手)。为卡片定义一次结构就足够了,例如,指向存储为手牌的卡片。