# Blackjack in C [Need help]

• 01-14-2012
Karim Bissar
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
• 01-16-2012
NeonBlack
Quote:

Originally Posted by Karim Bissar
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.
• 01-17-2012
root4
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.
• 01-19-2012
Karim Bissar
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
• 01-19-2012
bernt
Code:

```struct Player {     char player_name[50];     int player_type;     double money;     struct Player *next; }; //<-----```
I miss these semicolons all the time too.
• 01-20-2012
philinux
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);     } }```
• 01-20-2012
Karim Bissar
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)