Advertisement
cd62131

Sevens

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