2013-05-09 107 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