// cards.cpp : Defines the entry point for the console application.
//

#include <stdlib.h>
#include <stdio.h>
#include <time.h>


/* Structure to hold information about playing cards */
typedef struct {
    int suit;   /* The suit of the playing card */
    int value;  /* The value of the playing card */
} CARD;

/* Set up an integer to represent each suit */
#define CLUBS 4
#define DIAMONDS 3
#define HEARTS 2 
#define SPADES 1
/* Set up integers to represent non numeric cards */
#define ACE 1
#define KING 13
#define QUEEN 12
#define JACK 11

const int DECK_SIZE= 52;   //#cards in deck
const int NO_SHUFFLES= 1000; // #times to shuffle pack

void print_card(CARD);
/* Print out a deck of cards */

void shuffle_deck (CARD []);
/* Shuffle a deck of 52 cards*/

void print_hand (CARD *, int);
/* Print out a hand of cards */

CARD *deal_hand (CARD [], int, int);
/* Deal a hand of cards */

int main(int argc, char *argv[])
{
	CARD deck [DECK_SIZE];  /* Array to hold a deck of cards */
	CARD *north, *east, *south, *west; /* Various hands of cards */
	int i,j;
	int card_no= 0;			/* Counter tracks the card we are setting */

	srand(time(NULL));   /* Seed the random numbers */
	for (j= 1; j <= 4; j++) {
		for (i= 1; i <= 13; i++) {
			deck[card_no].suit=j;
                        deck[card_no].value=i;
			card_no++;
		}
	}
	shuffle_deck(deck);
	north= deal_hand(deck, 52,13);
	printf("North: ");
	print_hand(north,13);
    east= deal_hand(deck, 52-13,13);
	printf("East: ");
	print_hand(east,13);
	south= deal_hand(deck, 52-26,13);
	printf("South: ");
	print_hand(north,13);
	west= deal_hand(deck, 13,13);
	printf("West: ");
	print_hand(west,13);
 	free(north);
	free(east);
	free(south);
	free(west);	
	return 0;
}

void print_card(CARD my_card)
/* Prints out a single playing card */
{
    switch (my_card.value) {
	case(ACE):
		putchar('A');
		break;
	case(KING):
		putchar('K');
		break;
	case(QUEEN):
		putchar('Q');
		break;
	case(JACK):
		putchar('J');
		break;
	case(10):
		putchar('T');
		break;
	default:
		printf("%d",my_card.value);
	}
	switch (my_card.suit) {
	case(HEARTS):
		putchar('H');
		break;
	case(SPADES):
		putchar('S');
		break;
	case(DIAMONDS):
		putchar('D');
		break;
	case(CLUBS):
		putchar('C');
		break;
	default:
		fprintf (stderr,"Illegal suit\n");
	}

}

void shuffle_deck(CARD deck[])
/* Shuffle NO_SHUFFLES times */
{
	int i;
	CARD tmp;
	int card1, card2;
	for (i= 0; i < NO_SHUFFLES; i++) {
		card1= (rand()%DECK_SIZE);  /* Get two rand nos between 0 and 51 */
		card2= (rand()%DECK_SIZE);
		tmp= deck[card1];
		deck[card1]= deck[card2];
		deck[card2]= tmp;
	}
}

void print_hand (CARD *deckptr, int no_cards)
/* Print out a hand of no_cards */
{
	int i;
	for (i= 0; i < no_cards; i++) {
		print_card(deckptr[i]);
		putchar(' ');
	}
	printf  ("\n");
}

CARD *deal_hand (CARD deck[], int no_cards, int no_hand)
/* Return a pointer to allocated memory containing the hand of no_hand 
cards dealt from the back of a pack of no_cards cards */
{
	int i;
	CARD *hand;  /* Hand of cards we are dealt */
	if (no_hand > no_cards) {
		fprintf (stderr, "Not enough cards to deal!\n");
		exit (-1);
	}
	hand= (CARD *)malloc(no_hand *sizeof(CARD));
	if (hand == NULL) {
		fprintf (stderr, "Out of memory!\n");
		exit(-1);
	}
	for (i= 0; i < no_hand; i++) {
		hand[i]= deck[no_cards -1 -i];
	}
	return hand;

	
}

