I'm supposed to make a euchre program in C. However, when I try running it the output is the following:
Nine of Hearts
Ten of Hearts
Queen of Hearts
Ace of Hearts
Jack of Hearts
It does change what the face is, but it's always X of Hearts. I think it might be #CARDS 5.
If I increase #CARDS with another number here is the output:
#CARDS 10 outputs both Hearts and Diamonds
#CARDS 15 outputs Hearts, Diamonds, and Clubs
#CARDS 20 outputs Hearts, Diamonds, Clubs, and Spades
Is there a way I can fix this to allow the suits to be randomized with #CARDS 5?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define CARDS 5
#define FACES 6
// *******************begin replacement section*****************************
// card structure definition
struct card {
const char *face;
const char *suit;
};
typedef struct card Card;
// *******************end replacement section*******************************
// Can also use the following code instead of begin/end replacement section
// typedef struct card {
// const char *face;
// const char *suit;
// } Card;
// *************************************************************************
// prototypes
void fillDeck(Card * const wDeck, const char *wFace[], const char *wSuit[]);
void shuffle(Card * const wDeck);
void deal(const Card * const wDeck);
int main(void) {
Card deck[CARDS]; // define array of Cards
// initialize array of pointers
const char *face[] = {"Ace", "Nine", "Ten", "Jack", "Queen", "King"};
// initialize array of pointers
const char *suit[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
srand(time(NULL)); // seed RNG
fillDeck(deck, face, suit); // load the deck with Cards
shuffle(deck); // put Cards in random order
deal(deck); // deal all 52 cards
}
// place strings into Card structures
void fillDeck(Card * const wDeck, const char *wFace[], const char *wSuit[])
{
// loop through wDeck
for (size_t i=0; i<CARDS; i++) {
// wDeck indexing works like a normal array
// Use . operator to access members of struct for
// specific array elements
wDeck[i].face = wFace[i % FACES];
wDeck[i].suit = wSuit[i / FACES];
}
} // end fillDeck function
// shuffle cards
void shuffle(Card * const wDeck) {
// loop through wDeck randomly swapping Cards
for (size_t i=0; i<CARDS; i++) {
size_t j = rand() % CARDS;
Card temp = wDeck[i];
wDeck[i] = wDeck[j];
wDeck[j] = temp;
}
} // end shuffle function
// deal cards
void deal(const Card * const wDeck) {
// loop through wDeck
for (size_t i=0; i<CARDS; i++) {
printf("%5s of %-8s%s\n", wDeck[i].face, wDeck[i].suit,
(i+1) % 4 ? " " : "\n");
}
} // end deal function