Blackjack in C [Need help]

This is a discussion on Blackjack in C [Need help] within the Game Programming forums, part of the General Programming Boards category; Hello guys, First of all I just wanna say... Yes, I have used the search function and, no I didn't ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    3

    Blackjack in C [Need help]

    Hello guys,
    First of all I just wanna say... Yes, I have used the search function and, no I didn't find the answer I was looking for.

    I have a project due in a week or so, I have to code a "simple" game of blackjack... The catch is, it's not simple

    So...
    Here is the catch (or the many catchii, or is it catches?:P)
    -When you start the program you must have an option to start a new game or load from file
    -There are two decks of cards and each card must be removed from the deck and placed into the players hand when it is drawn.
    -You must have the option of selecting AI players
    -The dealer must also be an AI player
    -Each player has to make a bet, and the dealer will pay every player whose point total exceeds his own
    -The players data must be saved as a Struct so as to accomodate as many players as the user desires.

    I'm sorry if I'm boring you. I'm completely stumped here...
    This is what I've though of so far:
    -The cards are saved in two 4x13 arrays, Deck1 and Deck2.
    -Random roll from 1 to 2 determines which deck.
    -Random roll from 1-4 determines which suit.
    -Random roll from 1-13 determines which card.
    -Then a function would determine the value of the value of the card depending on row number

    The things I have no idea how to do:
    -Remove each drawn card from the array
    -Save a game and allow it to be continued and, by extension, load game.

    I'm aware that this may be trivial to many of you experienced programmers. I've been studying C for about two weeks now.

    Help much appreciate friends
    -Karim

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Quote Originally Posted by Karim Bissar View Post
    This is what I've though of so far:
    -The cards are saved in two 4x13 arrays, Deck1 and Deck2.
    -Random roll from 1 to 2 determines which deck.
    -Random roll from 1-4 determines which suit.
    -Random roll from 1-13 determines which card.
    -Then a function would determine the value of the value of the card depending on row number
    You should have 104 card objects. Put them into one list, in random order. When dealing, you take from the top of the deck (Just like real life).

    Come back when you have the code to do this.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    The first solution is to simulate real-world objects like suggested in the previous post: define a list structure with the appropriate functions (push, pop, etc...), then you would have one such list per player and one for the dealer (I guess, i don't know how to play blackjack). Initially you create the 104 nodes (one per card value) in the dealer list and then you unroll the game moving cards from one list to another. The problem is that it's going to be a bit tricky to save the game state.

    Another simpler way (which is what you suggested) is to have an array (or 2) of 104 items, holding each card value AND the card owner, a simple integer should be sufficient for the owner, like 0 for the dealer, something greater for a player. You don't have to "remove" a card, just mark who is the owner. Saving that should be much more simple (depending on how portable you want your saved file to be - write() and read() could be good enough(tm)).

    Before that, I suggest you prepare what functions you need (like deal() maybe) and try to write the main() to see where you're going (this is just a formal way to write the rules of that game). Once it's done you'll just have to fill in the blanks.
    Last edited by root4; 01-17-2012 at 05:06 AM.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    3
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void menu();
    void menuChoice(int choice);
    
    struct Player
    {
        char player_name[50];
        int player_type;
        double money;
        struct Player *next;
    }
    
    int main(void) {
        menu();
        printf("Please enter a choice:");
        system("cls");
        int choice;
        menuChoice(choice);
        return 0;
    }
    
    
    void menu()
    {
        printf("Please choose one of the following:\n");
        printf("Press 1 to start a new game.\n");
        printf("Press 2 to load an existing game.\n");
        printf("Press 3 to exit.\n");
    }
    
    void menuChoice(int choice)
    {
        scanf("%d", choice);
        if(choice == 1)
        {
            struct Player *root;
            root = malloc( sizeof(struct Player) );
            root->next = 0;
            int cpu_players;
            printf("Choose a desired name:");
            scanf("%s", &root->player_name);
            printf("Choose an amount of money to play with: $");
            scanf("%lf", &root->money);
            root->player_type = 0 ;
    
    
            printf("Please enter the number of CPU players: ");
            scanf_s("%d", &cpu_players);
            int i=0;
            for(i=0;i<cpu_players;i++)
            {
                struct Player player;
                player.player_type = 1;
                printf("Choose a name for CPU player %d: ", i+1);
                scanf("%s", player.player_name);
                printf("Choose amount of money for cpu player %d: ", i+1);
                scanf("%lf", &player.money);
            }
        }
        else if(choice == 2)
        {
    
        }
        else
        {
            exit(0);
        }
    }
    So guys this is what I've written so far, there's an error that I can't get my head around in the main function
    "16 C:\Dev-Cpp\main.c two or more data types in declaration of `main' "

    Help much appreciated

  5. #5
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Code:
    struct Player
    {
        char player_name[50];
        int player_type;
        double money;
        struct Player *next;
    }; //<-----
    I miss these semicolons all the time too.
    Consider this post signed

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    1
    although iam a fan of hungarian notation i left it out. Nevertheless i quick found something i would change, have a look

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void menu();
    void menuChoice(); /* no input value needed as the function itself will ask for a value */
    
    struct Player
    {
        char player_name[50];
        int player_type;
        double money;
        struct Player *next;
    }; /* non functions end with a ';' */
    
    int main() {
        menu();
        printf("Please enter a choice:");
        /*system("cls");     <-- will instantly clear the screen leaving you with no displayed menu text*/
        menuChoice();
        return 0;
    }
    
    
    void menu()
    {
        printf("Please choose one of the following:\n");
        printf("Press 1 to start a new game.\n");
        printf("Press 2 to load an existing game.\n");
        printf("Press 3 to exit.\n");
    }
    
    void menuChoice()
    {
        int choice = 0;
        scanf("%d", &choice);
        if(choice == 1)
        {
            struct Player *root;
            root = malloc( sizeof(struct Player) );
            root->next = 0;
            int cpu_players;
            printf("Choose a desired name:");
            scanf("%s", root->player_name);
            printf("Choose an amount of money to play with: $");
            scanf("%lf", &root->money);
            root->player_type = 0 ;
    
    
            printf("Please enter the number of CPU players: ");
            scanf("%d", &cpu_players);
            int i=0;
            for(i=0;i<cpu_players;i++)
            {
                struct Player player;
                player.player_type = 1;
                printf("Choose a name for CPU player %d: ", i+1);
                scanf("%s", player.player_name);
                printf("Choose amount of money for cpu player %d: ", i+1);
                scanf("%lf", &player.money);
            }
        }
        else if(choice == 2)
        {
    
        }
        else
        {
            exit(0);
        }
    }
    Last edited by philinux; 01-20-2012 at 05:06 AM.

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    3
    Hey guys, thanks for your help but I've decided to start from scratch because I got damn confused with the linked lists
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <math.h>
    
    // Inizializzazione di variabili
    int choice,
        playerNumber;
    // Prototipi di funzioni
    void menu();
    void menuChoice();
         
    //Inizializzazione strutture dati
    char deckOne[4][13],
         deckTwo[4][13];
    
    typedef struct players
    {
           char playerName[50];
           int playerType;
           double playerCash;
           struct players *next;
           struct players *head;
           struct players *temp;
           }players_t;
           
    // Funzione main
    int main(void){
        menu();
      
        return 0;
    }
    
    // Funzione menu
    void menu(){
        printf("Please choose one of the following:\n");
        printf("Press 1 to start a new game.\n");
        printf("Press 2 to load an existing game.\n");
        printf("Press 3 to exit.\n");
    };
    
    //Funzione selezione menu
    void menuChoice(){
         scanf("%d", choice);
         if(choice==1){
                       players_t p1;
                       players_t temp = &p1;
                       printf("Enter your desired name");
                       scanf("%s", &temp.playerName);
    The code above isn't working as it should, I think I'm getting confused with the points within the struct. *head is a pointer to the "top" node in the list, *temp is essentially a new node that will replace the address of the old *head.
    Am I doing this right, can someone point me in the right direction (no pun intended)
    Last edited by Karim Bissar; 01-20-2012 at 09:13 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. blackjack
    By gloworm in forum C Programming
    Replies: 6
    Last Post: 04-05-2010, 02:39 AM
  2. BlackJack
    By gsoft in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 01-04-2005, 12:09 PM
  3. Blackjack
    By The Brain in forum C++ Programming
    Replies: 67
    Last Post: 09-11-2004, 10:29 AM
  4. Help with BlackJack
    By Jontay in forum C++ Programming
    Replies: 12
    Last Post: 06-25-2003, 02:44 PM
  5. Blackjack!
    By Dr. Bebop in forum Game Programming
    Replies: 1
    Last Post: 10-03-2002, 08:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21