Hi I really need help with this program. I'm separating it into 3 functions:
read_cards
anaylze_cards
and then print_results
but I'm stuck on read_cards
I'm basically doing a color blind poker game where the suits are ignoreed. There are two players and each player has 5 cards. The players compare their hands according to rankings. But I'm stuck on how to have the user input their cards in a
2x5 array. ANY help is really apperciated. I have my anaylze functions pretty much done but it needs to input like this:
02020 JJQQA
1st ply| 2nd plyr
Code:#include <stdbool.h> /* C99 only */ #include <stdio.h> #include <stdlib.h> #define NUM_PLAYERS 2 #define HANDS 2 #define CARDS 5 /* external variables */ int hand[NUM_PLAYERS][5]; bool straight, flush, four, three; int pairs; /* can be 0, 1, or 2 */ /* prototypes */ void read_cards(void); void analyze_hand(void); void print_result(void); /********************************************************** * main: Calls read_cards, analyze_hand, and print_result * * repeatedly. * **********************************************************/ int main(void) { for (;;) { read_cards(); } } /********************************************************** * read_cards: Reads the cards into the external variable * * hand; checks for bad cards and duplicate * * cards. * **********************************************************/ void read_cards(void) { char ch, hands_ch, cards_ch; int i, hands, cards; bool bad_card, duplicate_card; int cards_read = 0; printf("Enter a card: "); cards_ch = getchar(); switch (cards_ch) { case '0': cards = 10; break; case '2': cards = 0; break; case '3': cards = 1; break; case '4': cards = 2; break; case '5': cards = 3; break; case '6': cards = 4; break; case '7': cards = 5; break; case '8': cards = 6; break; case '9': cards = 7; break; case 't': case 'T': cards = 8; break; case 'j': case 'J': cards = 9; break; case 'q': case 'Q': cards = 10; break; case 'k': case 'K': cards = 11; break; case 'a': case 'A': cards = 12; break; default: bad_card = true; } hands_ch = getchar(); while ((ch = getchar()) != '\n') if (ch != ' ') bad_card = true; if (bad_card) { printf("Bad card; ignored.\n"); continue; } duplicate_card = false; for (i = 0; i < cards_read; i++) if (hand[i][HANDS] == hands && hand[i][CARDS] == cards) { printf("Duplicate card; ignored.\n"); duplicate_card = true; break; } if (!duplicate_card) { hand[cards_read][HANDS] = hands; hand[cards_read][CARDS] = cards; cards_read++; } } }



LinkBack URL
About LinkBacks


