Advertisement
Zelimkhan_Magomadov

poker

Jan 27th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5.  
  6. #define DECK_SIZE 52
  7.  
  8. #define SUIT_SIZE 4
  9. #define FACE_SIZE 13
  10.  
  11. #define CARDS_IN_HAND_SIZE 5
  12.  
  13.  
  14. void shuffle(int deck[][FACE_SIZE]);
  15. void deal(const int deck[][FACE_SIZE], const char *face[], const char *suit[], int cardsInHand[][2]);
  16.  
  17. void onePairCheck(int cardsInHand[][2], int size);
  18. void twoPairsCheck(int cardsInHand[][2], int size);
  19.  
  20. void main(void)
  21. {
  22.   srand(time(NULL));
  23.  
  24.   int deck[SUIT_SIZE][FACE_SIZE] = {0};
  25.  
  26.   const char *suit[SUIT_SIZE] = {"Hearts", "Diamonds", "Clubs", "Spades"};
  27.  
  28.   const char *face[FACE_SIZE] = {"Ace", "Deuce", "Three", "Four", "Five",
  29.                                  "Six", "Seven", "Eight", "Nine", "Ten",
  30.                                  "Jach", "Queen", "King"};
  31.  
  32.   int cardsInHand[CARDS_IN_HAND_SIZE][2];
  33.  
  34.  
  35.   shuffle(deck);
  36.   deal(deck, face, suit, cardsInHand);
  37.  
  38.   onePairCheck(cardsInHand, CARDS_IN_HAND_SIZE);
  39.   twoPairsCheck(cardsInHand, CARDS_IN_HAND_SIZE);
  40.  
  41.     return;
  42. }
  43.  
  44. void shuffle(int deck[][FACE_SIZE])
  45. {
  46.   int row;
  47.   int column;
  48.  
  49.   for (int card = 1; card <= DECK_SIZE; ++card)
  50.   {
  51.     do
  52.     {
  53.       row = rand() % SUIT_SIZE;
  54.       column = rand() % FACE_SIZE;
  55.  
  56.      }while(deck[row][column] != 0);
  57.  
  58.      deck[row][column] = card;
  59.   }
  60.  
  61. }
  62.  
  63. void deal(const int deck[][FACE_SIZE], const char *face[], const char *suit[],
  64.                                                          int cardsInHand[][2])
  65. {
  66.   for (int card = 1; card <= CARDS_IN_HAND_SIZE; ++card)
  67.   {
  68.     int row = 0;
  69.     int column = 0;
  70.  
  71.     for (int i = 1; i <= DECK_SIZE; i++)
  72.     {  
  73.        if (deck[row][column] == card)
  74.        {  
  75.           printf("%5s of %-8s\n", face[column], suit[row]);
  76.  
  77.           cardsInHand[card - 1][0] = row;
  78.           cardsInHand[card - 1][1] = column;
  79.  
  80.           break;
  81.         }
  82.    
  83.       if (i % FACE_SIZE == 0)
  84.       {
  85.          row++;
  86.          column = 0;
  87.       }
  88.      else
  89.        column++;
  90.            
  91.      }
  92.        
  93.   }
  94.  
  95. }
  96.  
  97. void onePairCheck(int cardsInHand[][2], int size)
  98. {
  99.   for (int i = 0; i < size - 1; ++i)
  100.   {
  101.      for (int j = i + 1; j < size; ++j)
  102.      {
  103.        if (cardsInHand[i][1] == cardsInHand[j][1])
  104.        {
  105.           printf("\n%50s\n", "Player has one pair");
  106.           return;
  107.         }  
  108.  
  109.      }
  110.  
  111.   }
  112.  
  113. }
  114.  
  115. void twoPairsCheck(int cardsInHand[][2], int size)
  116. {
  117.   int pair = 0;
  118.   int dontCheckTwice = -1;
  119.  
  120.   for (int i = 0; i < size - 1; ++i)
  121.   {
  122.      if (i == dontCheckTwice)
  123.      {
  124.         continue;
  125.      }
  126.      else
  127.     {
  128.       for (int j = i + 1; j < size; ++j)
  129.       {
  130.          if (cardsInHand[i][1] == cardsInHand[j][1])
  131.          {
  132.             pair++;
  133.             dontCheckTwice = j;
  134.  
  135.             break;
  136.          }  
  137.  
  138.       }  
  139.  
  140.     }
  141.  
  142.   if (pair == 2)
  143.   {
  144.      printf("\n%51s\n", "Player has two pairs");
  145.      return;
  146.   }
  147.  
  148.   }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement