Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
enum Suit { SPADES = 0, HEARTS = 1, CLUBS = 2, DIAMONDS = 3};
enum Card_Value {TWO =2, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE};
typedef struct
{
enum Suit suit;
enum Card_Value card;
char card_code[52];
} Card;
typedef struct
{
char player_name[10];
Card hand[5];
}Hand;
void initalize_random_generator(void);
void shuffle_deck(Card *p_deck_index);
Card * deck(void);
char * get_card_code(int suit, int face_value);
void deal_cards(int player_num, int num_of_cards, int num_of_cards_in_hand, int num_cards_delt, Card *p_deck_index, Hand player[]);
int main()
{
int num_of_players;
int i, j, num_cards_delt = 0;
Card *p_deck;
initalize_random_generator();
p_deck = deck();
for (j = 0; j < 52; j++)
{
printf("%s ", p_deck[j].card_code);
}
printf("Enter the number of players: ");
scanf(" %d", &num_of_players);
// declare variable player array
Hand player[num_of_players];
for (i = 0; i < num_of_players; i++)
{
printf("Please enter player %d's name: ", i + 1);
scanf(" %s", player[i].player_name);
}
for (j = 0; j < 5; j++)
{
for (i = 0; i < num_of_players; i++)
{// <----- according to the debugger num_cards_delt is still 0 as set in the decleration
deal_cards(num_of_players, 1, j, num_cards_delt, p_deck, player);
printf("%s ", player[i].hand[j].card_code); //<------- when debugger is pointing here ie hasnt done the printf yet num_cards_dealt is -274638395743 or what ever
num_cards_delt++;
}
}
}
void initalize_random_generator(void)
{
srand((unsigned) time(NULL));
}
Card * deck(void)
{
static Card deck[52];
shuffle_deck(deck);
return deck;
}
void shuffle_deck(Card *p_deck_index)
{
int count_cards = 0, card_found = 0;
int i;
int suit_value, face_value;
char *temp_string;
while (count_cards < 52)
{
suit_value = rand() % 4;
face_value = rand() % 13 + 2;
for (i = 0; i < count_cards + 1; i++)
{
if (p_deck_index[i].suit == suit_value && p_deck_index[i].card == face_value)
{
card_found = 1;
break;
}
}
if (!card_found) //card_found is 0
{
/*
p_deck_index[count_cards].suit = suit_value;
p_deck_index[count_cards].card = face_value;
temp_string = get_card_code(suit_value, face_value);
strcpy(p_deck_index[count_cards].card_code, temp_string));
count_cards ++;
//*/
temp_string = get_card_code(suit_value, face_value);
p_deck_index[count_cards] = (Card) {suit_value, face_value};
strcpy(p_deck_index[count_cards++].card_code, temp_string);
}
card_found = 0;
}
for (i = 0; i < 52; i++)
{
printf("%s ", p_deck_index[i].card_code);
}
}
char * get_card_code(int suit, int face_value)
{
/*******************************************
* Thanks to flp1969 for this code. Suit: *
* goes from 0 spades, 1 hearts ,2 clubs, *
* and 3 is diamonds. Card: ace = 0, 2 = 1,*
* 3 = 2 ... 10 = 9, J = 10, Q = 11, K = 12*
*******************************************/
static char s[5] = { 0 };
int c = 0x1f0a1;
if (face_value == ACE)
{
face_value -= 14;
}
else
{
face_value -= 1;
}
switch ( suit )
{ //deliberate fall through
case 2: // clubs
c += 0x10;
case 3: // diamonds
c += 0x10;
case 1: // hearts
c += 0x10;
}
switch ( face_value )
{
case 0 ... 10:
c += face_value;
break;
default:
c += face_value + 1;
}
s[0] = 0xf0 | ((c >> 18) & 7);
s[1] = 0x80 | ((c >> 12) & 0x3f);
s[2] = 0x80 | ((c >> 6) & 0x3f);
s[3] = 0x80 | (c & 0x3f);
return s;
}
void deal_cards(int player_num, int num_of_cards, int num_of_cards_in_hand, int num_cards_delt, Card *p_deck_index, Hand player[])
{
int i;
for (i = 0 + num_of_cards_in_hand; i < num_of_cards + num_of_cards_in_hand; i++)
{
strcpy(player[player_num].hand[i].card_code, p_deck_index[num_cards_delt].card_code);
}
}