Code:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define DECK_SIZE 52
#define NFACES 13
#define NSUITS 4
#define NCARDS 13
/*enum for card suits*/
enum suits_e{
SUIT_DIAMONDS,
SUIT_CLUBS,
SUIT_HEARTS,
SUIT_SPADES
};
/*card structure definition*/
typedef struct Card{
int face; /* define face */
int suit; /* define suit */
}Card, *CardPtr;/* end structure card */
/*function prototypes*/
void createDeck(CardPtr deck);
void printCard(CardPtr card);
void shuffle(CardPtr deck);
void shareCards(CardPtr deck, CardPtr Hand1, CardPtr Hand2, CardPtr Hand3, CardPtr Hand4);
void findHighest(CardPtr Hand);
int main(){
Card deck[DECK_SIZE]; /*Creating deck array*/
/*Creating hand arrays*/
Card Hand1[NCARDS]; /*President Hand*/
Card Hand2[NCARDS]; /*Prime Minister Hand*/
Card Hand3[NCARDS]; /*Minister Hand*/
Card Hand4[NCARDS]; /*Slave Hand*/
/*contains the head pointer for each hand
CardPtr hands[4];
hands[0] = CardPtr Hand1;
hands[1] = CardPtr Hand2;
hands[2] = CardPtr Hand3;
hands[3] = CardPtr Hand4;
*/
int i,j; /*counter*/
int x = 1;
int y=1;
createDeck(deck);
shuffle(deck);
/*printf("Deck now: \n");
for(i=0; i<DECK_SIZE; i++){
printf("%d). ", y);
printCard(&deck[i]);
y++;
}
*/
int a=1;
printf("PRESIDENT-Player 1 hand: \n");
shareCards(deck, Hand1, Hand2, Hand3, Hand4);
for(i=0; i<NCARDS; i++){
printf("[%d]. ", a);
printCard(&Hand1[i]);
a++;
}
printf("\n");
Card temp;
findHighest(Hand1);
int numCards = 0; /*To keep track of how many cards are played in one turn*/
printf("How many cards would you like to play: ");
scanf("%d",&numCards);
int num[numCards]; /*array to hold cards*/
for(i=0;i<numCards;i++){
printf("Enter the number index to the left for each card: ");
scanf("%d", &num[i]);
}
return 0;
}
/*Creates deck*/
void createDeck(CardPtr deck){
int face, suit;
int i=0; /*counter*/
for (suit=SUIT_DIAMONDS; suit<NSUITS; suit++){
for (face=0; face<NFACES; face++){
deck[i].face= face;
deck[i].suit= suit;
i++;
}
}
}
/*Prints a Card*/
void printCard(CardPtr card){
/*Face array*/
const char *face[NFACES] = {
"Deuce", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"
};
/*Suit array*/
const char *suit[NSUITS] = {
"Diamonds", "Clubs", "Hearts", "Spades"
};
printf("%s of %s\n", face[card->face], suit[card->suit]);
}
/*Shuffles deck*/
void shuffle(CardPtr deck){
int i, index;
Card temp;
srand(time(NULL)); // randomize seed with time
for(i=DECK_SIZE-1; i>0; i--){
index = rand()% (i + 1);
temp = deck[index];
deck[index]= deck[i];
deck[i]= temp;
}
}
/*Share cards*/
void shareCards(CardPtr deck, CardPtr Hand1, CardPtr Hand2, CardPtr Hand3, CardPtr Hand4){
int i=0;
int j=0;
int k=0;
int l=0; /*counters*/
for (i=0;i<DECK_SIZE;i++){
if (i<13){
Hand1[i] = deck[i];
}
else if (i<26){
Hand2[j] = deck[i];
j++;
}
else if (i<39){
Hand3[k] = deck[i];
k++;
}
else if (i<DECK_SIZE){
Hand4[l] = deck[i];
l++;
}
} /*end for*/
}
/*Find Card with highest value*/
void findHighest(CardPtr Hand){
int max=0; /*holds highest number*/
int i=0; /*counter*/
Card temp;
CardPtr location;
for (i=0;i<NCARDS;i++){
if (max<Hand[i].face){
max = Hand[i].face;
printf("Max is: %d\n");
temp = Hand[i];
location = Hand[i];
}
}
for (i=0;i<NCARDS;i++){
printf("%d\n",Hand[i].face);
}
printf("Hand1 highest value is: %d\n The location is %d,", temp , location);
return;
}