Advertisement
cd62131

Blackjack

Jul 12th, 2014
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int suit0();
  6. int num0();
  7. int draw(int suit, int num);
  8.  
  9. int main(void) {
  10.   char hs;
  11.   int suit, num;
  12.   srand((unsigned) time(NULL));
  13.   do {
  14.     printf("Hit or Stand?(h/s):");
  15.     scanf(" %c", &hs);
  16.     switch (hs) {
  17.     case 'h':
  18.       while(1) {
  19.         suit = suit0();
  20.         num = num0();
  21.         if (!draw(suit, num)) break;
  22.       }
  23.       if (suit == 1)
  24.         printf("S");
  25.       else if (suit == 2)
  26.         printf("H");
  27.       else if (suit == 3)
  28.         printf("C");
  29.       else
  30.         printf("D");
  31.  
  32.       if (num == 1)
  33.         printf("A\n");
  34.       else if (num == 11)
  35.         printf("J\n");
  36.       else if (num == 12)
  37.         printf("Q\n");
  38.       else if (num == 13)
  39.         printf("K\n");
  40.       else
  41.         printf("%d\n", num);
  42.       break;
  43.     case 's':
  44.       return 0;
  45.     default:
  46.       printf("hかsを入力してください。\n");
  47.       break;
  48.     }
  49.   } while (hs != 's');
  50.   return 0;
  51. }
  52.  
  53. int suit0(void) {
  54.   return rand() % 4 + 1;
  55. }
  56.  
  57. int num0(void) {
  58.   return rand() % 13 + 1;
  59. }
  60.  
  61. int draw(int suit0, int num0) {
  62.   static int sn[4][13] = {
  63.       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  64.       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  65.       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  66.       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  67.   };
  68.   if (sn[suit0][num0] > 0)
  69.     return 1;
  70.   else {
  71.     sn[suit0][num0]++;
  72.     return 0;
  73.   }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement