Advertisement
cd62131

Sevens

Jul 14th, 2014
1,693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.01 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6. typedef struct {
  7.   int suit;
  8.   int number;
  9.   char *display;
  10. } card_t;
  11. static const int suit = 4;
  12. static const int number = 13;
  13. static void shuffle(card_t **cards);
  14. static card_t **init(const char **suits, const char **numbers);
  15. static int input_number(int lower, int upper);
  16. static int ask_players(void);
  17. static card_t ***deal_to(card_t **cards, int player);
  18. static void compact_one_card(card_t **some_player, int player);
  19. static void first_play_seven(card_t ***players, int player);
  20. static int count_card(card_t **some_player, int player);
  21. static int **init_open(void);
  22. static void puts_open_card(int **open, const char **suits, const char **numbers);
  23. static int win(card_t **some_player, int player);
  24. static void win_first_hand(card_t ***players, int player);
  25. static void puts_hand(int no, card_t **some_player, int player);
  26. static int can_put(card_t *card, int **open);
  27. static int pass_or_not(card_t **some_player, int player, int **open);
  28. static void play(card_t **some_player, int in, int player, int **open);
  29. static void choice(int no, card_t **some_player, int player, int **open);
  30. static void shuffle(card_t **cards) {
  31.   int i, j;
  32.   card_t *p;
  33.   srand((unsigned) time(NULL));
  34.   for (i = suit * number - 1; i > 0; i--) {
  35.     j = (int) (rand() / ((double) (RAND_MAX) + 1.) * (i + 1));
  36.     p = cards[i];
  37.     cards[i] = cards[j];
  38.     cards[j] = p;
  39.   }
  40. }
  41. static card_t **init(const char **suits, const char **numbers) {
  42.   int i, j, k;
  43.   card_t **cards = (card_t **) malloc(suit * number * sizeof(card_t *));
  44.   for (i = 0; i < suit; i++) {
  45.     for (j = 0; j < number; j++) {
  46.       k = i * number + j;
  47.       cards[k] = (card_t *) malloc(sizeof(card_t));
  48.       cards[k]->suit = i;
  49.       cards[k]->number = j;
  50.       cards[k]->display = (char *) malloc((strlen(suits[i]) + 2 + 1) * sizeof(char));
  51.       sprintf(cards[k]->display, "%s%s", suits[i], numbers[j]);
  52.     }
  53.   }
  54.   shuffle(cards);
  55.   return cards;
  56. }
  57. static int input_number(int lower, int upper) {
  58.   char in[BUFSIZ];
  59.   int i, ret;
  60.   while (1) {
  61.     printf("(%d - %d): ", lower, upper);
  62.     fgets(in, BUFSIZ, stdin);
  63.     in[strlen(in) - 1] = '\0';
  64.     for (i = 0; i < strlen(in); i++)
  65.       if (!isdigit(in[i])) goto clear;
  66.     ret = atoi(in);
  67.     if (ret < lower || upper < ret) goto clear;
  68.     break;
  69.     clear: ;
  70.   }
  71.   return ret;
  72. }
  73. static int ask_players(void) {
  74.   printf("Players? ");
  75.   return input_number(1, 52);
  76. }
  77. static card_t ***deal_to(card_t **cards, int player) {
  78.   int i, j;
  79.   card_t ***players = (card_t ***) malloc(player * sizeof(card_t **));
  80.   for (i = 0; i < player; i++) {
  81.     players[i] = (card_t **) malloc((suit * number / player + 1) * sizeof(card_t *));
  82.     for (j = 0; j < suit * number / player + 1; j++) {
  83.       players[i][j] = NULL;
  84.     }
  85.   }
  86.   for (i = 0, j = 0; i < suit * number; i++, j++) {
  87.     players[j % player][i / player] = cards[i];
  88.   }
  89.   return players;
  90. }
  91. static void compact_one_card(card_t **some_player, int player) {
  92.   int i;
  93.   for (i = 0; i < suit * number / player; i++) {
  94.     if (some_player[i]) continue;
  95.     some_player[i] = some_player[i + 1];
  96.     some_player[i + 1] = NULL;
  97.   }
  98. }
  99. static void first_play_seven(card_t ***players, int player) {
  100.   int i, j;
  101.   for (i = 0; i < player; i++) {
  102.     for (j = 0; j < suit * number / player + 1; j++) {
  103.       if (!players[i][j]) break;
  104.       if (players[i][j]->number == 7 - 1) {
  105.         players[i][j] = NULL;
  106.         compact_one_card(players[i], player);
  107.         j--;
  108.       }
  109.     }
  110.   }
  111. }
  112. static int count_card(card_t **some_player, int player) {
  113.   int i, c = 0;
  114.   for (i = 0; i < suit * number / player + 1; i++) {
  115.     if (!some_player[i]) break;
  116.     c++;
  117.   }
  118.   return c;
  119. }
  120. static int **init_open(void) {
  121.   int i, j;
  122.   int **open = (int **) malloc(suit * sizeof(int *));
  123.   for (i = 0; i < suit; i++) {
  124.     open[i] = (int *) malloc(number * sizeof(int));
  125.     for (j = 0; j < number; j++) {
  126.       if (j != 7 - 1) {
  127.         open[i][j] = 0;
  128.         continue;
  129.       }
  130.       open[i][j] = 1;
  131.     }
  132.   }
  133.   return open;
  134. }
  135. static void puts_open_card(int **open, const char **suits, const char **numbers) {
  136.   int i, j;
  137.   printf("   ");
  138.   for (i = 0; i < number; i++) {
  139.     if (i != 9) printf(" ");
  140.     printf("%s", numbers[i]);
  141.   }
  142.   puts("");
  143.   for (i = 0; i < suit; i++) {
  144.     printf("%s ", suits[i]);
  145.     for (j = 0; j < number; j++) {
  146.       if (open[i][j] == 0)
  147.         printf(" .");
  148.       else
  149.         printf(" #");
  150.     }
  151.     puts("");
  152.   }
  153. }
  154. static int win(card_t **some_player, int player) {
  155.   return count_card(some_player, player) == 0;
  156. }
  157. static void win_first_hand(card_t ***players, int player) {
  158.   int i;
  159.   for (i = 0; i < player; i++) {
  160.     if (win(players[i], player)) {
  161.       printf("Player %d win, becouse all hands are 7.\n", i);
  162.       exit(0);
  163.     }
  164.   }
  165. }
  166. static void puts_hand(int no, card_t **some_player, int player) {
  167.   int i, c;
  168.   printf("Player %d: ", no);
  169.   c = count_card(some_player, player);
  170.   for (i = 0; i < c; i++) {
  171.     printf("%d:[%s]", i, some_player[i]->display);
  172.     if (i < c - 1) printf(", ");
  173.   }
  174.   printf("  ");
  175. }
  176. static int can_put(card_t *card, int **open) {
  177.   int suit = card->suit, number = card->number;
  178.   if (number == 1 - 1)
  179.     return open[suit][2 - 1] == 1;
  180.   else if (number == 13 - 1)
  181.     return open[suit][12 - 1] == 1;
  182.   else
  183.     return open[suit][number - 1] == 1 || open[suit][number + 1] == 1;
  184. }
  185. static int pass_or_not(card_t **some_player, int player, int **open) {
  186.   int i, c, ret = 0;
  187.   c = count_card(some_player, player);
  188.   for (i = 0; i < c; i++)
  189.     ret = ret || can_put(some_player[i], open);
  190.   return ret;
  191. }
  192. static void play(card_t **some_player, int in, int player, int **open) {
  193.   card_t *card = some_player[in];
  194.   open[card->suit][card->number] = 1;
  195.   some_player[in] = NULL;
  196.   compact_one_card(some_player, player);
  197. }
  198. static void choice(int no, card_t **some_player, int player, int **open) {
  199.   int c, in;
  200.   puts_hand(no, some_player, player);
  201.   if (!pass_or_not(some_player, player, open)) {
  202.     puts("pass");
  203.     return;
  204.   }
  205.   c = count_card(some_player, player);
  206.   do {
  207.     in = input_number(0, c - 1);
  208.   } while (!can_put(some_player[in], open));
  209.   play(some_player, in, player, open);
  210. }
  211. int main(void) {
  212.   int i;
  213.   int player;
  214.   card_t **cards;
  215.   card_t ***players;
  216.   int **open;
  217.   const char *suits[] = { "♠", "♥", "♦", "♣" };
  218.   const char *numbers[] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
  219.   cards = init(suits, numbers);
  220.   player = ask_players();
  221.   players = deal_to(cards, player);
  222.   first_play_seven(players, player);
  223.   if (suit * number / player <= 4) {
  224.     win_first_hand(players, player);
  225.   }
  226.   open = init_open();
  227.   do {
  228.     for (i = 0; i < player; i++) {
  229.       puts_open_card(open, suits, numbers);
  230.       choice(i, players[i], player, open);
  231.       if (win(players[i], player)) goto over;
  232.     }
  233.     continue;
  234.     over: printf("Player %d win.\n", i);
  235.     break;
  236.   } while (1);
  237.   return 0;
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement