Advertisement
cd62131

Blackjack

Jul 14th, 2014
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.68 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, status;
  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.         status = draw(&suit, &num);
  22.         if (!status) break;
  23.         if (status == -1) {
  24.           printf("All cards is open.\n");
  25.           return 0;
  26.         }
  27.       }
  28.       if (suit == 1)
  29.         printf("S");
  30.       else if (suit == 2)
  31.         printf("H");
  32.       else if (suit == 3)
  33.         printf("C");
  34.       else
  35.         printf("D");
  36.  
  37.       if (num == 1)
  38.         printf("A\n");
  39.       else if (num == 11)
  40.         printf("J\n");
  41.       else if (num == 12)
  42.         printf("Q\n");
  43.       else if (num == 13)
  44.         printf("K\n");
  45.       else
  46.         printf("%d\n", num);
  47.       break;
  48.     case 's':
  49.       return 0;
  50.     default:
  51.       printf("hかsを入力してください。\n");
  52.       break;
  53.     }
  54.   } while (hs != 's');
  55.   return 0;
  56. }
  57.  
  58. int suit0(void) {
  59.   return rand() % 4 + 1;
  60. }
  61.  
  62. int num0(void) {
  63.   return rand() % 13 + 1;
  64. }
  65.  
  66. int draw(int *suit0, int *num0) {
  67.   int i, j, all = 0;
  68.   static int sn[4][13] = {
  69.       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  70.       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  71.       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  72.       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  73.   };
  74.   if (!sn[*suit0 - 1][*num0 - 1]++) return 0;
  75.   for (i = 0; i < 4; i++)
  76.     for (j = 0; j < 13; j++)
  77.       if (sn[i][j]) all++;
  78.   if (all == 52) return -1;
  79.   return 1;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement