2010-04-11 16 views
0

這是我到目前爲止... 我還沒有弄清楚我將如何處理11/1的情況與王牌,當玩家選擇打擊/立場的選項,我得到段錯誤。處理ace和在二十一點程序中找到段錯誤

幫助!!!

更新的代碼

#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; 

} 
+1

-1無法編譯啓用警告 - 我欣賞這可能會受到傷害,但我寧願你生我的氣,也不會忘記將來使用編譯器警告。 – 2010-04-11 23:36:15

+1

我改進了你的頭銜。語言,編譯器以及作業作業的事實應該用標籤來表示;標題用於描述問題或問題。 – 2010-04-11 23:37:40

+0

我如何(不使用邪惡的GOTO)在if語句中獲得EOF(在p> 21或d> 21的情況下)? – 2010-04-12 01:16:19

回答

4

要解決你的內存設計缺陷,使用scanf("%d",&k);代替scanf("%d",k);(請注意,我說的符號,你需要的是因爲scanf的第二個參數是指向的位置在那裏應存儲哪些內容在閱讀k本身並不是一個指針。 - 添加&獲得一個指向k

如需辦理尖子,在什麼條件下的尖子去從價值11到價值1?您希望代碼中的哪些行需要觸及以啓用該代碼行?我可以想到一種實現它的方法,其中包含一個變量,用於跟蹤有多少玩家計算玩家的總分數,或者另一種方法是及時重新計算分數以使用它。

+0

我不能相信我錯過了在段錯誤。謝謝。 我正在考慮ACE問題,就像d> 21和(不知道如何編碼這個..)deal [(任何卡片值)]。value == 11 然後將值更改爲1 。 但是,我不知道如何編碼...可能應該繞行134 ... – 2010-04-11 23:15:04

+0

@Bill,我認爲你是在正確的軌道上。只要保持你已經處理過的每個球員到目前爲止有多少個球員的數量。當你需要時,從玩家的分數中減去10,並減少玩家手中的ace數量。 – sblom 2010-04-12 00:01:58

+0

我甚至不確定我會如何去做這件事...上帝,我是這樣一個noob ... – 2010-04-12 00:09:41

1

查看this question關於如何處理王牌情況。

作爲您的代碼的一般提示;你有三次基本相同的結構(卡片,經銷商手,玩家手)。只需爲卡片定義一次結構就足夠了,例如,指向存儲爲手的卡片。

+0

好吧...聽起來更簡單...但我是這樣一個小白菜,我甚至不知道從哪裏開始做... – 2010-04-11 23:11:38

+0

那麼,例如,刪除'dealerHand'和'playerHand'結構體,併爲它們聲明數組,像'Card * deal [HANDSIZE];'然後分配比如'deal [0] =&cards [51];'。然後,您可以從手中引用卡片上的數據,例如deal [0] - > suit'。這樣可以節省您每次處理卡時需要複製三份數據的功能。 – Arkku 2010-04-11 23:17:26

+0

呃...這將需要幾分鐘時間來轉換所有內容... 我會編輯帖子,當我在那裏... – 2010-04-11 23:27:58

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()問題,立竿見影。

+0

+1爲苛刻的編譯與警告打開;如果專業人士認爲它是必不可少的,那麼根本沒有理由讓學習者認爲他們不需要它。 – 2010-04-11 23:35:18

+0

+1,我還會添加'-std = c99 -pedantic'或'-ansi -pedantic'。 – Arkku 2010-04-12 00:27:54