Reputation: 345
I have a small application that I am creating using GCC in Ubuntu 10.04. I have a header file and a source file.
My header file:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DECKSZ 52
#define HAND_SIZE 5
#define STACKMAX 52
#define EMPTY -1
#define FULL (STACKMAX-1)
typedef enum boolean {false, true} boolean;
typedef struct card {
enum pip {ACE=1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING} pips;
enum suit {SPADES, CLUBS, HEARTS, DIAMONDS} suits;
char cardName[20];
} card;
typedef struct stack {
card s[STACKMAX];
int top;
} stack;
extern card deck[];
void initDeck(card[]);
void labelCards(card[]);
void shuffleDeck(card[]);
boolean dealHand(card[], stack*);
void displayHand(card*);
void arrangeHand(card*);
void swap(card*, card*);
boolean isFlush(card[]);
boolean isStraight(card[]);
boolean isXOfAKind(card[], int, enum pip);
boolean isStraightFlush(card[]);
boolean isFullHouse(card[]);
boolean isTwoPair(card[]);
boolean isEmpty(stack*);
boolean isFull(stack*);
void push(card*, stack*);
card pop(stack*);
void reset(stack*);
My source file:
#include "Poker.h"
int main(void) {
int i;
int flushCount = 0;
int straightCount = 0;
int xOfAKindCount = 0;
int straightFlushCount = 0;
int fullHouseCount = 0;
int isTwoPairCount = 0;
stack *stkDeck = stack;
stack *stkHand = stack;
card deck[DECKSZ] = {0};
initDeck(deck);
labelCards(deck);
for (i = 0; i < DECKSZ; i++) {
push(&deck[i], stkDeck);
}
/*do {*/
flushCount = 0;
straightCount = 0;
xOfAKindCount = 0;
straightFlushCount = 0;
fullHouseCount = 0;
isTwoPairCount = 0;
shuffleDeck(deck);
displayHand(deck);
arrangeHand(&deck[0]);
flushCount = isFlush(&deck[0]);
straightCount = isStraight(&deck[0]);
xOfAKindCount = isXOfAKind(&deck[0], 2, 0);
straightFlushCount = isStraightFlush(&deck[0]);
fullHouseCount = isFullHouse(&deck[0]);
isTwoPairCount = isTwoPair(&deck[0]);
printf("Flush Count: %d\n", flushCount);
printf("Straight Count: %d\n", straightCount);
printf("X Of A Kind Count: %d\n", xOfAKindCount);
printf("Straight Flush Count: %d\n", straightFlushCount);
printf("Full House Count: %d\n", fullHouseCount);
printf("Two Pair Count: %d\n", isTwoPairCount);
/*} while (1);*/
return EXIT_SUCCESS;
}
void initDeck(card deck[]) {
int counter;
for (counter = 0; counter < DECKSZ; counter++) {
deck[counter].pips = (const)((counter % 13) + 1);
deck[counter].suits = (const)(counter / 13);
}
}
void labelCards(card deck[]) {
const char *pipNames[] = {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
const char *suitNames[] = {" of Spades"," of Hearts"," of Diamonds"," of Clubs"};
int i, tmpPip = 0, tmpSuit = 0;
for (i = 0; i < DECKSZ; i++) {
tmpPip = (deck[i].pips) - 1;
tmpSuit = (deck[i].suits);
strcpy(deck[i].cardName, pipNames[tmpPip]);
strcat(deck[i].cardName, suitNames[tmpSuit]);
}
}
void shuffleDeck(card deck[]) {
int i, j;
for (i = 0; i < DECKSZ; i++) {
j = rand() % DECKSZ;
swap(&deck[i], &deck[j]);
}
}
boolean dealHand(card deck[], stack *stkHand) {
boolean successfulDeal = ((boolean) (0));
int i;
for (i = 0; i < HAND_SIZE; i++) {
push(&deck[i], stkHand);
}
return successfulDeal;
}
void displayHand(card hand[]) {
int i;
for (i = 0; i < HAND_SIZE; i++) {
printf("%s\n", hand[i].cardName);
}
}
void arrangeHand(card *hand) {
int i, j;
for (i = HAND_SIZE-1; i >= 0; i--) {
for (j = 0; j < i; j++) {
if ((hand+j)->pips > (hand+j+1)->pips)
swap(hand+j, hand+j+1);
}
}
}
void swap(card *c1, card *c2) {
card temp;
temp = *c1;
*c1 = *c2;
*c2 = temp;
}
boolean isFlush(card hand[]) {
int i, count = 0, result = 0;
for (i = 0; i < HAND_SIZE-1; i++) {
if (hand[i].suits != hand[i+1].suits) {
count++;
}
}
if (count == HAND_SIZE)
result = 1;
return ((boolean) (result));
}
boolean isStraight(card hand[]) {
int i, count = 0, result = 0;
for (i = 0; i < HAND_SIZE - 1; i++) {
if (hand[i].pips == (hand[i+1].pips + 1)) {
count++;
}
}
if (count == HAND_SIZE)
result = 1;
return ((boolean) (result));
}
boolean isXOfAKind(card hand[], int x, enum pip pipsIgnored) {
int i, count = 0, result = 0;
for (i = 0; i < HAND_SIZE - 1; i++) {
if (hand[i].pips == hand[i+1].pips) {
if (hand[i].pips != pipsIgnored) {
count++;
}
}
}
if (count == (x - 1))
result = 1;
return count;
}
boolean isStraightFlush(card hand[]) {
int result = 0;
result = isFlush(hand);
result = isStraight(hand);
return ((boolean) (result));
}
boolean isFullHouse(card hand[]) {
int result = 0;
result = isXOfAKind(hand, 3, 0);
result = isXOfAKind(hand, 2, 0);
return ((boolean) (result));
}
boolean isTwoPair(card hand[]) {
int result = 0;
result = isXOfAKind(hand, 2, hand->pips);
result = isXOfAKind(hand, 2, hand->pips);
return ((boolean) (result));
}
boolean isEmpty(stack *stk) {
return ((boolean) (stk->top = EMPTY));
}
boolean isFull(stack *stk) {
return ((boolean) (stk->top == FULL));
}
void push(card *c, stack *stk) {
stk->top++;
stk->s[stk -> top] = *c;
}
card pop(stack *stk) {
return (stk->s[stk->top--]);
}
void reset(stack *stk) {
stk->top = EMPTY;
}
My question pertains to the deck[] array inside main(). I would like to implement it as a stack so that each hand comes off the stack as 5 card structs when a hand is "drawn". Once the deck is out of cards, I would like it to create a newly shuffled deck of cards and continue to deal hands (meaning the stack would be repopulated to 52 cards and continue to allow hands to be popped from the stack).
Would anybody be able to help me implement a stack for my array of card structs (my "deck")? Thank you!
Upvotes: 0
Views: 1013
Reputation: 3154
You are asking for an implementation of a stack using an array. I found an implementation using Google Search (not saying it is any good). Here are PUSH and POP parts. The stuff in "main" is just pro forma stuff. You should be able to manipulate this code to do what you want.
/*STACK PUSH() AND POP() IMPLEMENTATION USING ARRAYS*/
#include <stdio.h>
#define MAX 52
int top, status;
/*PUSH FUNCTION*/
void push (int stack[], int item)
{ if (top == (MAX-1))
status = 0;
else
{ status = 1;
++top;
stack [top] = item;
}
}
/*POP FUNCTION*/
int pop (int stack[])
{
int ret;
if (top == -1)
{ ret = 0;
status = 0;
}
else
{ status = 1;
ret = stack [top];
--top;
}
return ret;
}
/*MAIN PROGRAM*/
void main()
{
int stack [MAX], item;
top = -1;
push (stack, item);
item = pop (stack);
}
Upvotes: 1
Reputation: 11791
There are several problems here: How to represent the stack of cards (an easy way to do it is using an array, as the number of elements is bounded and small; have a variable to tell how many cards are left). The other problem is to get the shuffled deck, the simplest solution is the Knuth shuffle. Note that if you take out cards by 5, you will have two cards left over at the end, you'll have to decide what to do with them (just shuffle all 52 cards each time, or shuffle just the 50 that were handed out).
Upvotes: 0