Thread: Blackjack

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101

    Blackjack

    Hi there.

    I am trying to write a blackjack game and I am running into a few teething problems. My main() function currently outputs what values are stored for certain variables, just to see if I'm doing things properly. Here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define MAX 10
    
    typedef struct {
      int card;
      int suit;
      int value;
    } card_t;
    
    void generate_card(card_t *crd, int *num, int *score);
    
    int main(void) {
    
      /* current scores for dealer and player */
      int p_score = 0;
      int d_score = 0;
    
      /* number of cards in their hand */
      int p_numofcards = 0;
      int d_numofcards = 0;
    
      /* initialise hands */
      card_t p_hand[MAX];
      card_t d_hand[MAX];
    
      srand((unsigned int)time(NULL));
    
      int i;
      for(i=0;i<2;i++) {          /*loop twice because at the start of blackjack, each player is given two cards*/
        generate_card(&p_hand[i], &p_numofcards, &p_score);
        generate_card(&d_hand[i], &d_numofcards, &d_score);
      }
    
      /* test */
      printf("p_score=&#37;d, p_numofcards=%d\n", p_score, p_numofcards);
      printf("d_score=%d, d_numofcards=%d\n", d_score, d_numofcards);
    int k;
      for(k=0;k<2;k++) {
        printf("p_hand[%d].card=%d,"
               " p_hand[%d].suit=%d, "
               " p_hand[%d].value=%d\n",
                k,p_hand[i].card,
                k,p_hand[i].suit,
                k,p_hand[i].value
               );
    
      printf("d_hand[%d].card=%d,"
             " d_hand[%d].suit=%d,"
             " d_hand[%d].value=%d\n",
              k,d_hand[i].card,
              k,d_hand[i].suit,
              k,d_hand[i].value
            );
      }
    
      return 0;
    }
    
    void generate_card(card_t *crd, int *num, int *score) {
    
      /* crd = (card_t *)malloc(MAX*sizeof(card_t)); */
    
      crd->card = (rand() % 13) + 1;
      crd->suit = (rand() % 4) + 1;
      int c;
      for(c=1;c<11;c++) {   
        if(crd->card == c) {  /*if card is 1-10 (ace- 10) then let the value of the card be whatever it is*/
          crd->value = c;       
          (*score)+=c;              /*adds to the score (eg. 10 of hearts would add a 10) */
          (*num)++;                 /*increments the number of cards in the players' hand*/
        }
      }
      switch(crd->card) {
        case 11: case 12: case 13:            /* if card is jack, queen or king...*/
          crd->value = 10;
          (*score)+=10; 
          (*num)++;   
          break;
      }
    
      /*free(crd);*/
    }
    Let me explain a few things. The card_t type represents with the following attributes: card( 1-13 for ace-king), suit(1-4 for hearts, clubs etc), value( ace=1, jack=10, queen=10 and so on). The generate_card function is supposed to generate a random card to simulate the dealer dealing unkown cards to the participants of the game. The program compiles but outputs rather absurd data:

    Code:
    $ gcc -o bjack bjack.c
    $ ./bjack
    p_score=8, p_numofcards=2
    d_score=8, d_numofcards=2
    p_hand[0].card=0, p_hand[0].suit=0,  p_hand[0].value=0
    d_hand[0].card=0, d_hand[0].suit=134513052, d_hand[0].value=24641422
    p_hand[1].card=0, p_hand[1].suit=0,  p_hand[1].value=0
    d_hand[1].card=0, d_hand[1].suit=134513052, d_hand[1].value=24641422
    Another run to show it is producing random cards correctly:
    Code:
    $ ./bjack
    p_score=11, p_numofcards=2
    d_score=6, d_numofcards=2
    p_hand[0].card=0, p_hand[0].suit=0,  p_hand[0].value=0
    d_hand[0].card=0, d_hand[0].suit=134513052, d_hand[0].value=24641422
    p_hand[1].card=0, p_hand[1].suit=0,  p_hand[1].value=0
    d_hand[1].card=0, d_hand[1].suit=134513052, d_hand[1].value=24641422
    Any help would be much appreciated, thanks.
    Last edited by Tommo; 06-14-2007 at 01:32 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  2. Help with Blackjack program
    By sugie in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2005, 12:30 AM
  3. Blackjack!
    By Dr. Bebop in forum Game Programming
    Replies: 1
    Last Post: 10-03-2002, 08:58 PM
  4. Blackjack
    By the_head in forum C Programming
    Replies: 1
    Last Post: 08-03-2002, 08:57 AM
  5. BlackJack Program...
    By 67stangman in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2002, 10:44 PM