Code:
#ifndef CARDGAMES_H
#define CARDGAMES_H
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <time.h>
typedef struct {
char suit;
char label;
int value;
} card ;
//function prototypes
void shuffleCards(card deck[], int deckLength);
void cardString(card c, char * lbl);
bool drawCard(card deck[], card * returnCard, int * deckLength);
int combineDecks(card deckToHold[], int * holdlength, card deckToTake[], int * takelength);
void addToDeck(card deck[], card toAdd, int * deckLength);
void printDeck(card deck[], int deckLength);
int containsCard(card hand[], int nCards, card find);
int containsMatch(card hand[], int nCards, card find);
int compareCards(card c1, card c2);
card removeCard(card deck[], int * deckLength, int index);
bool isRed(card c);
bool isBlack(card c);
function: addToDec
parameters:
card deck[]: array of cards representing the deck
card toAdd: the card to add to the deck
int * deckLength: the number of cards currently held in the deck
Adds the card toAdd to the end of deck (at position deckLength), and increments deckLength
to reflect the added card.
*/
void addToDeck(card deck[], card toAdd, int * deckLength)
{
int i;
for(i=0; i < deckLength; i++)
{
sprintf(toAdd, "%s\n", deck[i]);
}
}
/*
void printDeck
parameters:
card deck[]: array of cards to print out
int deckLength: number of cards in the deck
This function uses the cardString function to print out each
card on the deck, with each card appearing on a new line.
for each card in the deck
get the string of that card using cardString
print it out
print a newline
*/
void printDeck(card deck[], int deckLength)
{
int i;
for(i=0; i < deckLength; i++)
{
char lbl[100];
cardString(deck[i], lbl);
printf("%s\n", lbl);
}
}
/*
function: containsCard
parameters:
card hand[]: array that represents a hand of cards
int nCards: the number of cards in a hand
card find: the card we're trying to find in that hand
returns:
integer. if the card is in the hand, the array index that contains that card
if the card is not in the hand, -1
Looks through the entire array and uses the compareCards function to determine
whether the hand contains that card. If it does contain that card, returns the index
where that card is. If the card is not in the hand, returns -1.
for each card in the hand
if compareCards(the find card, the card in the hand) == 0
return that card's index
if we didn't find the card return -1
*/
int containsCard(card hand[], int nCards, card find){
}
/*
function: containsMatch
parameters:
card hand[]: array that represents a hand of cards
int nCards: the number of cards in a hand
card find: the card we're trying to find the match to in that hand, where a match
is a card of the same value and same color suit, but not same suit (for example, king of hearts
matches to king of diamonds; ten of spades matches to ten of clubs, etc).
returns:
integer. if the card is in the hand, the array index that contains that card
if the card is not in the hand, -1
Determine which suit we're looking for:
if find.suit is diamonds, we're looking for hearts
hearts diamonds
clubs spades
spades clubs
for each card in the hand:
if the value of that card is equal to the value of the find card
if the suit of that card is equal to the suit we're looking for
return the index of that card
if we didn't find the card, return -1.
*/
int containsMatch(card hand[], int nCards, card find){
}
/*
function: compareCards
parameters:
card c1: a card
card c2: a card
returns: 0 if card 1 has the same numerical value as card 2
the difference of c2 - c1's values if they are not equal
(so a positive value if c1 < c2 and a negative value if c2 > c1)
*/
int compareCards(card c1, card c2)
{
return c2.value - c1.value;
}
/*
function: removeCard
parameters:
card deck[]: deck of cards to remove the card at the given index from
int * deckLength: number of cards in the deck
int index: index of the card to remove
returns: the removed card
Removes the card at index by doing the following:
swap the card at index with the card at the end of the deck
declare a new card, and set it equal to the card at the end of the deck
Decrement decklength to indicate that that card is gone
return the new card you declared and set equal to the end of the deck
*/
card removeCard(card deck[], int * deckLength, int index){
}
/*
function: isRed
parameters:
card c: a card
returns: true if the card is red (has suit diamond or heart)
*/
bool isRed(card c)
{
if(c.suit == 'D' || c.suit == 'H')
{
return 1;
}
else
{
return 0;
}
}
/*
function: isBlack
parameters:
card c: a card
returns: true if the card is black (has suit spade or club)
*/
bool isBlack(card c){
if(c.suit == 'S' || c.suit == 'C')
{
return 1;
}
else
{
return 0;
}
}
//////////////////////////////////////////
// extra credit options //
//////////////////////////////////////////
/*
function: sortCards
parameters:
card deck[]: array of cards
int decklength: number of cards in the deck[] array
This function should sort the array of cards in the following order:
by suits: clubs, diamonds, hearts, spades (alphabetic)
within each suit: 2 3 4 5 6 7 8 9 T J Q K A (value of T = 10, J=11, value of Q=12, value of K=13, value of A = 14)
*/
void sortCards(card deck[], int deckLength){
}
/*
function: loadDeck
parameters:
card deck[]: array that the deck should be loaded into
string filename: file name that contains the deck information.
the file should be in the format
suit label value
suit label value
...
one line for each card.
for example:
H A 13
H 2 2
H 3 3
for ace, 2, and 3 of hearts.
There are an undetermined number of cards in the file (maybe > 52).
You will need to support any number of cards.
You can do this through dynamic memory allocation or other methods.
return: the number of cards loaded into the deck.
*/
int loadDeck(card deck[], char * filename){
}
//////////////////////////////////
// provided functions //
//////////////////////////////////
/*
function: loadDefaultDeck
parameters:
card deck[]: array that the deck should be loaded into
Loads the standard deck into the deck array.
return: the number of cards loaded into the deck.
*/
int loadDefaultDeck(card deck[]){
int suit;
int value;
int ctr = 0;
for (suit = 0; suit < 4; suit++){
for (value = 2; value < 15; value++){
switch (suit){
case 0:
deck[ctr].suit = 'D';
break;
case 1:
deck[ctr].suit = 'H';
break;
case 2:
deck[ctr].suit = 'S';
break;
case 3:
deck[ctr].suit = 'C';
break;
}
deck[ctr].value = value;
switch (value){
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
deck[ctr].label = value + 48;
break;
case 10:
deck[ctr].label = 'T';
break;
case 11:
deck[ctr].label = 'J';
break;
case 12:
deck[ctr].label = 'Q';
break;
case 13:
deck[ctr].label = 'K';
break;
case 14:
deck[ctr].label = 'A';
break;
}
ctr++;
}
}
return ctr;
}
/*
function: cardString
parameters:
card c: the card to print the name of
char * lbl: an already-allocated character array
This function should return the string name of the card passed in in the
parameter string lbl.
For example, "queen of hearts", "ace of spades", etc.
*/
void cardString(card c, char * lbl){
switch (c.label){
case '2':
strcpy(lbl, "two of ");
break;
case '3':
strcpy(lbl, "three of ");
break;
case '4':
strcpy(lbl, "four of ");
break;
case '5':
strcpy(lbl, "five of ");
break;
case '6':
strcpy(lbl, "six of ");
break;
case '7':
strcpy(lbl, "seven of ");
break;
case '8':
strcpy(lbl, "eight of ");
break;
case '9':
strcpy(lbl, "nine of ");
break;
case 'T':
strcpy(lbl, "ten of ");
break;
case 'J':
strcpy(lbl, "Jack of ");
break;
case 'Q':
strcpy(lbl, "Queen of ");
break;
case 'K':
strcpy(lbl, "King of ");
break;
case 'A':
strcpy(lbl, "Ace of ");
break;
}
switch (c.suit){
case 'D':
strcat(lbl, "diamonds");
break;
case 'H':
strcat(lbl, "hearts");
break;
case 'C':
strcat(lbl, "clubs");
break;
case 'S':
strcat(lbl, "spades");
break;
}
}
#endif
I took out some of the functions I haven't gotten to for easier to read.