Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- #define NUM_SUITE 4
- #define NUM_RANKS 13
- #define MAXPLAYERS 4
- #define MAXCHAR 40
- #define MAXCARDS 5
- //prototype
- int deal(char*);
- int main(void)
- {
- // declaration
- char player[MAXPLAYERS][MAXCHAR];
- int playerRank[MAXPLAYERS][MAXCARDS];
- char playerSuite[MAXPLAYERS][MAXCARDS];
- int playerTotal[MAXPLAYERS];
- char suite = ' ';
- // randomness ???
- srand((unsigned)time(NULL));
- //loop 1 == assign names
- for (int iPlayer = 0; iPlayer < MAXPLAYERS; iPlayer++)
- {
- printf("Enter the players name: ");
- gets_s(player[iPlayer]);
- }
- //loop 2 == deal cards
- for (int iPlayer = 0; iPlayer < MAXPLAYERS; iPlayer++)
- {
- printf("player %d name %s\n", iPlayer + 1, player[iPlayer]);
- for (int iCard = 0; iCard < MAXCARDS; iCard++)
- {
- playerRank[iPlayer][iCard] = deal(&suite);
- playerSuite[iPlayer][iCard] = suite;
- }
- }
- //compare hands
- for (int iPlayer = 0; iPlayer < MAXPLAYERS; iPlayer++)
- {
- //convert rank from character value to int value
- for (int iCard = 0; iCard < MAXCARDS; iCard++)
- {
- if (playerRank[iPlayer][iCard] >= 50 && playerRank[iPlayer][iCard] <= 59)
- playerRank[iPlayer][iCard] -= 48;
- else
- playerRank[iPlayer][iCard] = 10;
- }
- }
- printf("\n\n");
- return (0);
- } // end main()
- int deal(char *playerSuite)
- {
- //local declaration
- bool in_hand[NUM_SUITE][NUM_RANKS] = { false }; // will prevent repeated cards
- int num_cards = 1; // number of cards in each hand
- int rank; // card number
- int suite; // card suit
- const char rank_code[] = { '2', '3', '4', '5', '6', '7', '8', // possible numbers
- '9', 't', 'j', 'q', 'k', 'a' };
- const char suite_code[] = { 'c', 'd', 'h', 's' }; // possible suits
- do
- {
- do
- {
- suite = rand() % NUM_SUITE; // picks a random suit
- rank = rand() % NUM_RANKS; // picks a random rank
- if (!in_hand[suite][rank]) // if card not in hand do the following
- {
- in_hand[suite][rank] = true; // add card to hand
- num_cards--; // count down number of cards needed
- printf(" %c%c", rank_code[rank], suite_code[suite]); // display card
- }
- } while (in_hand[suite][rank] == false); // get a new card if card generated was already in hand
- } while (num_cards > 0); // keep going until 5 cards in hand
- printf("\n");
- *playerSuite = suite_code[suite];
- return rank_code[rank];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement