c - Intermittent Failure with Blackjack code -


the purpose of code play simulated game of blackjack. 'dealer' automated , deal 2 cards , stop when wins/busts or hits 17. user draws until he/she satisfied. winner determined.

i've run brick wall because code compiles fine, when runs either work (and work mean not work intended run) or crash.

i have no idea how can happen of time , not of time , need help.

here code:

#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; < 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; < 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; < 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);                  {          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);               {          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 do: press 1 hit. 2 stay. \n");               scanf("%d" , &x);              if(x == 1)          {             yourhand[handindex] = draw(deck);             cardlimit == value(yourhand);             }             else             {                 printf("player choses 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; < count; i++)        {       card[i] = yourhand[i];       }       for(i = 0; < 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");  } 

in code

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

you never change handindex, these 2 assignments same element (ie, second draw overwrites first)

    cardlimit = value(yourhand); 

now, having written yourhand[0] twice, , without initialising other elements, call value expects yourhand[0] and yourhand[1] initialised.

this should visible under valgrind (you're reading random values uninitialized memory).


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -