Thread: blackjack game Please, please help!!

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    13

    blackjack game Please, please help!!

    I have to write a blackjack game for my C programming class. I have most of the code written, but I need some help. Please can
    someone help. Here's my code.
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    #define DECKS 312
    #define HEARTS 0x03
    #define DIAMONDS 0x04
    #define CLUBS 0x05
    #define SPADES 0x06

    int GetCard();
    int ConvertAce(int value);
    int DealerAI();
    void DisplayCards();
    void InputChoice();
    void InputChoice2();
    void Results();
    void SorH();
    void Continue();
    int draw_card(void);
    void shuffle(void);
    int rnd(int range);
    void seedrnd(void);

    int card1, card2, dealerCV, bet, dcard1, dcard2;
    int playerCV = 0;
    int cash = 500;
    int cards[DECKS];
    int r=DECKS;
    int card;
    char c,s;

    void main()
    {
    srand(time(NULL));
    DisplayCards();

    }
    int GetCard()
    {

    while( r--)
    {
    card = draw_card();
    switch((card % 13) + 1)
    {
    case(1):
    c = 'A';
    break;
    case(10):
    c = 'T';
    break;
    case(11):
    c = 'J';
    break;
    case(12):
    c = 'Q';
    break;
    case(13):
    c = 'K';
    break;
    default:
    c = (card%13) + '1';
    }

    if(card>=0 && card<=13)
    s = HEARTS;
    else if(card>=14 && card<=26)
    s = DIAMONDS;
    else if(card>=27 && card<=39)
    s = CLUBS;
    else if(card>=40 && card<=52)
    s = SPADES;
    else if(card>=53&& card<=65)
    s = HEARTS;
    else if(card>=66 && card<=78)
    s = DIAMONDS;
    else if(card>=79 && card<=91)
    s = CLUBS;
    else if(card>=92 && card<=104)
    s = SPADES;
    else if(card>=105&& card<=117)
    s = HEARTS;
    else if(card>=118 && card<=130)
    s = DIAMONDS;
    else if(card>=131 && card<=143)
    s = CLUBS;
    else if(card>=144 && card<=156)
    s = SPADES;
    else if(card>=157&& card<=169)
    s = HEARTS;
    else if(card>=170 && card<=182)
    s = DIAMONDS;
    else if(card>=183 && card<=195)
    s = CLUBS;
    else if(card>=196 && card<=208)
    s = SPADES;
    else if(card>=209 && card<=221)
    s = HEARTS;
    else if(card>=222 && card<=234)
    s = DIAMONDS;
    else if(card>=235 && card<=247)
    s = CLUBS;
    else if(card>=248 && card<=260)
    s = SPADES;
    else if(card>=261 && card<=273)
    s = HEARTS;
    else if(card>=274 && card<=286)
    s = DIAMONDS;
    else if(card>=287 && card<=299)
    s = CLUBS;
    else
    s=SPADES;
    return r;

    }
    }
    int draw_card(void)
    {
    int card;

    do
    {
    card=rnd(DECKS);
    }
    while(cards[card]);
    cards[card] = 1;
    return(card+1);
    }
    void shuffle(void)
    {
    int x;

    seedrnd();
    for(x=0; x<DECKS; x++)
    cards[x] = 0;
    }

    int rnd( int range)
    {
    int d;
    d = rand()%range;
    return(d);
    }
    void seedrnd(void)
    {
    srand((unsigned) time(NULL));
    }

    int ConvertAce(int value)
    {
    return 0;
    }
    void DisplayCards()
    {
    int flag1, flag2, dflag1, dflag2;

    playerCV = 0;
    dealerCV = 0;
    dflag1 = 0;
    dflag2 = 0;
    dcard1 = GetCard();
    dcard2 = GetCard();
    flag1=0;
    flag2=0;
    card1 = GetCard();
    card2 = GetCard();
    printf("Cash : $", cash);
    if(cash<=0){
    printf("You are bankrupt, GAME OVER!");
    cash = 500;
    Continue();
    DisplayCards();
    }
    if (card1 == 11){
    printf("Your first card is %c\n.", r);
    flag1=1;
    }
    else if (card1 == 1){
    printf("Your first card is %c\n.", r);
    flag1=1;
    }
    else
    printf("Your first card is %d.\n", r);
    printf("Place your bet: $ ", &bet);
    if(bet > cash){
    printf("You cannot bet more than what you have.\n");
    }
    if (dcard1 == 11){
    printf("Dealer's first card is %c\n.", r);
    dflag1=1;
    }
    else
    printf("Dealer's first card is%c.\n", r);

    Continue();
    DisplayCards();
    }
    if (card2==11){
    printf("Your second card is %d.\n", r);
    flag2=1;

    else if (card2==1){
    printf("Your second card is %d.\n", r);
    flag2=1;
    }
    else
    printf("Your second card is %d.\n", r);
    if (flag1==1){
    card1=11;
    }
    if (flag2==11){
    card2=11;
    }
    playerCV = card1 + card2;

    if (22<=playerCV && flag1=1){
    card1=1;
    }
    playerCV = card1 + card2;

    if (22<=playerCV && flag2=2){
    card2=1;
    }
    playerCV = card1 + card2;
    }
    void SorH()
    char choice;
    {
    printf("Enter choice (H)it, (S)tand, s(P)lit, (D)ouble, (I)nsuranc.\n");

    while (choice = getchar())

    switch ( choice ){
    case 'H': case 'h':
    playerCV = playerCV + GetCard();
    if (playerCV > 21 && flag1==1){
    playerCV=-10;
    else if (playerCV > 21 && flag2==1)
    playerCV=-10;
    else if (playerCV > 21)
    system("CLS");
    printf("Your total card value is %d\n.", playerCV);
    printf("You bust\n");
    cash = cash - bet;
    Continue();
    DisplayCards();
    }
    system("CLS");
    printf("Your total card value is %d\n.", playerCV);
    SorH();
    break;
    case 'S': case 's':
    system("CLS");
    Results();
    break;
    case 'P': case 'p':
    split();
    break;
    case 'D': case 'd':
    Double();
    break;
    case 'I': case 'i':
    Insurance();
    break;
    }
    void Results()
    {
    dealerCV = DealerAI();
    printf("Your total card value is \n", playerCV);
    if (playerCV > 21){
    printf("You bust \n.");
    cash = cash = bet;
    Continue();
    DisplayCards();
    }
    else
    printf("Dealer's total card value is %d.\n", dealerCV);
    if (delaerCV > 21){
    printf("Dealer busts.\n");
    cash = cash + bet;
    Continue();
    DisplayCards();
    }
    else {
    if (playerCV > dealerCV){
    printf("You win.\n");
    cash = cash + bet;
    }
    if ( playerCV=<dealerCV){
    printf("You lose.\n");
    cash = cash - bet;
    }
    Continue();
    DisplayCards();
    }
    }
    int DealerAI()
    {
    int c1, c2, cv, f1, f2;
    c1 = GetCard();
    c2 = GetCard();

    if (c1 == 11)
    f1 = 1;

    if ( c2 == 11)
    f2 = 1;
    cv = c1 + c2;

    while ( cv<= 16)
    cv = cv + GetCard();
    if (cv > 21 & f1 == 1){
    while (cv <= 16)
    cv = 1 + c2 + GetCard();

    }

    if (cv > 21 & f2 == 1){
    while (cv <= 16){
    cv = c1 + 1 + GetCard();
    }
    return cv;
    }
    void Continue()
    char decide;
    {
    printf("Enter (Q)uit, (R)estart.\n");

    while ( decide = getchar());
    switch ( decide ){
    case 'Q': case 'q':
    end game;
    case 'R': case 'r':
    printf("Press R to restart game"\n);
    system("CLS")
    }
    Thanks in advance for all the help!

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    >can someone help.

    Sure. If you tell us what you need help with. Does it compile ? Does it link ? Does it run as expected ? If not, what step fails, and what is the errormessage ?

    void Continue()
    char decide;
    {
    This is very (!) old fashioned, and as you don't use it anywhere else, I guess your compiler will flag you an error for it. Put the variable inside the function.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>switch ( decide )//use toupper here to convert all to upper case and reduce code length

    don't forget the syntax of switch
    ie
    case XYZ:
    break;
    default:
    break;

    Also at least run it through a compiler and remove the typos.

    ie
    printf("Dealer's total card value is %d.\n", dealerCV);
    if (delaerCV > 21)

    Could you be more specific about the problems.

    Please use the {code} {/code} [using square brackets instead of chicken lips] tags as this makes large code readable.
    Too hard to follow without.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

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. 2D Game project requires extra C++ programmers, new or experienced
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 05-16-2007, 10:46 AM
  3. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-22-2006, 12:23 AM
  4. Game Engine Link Prob
    By swgh in forum Game Programming
    Replies: 2
    Last Post: 01-26-2006, 12:14 AM
  5. Game Programmer's AIM Circle: Join Today
    By KingZoolerius66 in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 12-20-2003, 12:12 PM