Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <time.h>
  6. #define SIZE 36
  7. #define HAND_SIZE 6
  8. #define LEAR_LIST 4
  9. #define MAX 14
  10.  
  11.  
  12. typedef struct
  13. {
  14.     int par;
  15.     char lear;
  16. }card;
  17.  
  18. void create_deck(card deck[]);
  19. void take_card(card deck[], card hand[], int *curr);
  20. void print_hand (card hand[]);
  21. void welcome();
  22.  
  23.  
  24. int main(void)
  25. {
  26.     system ("chcp 65001");
  27.     system ("cls");
  28.     system ("color F0");
  29.     int curr=0;
  30.     char ch=0;
  31.     card deck[SIZE]={0};
  32.     card hand[HAND_SIZE]={0};
  33.     create_deck(deck);
  34.     welcome();
  35.     printf("Press 'y'\n");
  36.     while(1)
  37.     {
  38.         ch=_getch();
  39.         if(ch=='y')
  40.             {
  41.             take_card(deck, hand, &curr);
  42.             print_hand(hand);
  43.             printf("\nTake one more card?(y/n): \n");
  44.             }
  45.         else if(ch=='n')
  46.         {
  47.             system("cls");
  48.             printf("=)");
  49.             break;
  50.         }
  51.     }
  52.     _getch();
  53.     return 0;
  54. }
  55.  
  56. void create_deck(card deck[])
  57. {
  58.     int temp_index=0;
  59.     for(int i=0;i<LEAR_LIST;i++)
  60.     {
  61.             for(int j=6;j<=MAX;j++)
  62.             {
  63.                 deck[temp_index].par=j;
  64.                 deck[temp_index].lear=65+i;
  65.                 temp_index++;
  66.             }
  67.     }
  68. }
  69.  
  70.  
  71. void take_card(card deck[], card hand[], int *curr)
  72. {
  73.     time_t tm;
  74.     int t=time(&tm);
  75.     srand(t);
  76.     int index=rand()%(SIZE-1);;
  77.     while(1)
  78.     {
  79.         if(deck[index].par==0 && deck[index].lear=='0')
  80.         {
  81.         index=rand()%(SIZE-1); 
  82.         }
  83.         else
  84.         {
  85.             hand[*curr]=deck[index];
  86.             deck[index].par=0;
  87.             deck[index].lear='0';
  88.             break;
  89.         }
  90.     }
  91. }
  92.  
  93. void print_hand (card hand[])
  94. {
  95.     for(int i=0;i<HAND_SIZE;i++)
  96.     {
  97.         if(hand[i].par==0) break;
  98.         switch (hand[i].par)
  99.         {
  100.             case 11:
  101.             printf("J"); break;  
  102.             case 12:
  103.             printf("D"); break;  
  104.             case 13:
  105.             printf("K"); break;  
  106.             case 14:
  107.             printf("A"); break;
  108.             default:
  109.             printf("%d", hand[i].par);
  110.             break;
  111.         }
  112.         switch (hand[i].lear)
  113.         {
  114.             case 65:
  115.             printf("\x06"); break;  //SPADE  
  116.             case 66:
  117.             printf("\x05"); break; //CLUB    
  118.             case 67:
  119.             printf("\x03"); break;  //HEART  
  120.             case 68:
  121.             printf("\x04"); break; //DIAMOND
  122.             default:
  123.             break;
  124.         }
  125.         printf ("  ");
  126.     }
  127. }
  128.  
  129. void welcome()
  130. {
  131.     printf("-----------------------------------------------\n");
  132.     printf("|               Black Jack Game               |\n");
  133.     printf("-----------------------------------------------\n");
  134.     printf("Press any key to read rules...\n");
  135.     _getch();
  136.     system("cls");
  137.     printf("Rules: \n\n");
  138.     printf(" You must collect a combination of cards as close as possible to 21.\n \
  139. If you go over, you lose.\n \
  140. To win - the sum of your cards must be higher than the sum of the dealer's cards.\n\n");
  141.     printf("Costs of carts: \n\n");
  142.     printf("6 - 6\n7 - 7\n8 - 8\n9 - 9\n10 - 10\nJ/D/K - 10\nA - 1/11(in ur choose)\n\n");
  143.     printf("Press any key to start....");
  144.     _getch();
  145.     system("cls");
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement