I'm working on a blackjack game in C. I have three functions, one to fill the deck, one for shuffling the cards and one for dealing the cards. My problem is that I don't know how to give my cards an integer value, I need that to see who wins. I would be very grateful for some input on how to solve this.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "func.h"
/*fill deck with 52 cards*/
void fillDeck(Card * const Deck, const char *suit[], const char *deck[]){
int s;
for (s = 0; s < 52; s++){
Deck[s].suits = deck[s % 13];
Deck[s].decks = suit[s / 13];
}
return;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "func.h"
/*shuffle cards*/
void shuffle(Card * const Deck){
int i, j;
Card temp;
for (i = 0; i < 52; i++){
j = rand() % 52;
temp = Deck[i];
Deck[i] = Deck[j];
Deck[j] = temp;
}
return;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "func.h"
/*deal cards*/
void deal(const Card * const Deck, int size, int size_1, int size_2){
int i, j, length;
char anotherCard[2];
char name1[30];
char name2[30];
printf("Name player one > ");
scanf("%s", name1);
printf("Name player two > ");
scanf("%s", name2);
printf("\nWelcome %s and %s, lets begin!\n\n", name1, name2);
getchar();
printf("%s's card:\n", name1);
for (i = 0; i < size; i++){
printf("%5s of %-8s%c", Deck[i].decks, Deck[i].suits, (i + 1) % 2 ? '\t' : '\n');
}
printf("\n%s's card:\n", name2);
for (i = 2; i < size_1; i++){
printf("%5s of %-8s%c", Deck[i].decks, Deck[i].suits, (i + 1) % 2 ? '\t' : '\n');
}
printf("\nDealer card:\n");
for (i = 4; i < size_2; i++){
printf("%5s of %-8s%c", Deck[i].decks, Deck[i].suits, (i + 1) % 2 ? '\t' : '\n');
}
return;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "func.h"
int main(void){
Card allCards[52];
const char *suits[] = { "spades", "hearts", "diamonds", "clubs" };
char *decks[] = { "ace", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king" };
int *values[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };
srand(time(NULL));
fillDeck(allCards, suits, decks, values);
shuffle(allCards);
deal(allCards, 2, 4, 6);
getchar();
return 0;
}
/*func.h*/
struct card{
const char *suits;
const char *decks;
};
typedef struct card Card;
void fillDeck(Card * const Deck, char *suit[], char *deck[]);
void shuffle(Card * const Deck);
void deal(const Card * const Deck, int size, int size_1, int size_2);
#endif
I would recommend that you add a field to your card struct to hold the value.
typedef struct card {
const char *suits;
const char *decks;
int value;
}
To assign the card the value you would use:
void fillDeck(Card * const Deck, const char *suit[], const char *deck[], const int *value[])
{
int s;
for (s = 0; s < 52; s++){
Deck[s].suits = suit[s / 13];
Deck[s].decks = deck[s % 13];
Deck[s].value = value[s % 13];
}
}
Then when you are determining a winner you can simply look at all the cards in their hand and sum up the values.