2013-05-09 82 views
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

你可以嘗試在http://valgrind.org/下運行它,它是一個專門用於自動檢測內存損壞等事情的工具。 – Patashu 2013-05-09 22:27:49

+0

程序中的哪一點發生崩潰? – jwodder 2013-05-09 22:29:13

+1

您需要學習如何使用您正在開發的任何環境中提供的調試器,以至少在程序崩潰的地方獲得重點。這將是你的第一個線索;但是,如果程序因爲覆蓋堆或堆棧而崩潰,那麼崩潰點可能不是問題的關鍵。這就是爲什麼C很難:) – antlersoft 2013-05-09 22:35:02

回答

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]兩次,並與初始化任何其他元素,你打電話value哪些預計yourhand[0]yourhand[1]初始化。

這應該在valgrind下可見(您正在讀取來自未初始化內存的隨機值)。

+0

我該怎麼辦才能修復它?我試過擴展handIndex,並且它不時崩潰。 – user2368065 2013-05-09 23:08:06

+1

你應該修正你的邏輯。爲什麼'value'接受一個數組參數,然後假設一個不相關的非常量全局表示它的長度?把這些東西放在一起(作爲參數,而不是全局變量)。 valgrind還會給你什麼其他的錯誤?修復它們。其他崩潰在哪裏?在您的調試器中查看它們。 – Useless 2013-05-09 23:14:07