Guest User

Untitled

a guest
Jun 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5.  
  6. void game(int rate);
  7. void addcard(char* who);
  8. int cost(int cards[], int count);
  9. void output(int cards[], int suits[], int count);
  10. bool isBlackjack(int rate);
  11.  
  12. int balance = 1000;
  13. int rate;
  14. int winMultiplier;
  15. bool isUsed[13][4];
  16. int playerCards[12], playerSuits[12], playerCount;
  17. int dealerCards[12], dealerSuits[12], dealerCount;
  18. char *cardsName[13] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
  19. char *suitName[4] = {"spades", "hearts", "diamonds", "clubs"};
  20.                     //пики      черви     бубны       трефы
  21.  
  22. int main()
  23. {
  24.     extern int rate;
  25.     extern int balance;
  26.     printf("Blackjack game\n");
  27.     again: ;
  28.     printf("\nYour balance is %d.\n", balance);
  29.     if (balance == 0) {
  30.         printf("Game over.\n");
  31.         getchar();
  32.         return 0;
  33.     }
  34.     printf("Enter rate (or 0 to end game):\n");
  35.     ratein: ;
  36.     scanf("%d", &rate);
  37.     if (rate == 0) {
  38.         printf("Game was over with balance %d.\n", balance);
  39.         getchar();
  40.         return 0;
  41.     } else if (rate > balance) {
  42.         printf("Rate is too big. Enter smaller or 0 to end game.\n");
  43.         goto ratein;
  44.     } else {
  45.         game(rate);
  46.         goto again;
  47.     }
  48. }
  49.  
  50. void game(int rate)
  51. {
  52.     extern int winMultiplier;
  53.     winMultiplier = 1;
  54.     extern int balance;
  55.     extern int playerCards[12], playerSuits[12], playerCount;
  56.     playerCount = 0;
  57.     extern int dealerCards[12], dealerSuits[12], dealerCount;
  58.     dealerCount = 0;
  59.     addcard("player");
  60.     addcard("player");
  61.     addcard("dealer");
  62.     if (playerCount == 2 && isBlackjack(rate) == true)
  63.         return;
  64.  
  65.     costCheck: ;
  66.     printf("Your cards: ");
  67.     output(playerCards, playerSuits, playerCount);
  68.     printf("Your points: %d\n", cost(playerCards, playerCount));
  69.     printf("Dealer cards: ");
  70.     output(dealerCards, dealerSuits, dealerCount);
  71.     printf("Dealer points: %d\n", cost(dealerCards, dealerCount));
  72.     if (cost(playerCards, playerCount) > 21) {
  73.         printf("Oops! You've got overkill with %d points. You lost.\n", cost(playerCards, playerCount));
  74.         balance -= rate;
  75.         return;
  76.     }
  77.     if (cost(dealerCards, dealerCount) > 21) {
  78.         printf("Dealer has got overkill with %d points. You won!\n", cost(dealerCards, dealerCount));
  79.         balance += rate * winMultiplier;
  80.         return;
  81.     }
  82.     if (cost(playerCards, playerCount) <= 21 && cost(dealerCards, dealerCount) > 17 &&
  83.         cost(dealerCards, dealerCount) < 21) {
  84.         printf("You've won with %d points, which more than %d dealer points.",
  85.                cost(playerCards, playerCount), cost(dealerCards, dealerCount));
  86.         balance += rate * winMultiplier;
  87.         return;
  88.     }
  89.  
  90.     char c;
  91.     if (cost(playerCards, playerCount) < 21) {
  92.         printf("One more card (Y/n) ?\n");
  93.         scanf("%c", &c);
  94.         if (c == 'Y' || c == 'y') {
  95.             addcard("player");
  96.             goto costCheck;
  97.         }
  98.     }
  99.     if ((c == 'N' || c == 'n') && cost(dealerCards, dealerCount) < 17) {
  100.         addcard("dealer");
  101.         goto costCheck;
  102.     }
  103. }
  104.  
  105. void addcard(char* who)
  106. {
  107.     extern bool isUsed[13][4];
  108.     extern int playerCards[12], playerSuits[12], playerCount;
  109.     extern int dealerCards[12], dealerSuits[12], dealerCount;
  110.     if (!strcmp(who, "player")){
  111.         do {
  112.             srand(time(NULL));
  113.             playerCards[playerCount] = rand() % 13;
  114.             srand(time(NULL));
  115.             playerSuits[playerCount] = rand() % 4;
  116.         } while (isUsed[playerCards[playerCount]][playerSuits[playerCount]] == true);
  117.         isUsed[playerCards[playerCount]][playerSuits[playerCount]] = true;
  118.         ++playerCount;
  119.     } else {
  120.         do {
  121.             srand(time(NULL));
  122.             dealerCards[dealerCount] = rand() % 13;
  123.             srand(time(NULL));
  124.             dealerSuits[dealerCount] = rand() % 4;
  125.         } while (isUsed[dealerCards[dealerCount]][dealerSuits[dealerCount]] == true);
  126.         isUsed[dealerCards[dealerCount]][dealerSuits[dealerCount]] = true;
  127.         ++dealerCount;
  128.     }
  129. }
  130.  
  131. int cost(int cards[], int count)
  132. {
  133.     int i, total = 0;
  134.     for (i = 0; i < count; ++i){
  135.         switch (cards[i]) {
  136.             case  0: total += 11; break;    //Ace
  137.             case  1: total += 2; break;     //2
  138.             case  2: total += 3; break;     //3
  139.             case  3: total += 4; break;     //4
  140.             case  4: total += 5; break;     //5
  141.             case  5: total += 6; break;     //6
  142.             case  6: total += 7; break;     //7
  143.             case  7: total += 8; break;     //8
  144.             case  8: total += 9; break;     //9
  145.             case  9: total += 10; break;    //10
  146.             case 10:                        //Jack
  147.             case 11:                        //Queen
  148.             case 12: total += 10; break;    //King
  149.         }
  150.     }
  151.     if (total > 21) { //Если набралось больше 21, то пересчет:
  152.         total = 0;    //тузы будут стоить 1 вместо 11
  153.         for (i = 0; i < count; ++i)
  154.             switch (cards[i]) {
  155.                 case  0: total += 1; break;     //Ace
  156.                 case  1: total += 2; break;     //2
  157.                 case  2: total += 3; break;     //3
  158.                 case  3: total += 4; break;     //4
  159.                 case  4: total += 5; break;     //5
  160.                 case  5: total += 6; break;     //6
  161.                 case  6: total += 7; break;     //7
  162.                 case  7: total += 8; break;     //8
  163.                 case  8: total += 9; break;     //9
  164.                 case  9: total += 10; break;    //10
  165.                 case 10:                        //Jack
  166.                 case 11:                        //Queen
  167.                 case 12: total += 10; break;    //King
  168.         }
  169.     }
  170.     return total;
  171. }
  172.  
  173. bool isBlackjack(int rate)
  174. {
  175.     extern int playerCards[12], playerSuits[12], playerCount;
  176.     extern int dealerCards[12], dealerSuits[12], dealerCount;
  177.     extern int winMultiplier;
  178.     if (playerCount == 2 && dealerCount == 1) {
  179.         if (cost(playerCards, playerCount) == 21 && !(dealerCards[0] == 2 || dealerCards[0] == 9 ||
  180.                                                       dealerCards[0] == 10 || dealerCards[0] == 11 ||
  181.                                                       dealerCards[0] == 12)) {
  182.             balance += rate * 1.5;
  183.             printf("Congratulations! You winned with Blackjack!\n");
  184.             return true;
  185.         } else if (dealerCards[0] == 0) {
  186.             printf("Do you want to take your win which equal to the rate (Y/n) ?");
  187.             int c;
  188.             c = getchar();
  189.             if (c == 'Y' || c == 'y') {
  190.                 balance += rate;
  191.                 return true;
  192.             } else if (c == 'N' || c == 'n') {
  193.                 winMultiplier = 1.5;
  194.                 return false;
  195.             }
  196.         }
  197.     }
  198.     return false;
  199. }
  200.  
  201. void output(int cards[], int suits[], int count)
  202. {
  203.     extern char *cardsName[13];
  204.     extern char *suitName[4];
  205.     int i;
  206.     for (i = 0; i < count; ++i) {
  207.         printf("%s of %s", cardsName[cards[i]], suitName[suits[i]]);
  208.         if (i != count - 1)
  209.             printf(", ");
  210.         else
  211.             printf("\n");
  212.     }
  213. }
Add Comment
Please, Sign In to add comment