Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.42 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <string.h>
  6.  
  7. #define NUM_SUITE 4
  8. #define NUM_RANKS 13
  9. #define MAXPLAYERS 4
  10. #define MAXCHAR 40
  11. #define MAXCARDS 5
  12.  
  13. //prototype
  14. int deal(char*);
  15.  
  16.  
  17. int main(void)
  18. {
  19.     // declaration
  20.     char player[MAXPLAYERS][MAXCHAR];
  21.     int playerRank[MAXPLAYERS][MAXCARDS];
  22.     char playerSuite[MAXPLAYERS][MAXCARDS];
  23.     int playerTotal[MAXPLAYERS];
  24.     char suite = ' ';
  25.  
  26.     // randomness ???
  27.     srand((unsigned)time(NULL));
  28.  
  29.     //loop 1 == assign names
  30.     for (int iPlayer = 0; iPlayer < MAXPLAYERS; iPlayer++)
  31.     {
  32.         printf("Enter the players name: ");
  33.         gets_s(player[iPlayer]);
  34.     }
  35.  
  36.     //loop 2 == deal cards
  37.     for (int iPlayer = 0; iPlayer < MAXPLAYERS; iPlayer++)
  38.     {
  39.         printf("player %d name %s\n", iPlayer + 1, player[iPlayer]);
  40.  
  41.         for (int iCard = 0; iCard < MAXCARDS; iCard++)
  42.         {
  43.             playerRank[iPlayer][iCard] = deal(&suite);
  44.             playerSuite[iPlayer][iCard] = suite;
  45.         }
  46.     }
  47.  
  48.     //compare hands
  49.     for (int iPlayer = 0; iPlayer < MAXPLAYERS; iPlayer++)
  50.     {
  51.         //convert rank from character value to  int value
  52.         for (int iCard = 0; iCard < MAXCARDS; iCard++)
  53.         {
  54.             if (playerRank[iPlayer][iCard] >= 50 && playerRank[iPlayer][iCard] <= 59)
  55.                 playerRank[iPlayer][iCard] -= 48;
  56.             else
  57.                 playerRank[iPlayer][iCard] = 10;
  58.         }
  59.     }
  60.  
  61.     printf("\n\n");
  62.     return (0);
  63. } // end main()
  64.  
  65. int deal(char *playerSuite)
  66. {
  67.     //local declaration
  68.     bool in_hand[NUM_SUITE][NUM_RANKS] = { false }; // will prevent repeated cards
  69.     int num_cards = 1; // number of cards in each hand
  70.     int rank; // card number
  71.     int suite; // card suit
  72.     const char rank_code[] = { '2', '3', '4', '5', '6', '7', '8', // possible numbers
  73.         '9', 't', 'j', 'q', 'k', 'a' };
  74.     const char suite_code[] = { 'c', 'd', 'h', 's' }; // possible suits
  75.  
  76.                                                      
  77.  
  78.     do
  79.     {
  80.         do
  81.         {
  82.             suite = rand() % NUM_SUITE;  // picks a random suit
  83.             rank = rand() % NUM_RANKS;   // picks a random rank
  84.             if (!in_hand[suite][rank]) // if card not in hand do the following
  85.             {
  86.                 in_hand[suite][rank] = true; // add card to hand
  87.                 num_cards--; // count down number of cards needed
  88.                 printf(" %c%c", rank_code[rank], suite_code[suite]); // display card
  89.             }
  90.         } while (in_hand[suite][rank] == false); // get a new card if card generated was already in hand
  91.     } while (num_cards > 0); // keep going until 5 cards in hand
  92.  
  93.  
  94.     printf("\n");
  95.     *playerSuite = suite_code[suite];
  96.     return rank_code[rank];
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement