C Language Finding a Pair - Searching an array for column (vs row) match

Hi

(Background to the problem)
I am trying to find a pair using C language. The objective is to find a pair of cards and change the printHand function to a findPair function.

The rows are the suits and are initialised to suit names (i.e. ‘Clubs’ etc.)
The columns are the ‘face’ values and are initalised to card values (i.e. ‘Deuce’)

My ‘printHand’ array consists of the deal ‘hand’ plus zeros for the rest of the slots and is what I am thinking I should manipulate to a ‘find Pair’ function.

(Question 1)
Where I am lost is that I want to search an array that is filled with blank spaces (52 ‘slots’) of which 5 have been filled with card ‘values’ (printHand)

I am not sure how to compare the two values when I have to increment my search value to an unknown number of spaces until it reaches a non-zero figure.

If the array were filled only with values, I could simply search and be done with it. But, first I need to filter out only the non zero values and then perform my comparison.

(Second question)
Secondly, I am not sure how to compare two arrays based only on the column. For example, I understand if(array[y] == array2[y]), however if I want to search through the arrays varying both values until I can find a match ‘only’ on the ‘y’ value of the array.

Here I am presuming that the ‘y’ value is the ‘face’ value (i.e. Jack) and is what I am trying to find a pair of.

(Summary)
Here my printHand function really is just a precursor to a ‘print pair’ function and is what I am hoping to vary to get my output.

Hope that makes sense!

And thanks for any tips or suggestions.

Karen

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

#define SUITS 4
#define FACES 13
#define CARDS 52
#define HAND 5

//prototypes
void shuffle(unsigned int wDeck[][FACES]);
void deal(unsigned int wDeck[][FACES], const char *wFace[], const char *wSuit[], unsigned int wHand[][FACES] );
void printHand(unsigned int wHand[][FACES], const char *wFace[], const char *wSuit[]);

int main(void)
{
	//initialise deck array
	unsigned int deck[SUITS][FACES] = {0};

	//initialise hand array
	unsigned int hand[SUITS][FACES] = {0};

     //seed the randomiser
	 srand(time(NULL));

	 //shuffle the deck
	 shuffle(deck);

	 //initialise suit array
	 const char *suit[SUITS] = {"Hearts", "Diamonds", "Clubs", "Spades"};

	 //initialise face array
	 const char *face[FACES] = {"Ace", "Decue", "Three", "Four",
	                            "Five", "Six", "Seven", "Eight",
								"Nine", "Ten", "Jack", "Queen", "King"};

	deal(deck, face,suit,hand); // deal a hand

	printHand(hand, face, suit);
} // end Main

//shuffle cards in deck
void shuffle(unsigned int wDeck[][FACES]) {
	for (size_t card = 1; card <=CARDS; ++card) {
		size_t row;
		size_t column;

		//choose 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], const char *wFace[], const char *wSuit[], unsigned int wHand[][FACES] ) {
	//deal the cards
	for (size_t card = 1; card <= HAND; ++card) {
		//loop through rows of wDeck
		for (size_t row = 0; row < SUITS; ++row) {
			//loop through columsn of wDeck for current row
			for (size_t column = 0; column < FACES; ++column) {
				//if slot contiains current card, display card
				if (wDeck[row][column] == card) {
					printf("%5s of %-8s\n", wFace[column], wSuit[row]);
					wHand[row][column] = card;
				}
			}
		}
	}
	puts("\n");
}

void printHand(unsigned int wHand[][FACES], const char *wFace[], const char *wSuit[]) {
	for (size_t row = 0; row < SUITS; ++row) {
		for (size_t column = 0; column < FACES; ++column) {
			if (wHand[row][column] != 0) printf("%5s of %-8s\n", wFace[column], wSuit[row]);
		}
	}
}


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.