views:

179

answers:

3

Here's what i have so far... I have yet to figure out how i'm going to handle the 11 / 1 situation with an ace, and when the player chooses an option for hit/stand, i get segfault.

HELP!!!

updated code

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

}
+4  A: 

To fix your segfault, use scanf("%d",&k); instead of scanf("%d",k); (note the ampersand that I added. You need that because scanf's second argument is a pointer to the location where it should store what gets read in. k by itself isn't a pointer--adding the & gets a pointer to k.

For handling aces, under what conditions do the aces go from valued at 11 to valued at 1? What line(s) in your code do you expect to have to touch to enable that? I can think of a way to implement it involving a variable that keeps track of how many aces went into computing a player's total score, or another way would be to recompute a score just in time to use it.

sblom
i can't believe i missed that on the segfault. thanks.I'm thinking on the ace issue, something like if d>21 and (dont know how to code this..) deal[(any of the cards values)].value==11then change the value to 1.but again, i dont know how to code that... probably should go around line 134...
Bill Adams
@Bill, I think you're on the right track. What about just keeping a count of how many aces you've dealt to each player so far. When you need to, subtract 10 from the player's score, and decrement the number of aces in the player's hand.
sblom
i'm not even sure how i'd go about this... god i'm such a noob...
Bill Adams
+1  A: 

See this question on how to deal with the ace situation.

As a general tip about your code; you have essentially the same struct three times (Card, dealerHand, playerHand). It would suffice to define the struct once for the card and, for example, have pointers to the cards stored as the hands.

Arkku
Ok... that sounds simpler... but i'm such a noob at this, that I don't even know where to begin to do that...
Bill Adams
Well, for example, delete the `dealerHand` and `playerHand` structs, and declare the arrays for them like `Card *deal[HANDSIZE];` Then assign the cards like `deal[0] = `. You can then refer to the card's data from the hands like `deal[0]->suit`. For one this saves you having to copy three pieces of data every time you deal a card.
Arkku
ugh... this is going to take a couple minutes to convert everything...i'll edit the post when i'm there...
Bill Adams
ok i've updated it... and it seems to compile and run...totalling all the cards is still warping my mind...
Bill Adams
+4  A: 

In general: compiling with warnings enabled tends to be helpful (gcc doesn't help you out much by default!).

Compare:

$ gcc -o blackjack blackjack.c
$ 

with:

$ 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'
$ 

This spots the scanf() problem (and another one!) straight away.

Matthew Slattery
+1 for demanding compilation with warnings turned on; if professionals regard it as essential then there is simply no excuse for a learner to think they don't need it.
PP
+1, and I'd also add `-std=c99 -pedantic` or `-ansi -pedantic`.
Arkku