# Thread: Blackjack in C [Need help]

1. ## 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. 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.

3. 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.

4. Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Player
{
char player_name[50];
int player_type;
double money;
struct Player *next;
}

int main(void) {
system("cls");
int choice;
return 0;
}

{
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");
}

{
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. Code:
```struct Player
{
char player_name[50];
int player_type;
double money;
struct Player *next;
}; //<-----```
I miss these semicolons all the time too.

6. 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 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() {
/*system("cls");     <-- will instantly clear the screen leaving you with no displayed menu text*/
return 0;
}

{
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");
}

{
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);
}
}```

7. 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

//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 *temp;
}players_t;

// Funzione main
int main(void){

return 0;
}

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");
};