0% found this document useful (0 votes)
182 views

Card Shuffling and Dealing

This C program simulates shuffling and dealing cards to two players. It performs the following steps: 1. Defines constants for the number of card faces, suits, and total cards. It initializes arrays to store the face and suit names. 2. Shuffles the deck using a random number generator and stores the results in a 2D array. 3. Deals 10 cards from the shuffled deck to each player, storing the face and suit of each card. 4. Checks each player's hand for pairs, flushes, straights, etc and prints the results. Functions are used to check each category independently.

Uploaded by

psss
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
182 views

Card Shuffling and Dealing

This C program simulates shuffling and dealing cards to two players. It performs the following steps: 1. Defines constants for the number of card faces, suits, and total cards. It initializes arrays to store the face and suit names. 2. Shuffles the deck using a random number generator and stores the results in a 2D array. 3. Deals 10 cards from the shuffled deck to each player, storing the face and suit of each card. 4. Checks each player's hand for pairs, flushes, straights, etc and prints the results. Functions are used to check each category independently.

Uploaded by

psss
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

//Chapter 7 Exercise //7.12 Card Shuffling and Dealing #include <stdio.h> #include <stdlib.h> #include <time.

h> #define FACE 13 #define SUIT 4 #define CARDS 52 void shuffle(int wDeck[][SUIT]); void deal(int wDeck[][SUIT], char *wFace[], char *wSuit[]); int main() { char *face[FACE] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Sev en", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}; char *suit[SUIT] = {"Spades", "Hearts", "Clubs", "Diamonds"}; int deck[FACE][SUIT] = {0}; int point1 = 0, point2 = 0; srand(time(NULL)); shuffle(deck); deal(deck, face, suit); puts(""); } void shuffle(int wDeck[][SUIT]) { int row, column; int counter; for(counter = 1; counter <= CARDS; ++counter){ do{ row = rand() % SUIT; column = rand() % FACE; }while(wDeck[column][row] != 0); wDeck[column][row] = counter; } } void deal(int wDeck[][SUIT], char *wFace[], char *wSuit[]) { void checkFace(int wColumnNumber[]); void checkSuit(int wRowNumber[]); void checkConsecutive(int wColumnNumber[]); int int int int int int row, column; counter; columnNumber1[5] = {0}; rowNumber1[5] = {0}; columnNumber2[5] = {0}; rowNumber2[5] = {0};

for(counter = 1; counter <= 10; counter++){ for(row = 0; row < SUIT; row++){ for(column = 0; column < FACE; column++){ if(wDeck[column][row] == counter){ if(counter <= 5){ if(counter == 1){ puts("Player 1 Hand"); puts("-------------"); } printf("\t%5s of %-8s\n", wFace[ column], wSuit[row]); columnNumber1[counter - 1] = col umn; rowNumber1[counter - 1] = row; } else{ if(counter == 6){ puts(""); puts("Player 2 Hand"); puts("-------------"); } printf("\t%5s of %-8s\n", wFace[ column], wSuit[row]); columnNumber2[counter - 6] = col umn; rowNumber2[counter - 6] = row; } } } } } puts(""); puts("--------------------------------"); puts(""); puts("Player 1 Status"); puts("---------------"); checkFace(columnNumber1); checkConsecutive(columnNumber1); checkSuit(rowNumber1); puts(""); puts("Player 2 Status"); puts("---------------"); checkFace(columnNumber2); checkConsecutive(columnNumber2); checkSuit(rowNumber2); } //AABCD //AABBD //AAABC //AAABB //AAAAB count count count count count == == == == == 1 2 3 4 5 -> -> -> -> -> a pair two pair three of a kind three of a kind + a pair four of a kind

void checkFace(int wColumnNumber[]) { int outer_counter, inner_counter; int count = 0;

for(outer_counter = 0; outer_counter < 5; outer_counter++){ for(inner_counter = outer_counter + 1; inner_counter < 5; inner_ counter++){ if(wColumnNumber[outer_counter] == wColumnNumber[inner_c ounter]){ count++; } } } printf("\t"); switch(count){ case 0: puts("No pair."); break; case 1: puts("A pair!"); break; case 2: puts("Two pair!"); break; case 3: puts("Three of a kind!"); break; case 4: puts("Full house"); break; case 5: puts("Four of a kind!"); } } void checkSuit(int wRowNumber[]) { int count = 0; int counter; for(counter = 1; counter < 5; counter++){ if(wRowNumber[0] == wRowNumber[counter]){ count++; } } printf("\t"); if(count == 4){ puts("Flush!"); } else { puts("No flush."); } } void checkConsecutive(int wColumnNumber[]) { int hold; int outer_counter; int inner_counter;

int counter = 0; for(outer_counter = 0; outer_counter < 4; outer_counter++){ for(inner_counter = outer_counter; inner_counter < 4; inner_coun ter++){ if(wColumnNumber[inner_counter] > wColumnNumber[inner_co unter + 1]){ hold = wColumnNumber[inner_counter]; wColumnNumber[inner_counter] = wColumnNumber[inn er_counter + 1]; wColumnNumber[inner_counter + 1] = hold; } } } for(outer_counter = 0; outer_counter < 4; outer_counter++){ if(wColumnNumber[outer_counter] == wColumnNumber[outer_counter + 1] - 1){ counter++; } } if(wColumnNumber[3] == FACE - 1 && wColumnNumber[4] == 0){ counter++; } printf("\t"); if(counter == 4){ puts("Straight!"); } else{ puts("No Straight."); } }

You might also like