Radith said:
Hi All,
I have got this task which I cant get my head around:
It asks how I would store information:
I just want to get an opinion from the group:
So here's the question:
"
You have been asked to write a program for a card playing friend. How
would you store information about playing cards? For example how would
you store the fact that a certain card is the 7 of diamonds? (You may
use more than one variable if you wish)
/* BEGIN shuffle.c */
#include <stdio.h>
#include <time.h>
#define LU_RAND_SEED 123456789LU
#define LU_RAND(S) ((S) * 69069 + 362437 & 0xffffffffLU)
#define SUITS (sizeof suit / sizeof *suit)
#define RANKS (sizeof rank / sizeof *rank)
#define CARDS (SUITS * RANKS)
#define HAND 5
#define DEALS 5
struct poker {
int suit;
int rank;
};
long unsigned shuffle(int *, int, long unsigned);
int compar_rank(void const*, void const*);
int compar_suit(void const*, void const*);
int straight(struct poker *);
int pair(struct poker *);
int flush(struct poker *);
int three(struct poker *);
int four(struct poker *);
int full(struct poker *);
int two_pair(struct poker *);
void s_sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
int main(void)
{
size_t card;
struct poker hand[HAND];
long unsigned deal;
long unsigned seed = LU_RAND_SEED;
char *suit[] = {"Hearts","Diamonds","Clubs","Spades"};
char *rank[] = {"Deuce","Three","Four","Five","Six",
"Seven","Eight","Nine","Ten","Jack","Queen","King","Ace"
};
int deck[CARDS];
putchar('\n');
/**/
seed = (long unsigned)time(0);
/*//**/
deal = DEALS;
while (deal-- != 0) {
seed = shuffle(deck, CARDS, seed);
for (card = 0; card != HAND; ++card) {
hand[card].suit = deck[card] % SUITS;
hand[card].rank = deck[card] % RANKS;
}
if (pair(hand)) {
if (three(hand)) {
if (four(hand)) {
puts("Four of a Kind:");
} else {
if (full(hand)) {
puts("Full House:");
} else {
puts("Three of a Kind:");
}
}
} else {
if (two_pair(hand)) {
puts("Two Pair:");
} else {
puts("Pair:");
}
}
} else {
switch (2 * flush(hand) + straight(hand)) {
case 0:
s_sort(hand, HAND, sizeof *hand, compar_rank);
printf("%s High:\n", rank[hand[4].rank]);
break;
case 1:
puts("Straight:");
break;
case 2:
puts("Flush:");
break;
default:
puts("Straight Flush:");
break;
}
}
putchar('\n');
for (card = 0; card != HAND; ++card) {
printf("%s of %s\n",
rank[deck[card] % RANKS],
suit[deck[card] % SUITS]);
}
putchar('\n');
}
return 0;
}
int pair(struct poker *hand)
{
s_sort(hand, HAND, sizeof *hand, compar_rank);
return hand[1].rank == hand[0].rank
|| hand[2].rank == hand[1].rank
|| hand[3].rank == hand[2].rank
|| hand[4].rank == hand[3].rank;
}
int three(struct poker *hand)
{
s_sort(hand, HAND, sizeof *hand, compar_rank);
return hand[0].rank == hand[2].rank
|| hand[1].rank == hand[3].rank
|| hand[2].rank == hand[4].rank;
}
int four(struct poker *hand)
{
s_sort(hand, HAND, sizeof *hand, compar_rank);
return hand[0].rank == hand[3].rank
|| hand[1].rank == hand[4].rank;
}
int full(struct poker *hand)
{
s_sort(hand, HAND, sizeof *hand, compar_rank);
return hand[1].rank == hand[0].rank
&& hand[4].rank == hand[3].rank
&&(hand[2].rank == hand[1].rank
|| hand[3].rank == hand[2].rank);
}
int two_pair(struct poker *hand)
{
s_sort(hand, HAND, sizeof *hand, compar_rank);
return hand[1].rank == hand[0].rank
&& hand[2].rank == hand[3].rank
|| hand[1].rank == hand[0].rank
&& hand[4].rank == hand[3].rank
|| hand[1].rank == hand[2].rank
&& hand[4].rank == hand[3].rank;
}
int straight(struct poker *hand)
{
s_sort(hand, HAND, sizeof *hand, compar_rank);
return hand[4].rank == hand[3].rank + 1
&& hand[3].rank == hand[2].rank + 1
&& hand[2].rank == hand[1].rank + 1
&&(hand[1].rank == hand[0].rank + 1
|| hand[1].rank == 0 && hand[0].rank == 12);
}
int flush(struct poker *hand)
{
s_sort(hand, HAND, sizeof *hand, compar_suit);
return hand[0].suit == hand[4].suit;
}
int compar_rank(void const *first, void const *second)
{
int int_1 = (*(struct poker*)first).rank;
int int_2 = (*(struct poker*)second).rank;
return int_2 > int_1 ? -1 : int_2 != int_1;
}
int compar_suit(void const *first, void const *second)
{
int int_1 = (*(struct poker*)first).suit;
int int_2 = (*(struct poker*)second).suit;
return int_2 > int_1 ? -1 : int_2 != int_1;
}
long unsigned shuffle(int *array, int n, long unsigned seed)
{
int i, r;
array[0] = 0;
for (i = 1; n > i; ++i) {
seed = LU_RAND(seed);
r = seed % (i + 1);
array
= 0;
array = array[r];
array[r] = i;
}
return seed;
}
void s_sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
size_t bytes;
unsigned char *array, *after, *i, *j, *k, *p1, *p2, *end, swap;
array = base;
after = nmemb * size + array;
if (nmemb > (size_t)-1 / 3 - 1) {
nmemb = nmemb / 3 - 1;
} else {
nmemb = (nmemb * 3 + 1) / 7;
}
while (nmemb != 0) {
bytes = nmemb * size;
i = bytes + array;
do {
j = i - bytes;
if (compar(j, i) > 0) {
k = i;
do {
p1 = j;
p2 = k;
end = p2 + size;
do {
swap = *p1;
*p1++ = *p2;
*p2++ = swap;
} while (p2 != end);
if (bytes + array > j) {
break;
}
k = j;
j -= bytes;
} while (compar(j, k) > 0);
}
i += size;
} while (i != after);
nmemb = (nmemb * 3 + 1) / 7;
}
}
/* END shuffle.c */