I'm supposed to code a program for a Euchre card game. The code was from the book, my teacher wanted us to do the following:
- Modify card_shuffle_deal.c to only contain cards with face values of 9, 10, Jack, Queen, King, and Ace. The suits will stay the same (Hearts, Diamonds, Clubs, and Spades).
- Modify card_shuffle_deal.c to deal 5 cards instead of 52 cards. More specifically: after the cards are shuffled, deal 5 cards and print only those 5 onto the screen.
- We’re going to call those 5 cards “the hand.” Write a function that uses pointers to determine if the hand contains a Jack.
I already completed the first two objectives, and I think I got the "the hand" part. But the problem I am having with my program is that it does in fact work, however it is giving the wrong output. Instead of saying "A Jack has been found!" it says "A Ace has been found!" and everytime I run it it will say that even if that card is not present.
The only stuff I added to the code was:
the_hand(hand, face, suit)
void the_hand()
unsigned int hand[5][2]
And the stuff in the void the_hand() part on the bottom. Anyway here is my code, I tried Googling as much as I could, but really can't find much. I did find some poker games made in C and used some of the code as a reference. What am I doing wrong that I'm not getting the result(s) I'm looking for? Any tips in the right direction would be appreciated.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stddef.h>
#define SUITS 4
#define FACES 6
#define CARDS 24
#define THEE_HAND 5
// prototypes
void shuffle(unsigned int wDeck[][FACES]); // shuffling modifies wDeck
void deal(unsigned int wDeck[][FACES], unsigned int hand[][2], const char *wFace[], const char *wSuit[]);
void the_hand(unsigned int hand[][2], const char *wFace[], const char *wSuit[]) ;
//~ void jack(const char *wFace[3], const char *wSuit[]) ;
//~ void jack_ace(unsigned int wHands[][FACES], unsigned int hand[][2],
//~ const char *wFace[], const char *wSuit[]) ;
int main(void) {
// initialize deck array
unsigned int deck[SUITS][FACES] = {0};
unsigned int hand[5][2];
srand(time(NULL)); // seed the RNG
shuffle(deck); // shuffle the deck
// initialize suit array
const char *suit[SUITS] = {"Hearts", "Diamonds", "Clubs", "Spades"};
// initialize face array
const char *face[FACES] = {"Ace", "Nine", "Ten","Jack", "Queen", "King"};
deal(deck, hand, face, suit); // deal the deck
the_hand(hand, face, suit);
//~ jack_ace(hand, deck, face, suit);
}
// shuffle cards in deck
void shuffle(unsigned int wDeck[][FACES]) {
// for each of the cards, choose slot of deck randomly
for (size_t card = 20; card <= CARDS; card++) {
size_t row;
size_t column;
// choose new random location until unoccupied slot found
do {
row = rand() % SUITS;
column = rand() % FACES;
} while (wDeck[row][column] != 0);
// place card number in chosen slot of deck
wDeck[row][column] = card;
}
}
// deal cards in deck
void deal(unsigned int wDeck[][FACES], unsigned int hand[][2], const char *wFace[], const char *wSuit[]) {
// deal each of the cards
for (size_t card = 20; card <= CARDS; card++) {
// loop through rows of wDeck
for (size_t row = 0; row < SUITS; row++) {
// loop through columns of wDeck for current row
for (size_t column = 0; column < FACES; column++) {
// if slot contains current card, display card
// end if
if (wDeck[row][column] == card) {
printf("%5s of %-8s%c",
wFace[column], wSuit[row],
card % 2 == 0 ? '\n' : '\t');
} // end if
} // end column for loop
} // end row for lop
} // end card for loop
} // end deal function
void the_hand(unsigned int hand[][2], const char *wFace[], const char *wSuit[]) {
unsigned int jack[FACES] = {0};
int hold;
for(size_t k = 5; k <= THEE_HAND; k++){
for(size_t i = 0; i < FACES; i++){
hold = (hand[i][1]) %5;
jack[hold] = jack[hold]+1;
for(size_t j = 0; j < CARDS; j++){
if(jack[j] != 0 && jack[j] == 3){
printf("A %4s has been found!\n", wFace[j]);
}
}
}
}
}