Advertisement
I_LIKE_COFFEE

cards

Feb 16th, 2020
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.02 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #define SIZE_RANK 13
  6. #define SIZE_SUIT 4
  7. #define SIZE_DECK 52
  8.  
  9. typedef struct {//структура масти карты, используется для последующего помещения каждой масти в массив
  10.     char suit_for_card[10];
  11. } suitStruct;
  12.  
  13. typedef struct {//структура карты
  14.     int rank;
  15.     char* suit_card;//массив не работал, пришлось использовать указатель
  16. } Cards;
  17.  
  18. typedef struct {
  19.     Cards hand[2];
  20. } Players;
  21.  
  22. int main() {
  23.     printf("Input players count\n");
  24.     int players_count;
  25.     scanf("%d", &players_count);
  26.     suitStruct spades = { "spades" };
  27.     suitStruct hearts = { "hearts" };
  28.     suitStruct clubs = { "clubs" };
  29.     suitStruct diamonds = { "diamonds" };
  30.     suitStruct suit[SIZE_SUIT] = { spades, hearts, clubs, diamonds };//массив содержит в себе 4 экземляра структуры
  31.     //printf("%s\n", suit[1].suit_for_card); //тест на правильный вывод
  32.  
  33.     int ranks[SIZE_RANK] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };//ранки от 2 до туза(14)
  34.  
  35.     Cards deck[SIZE_DECK];//колода, содержит 52 экземпляра структуры(52 карты)
  36.  
  37.  
  38.     int k = 0;//я до сих пор не понял как это работает, но колода заполняется
  39.     for (int i = 0; i < SIZE_RANK; ++i) {
  40.         for (int j = 0; j < SIZE_SUIT; ++j) {
  41.             deck[k].rank = ranks[i];
  42.             deck[k].suit_card = suit[j].suit_for_card;
  43.             k++;
  44.         }
  45.     }
  46.  
  47.     /*for (int i = 0; i < 52; i++) {//проверка выхода
  48.         printf("%d", deck[i].rank);
  49.         printf(" %s\n", deck[i].suit_card);
  50.     }*/
  51.     srand(time(NULL));
  52.     for (int i = 0; i < SIZE_DECK; i++) {
  53.         int r = 0 + rand() % 52;
  54.         int temp_r = deck[i].rank;
  55.         char* temp_s = deck[i].suit_card;
  56.  
  57.         deck[i].rank = deck[r].rank;
  58.         deck[i].suit_card = deck[r].suit_card;
  59.  
  60.         deck[r].rank = temp_r;
  61.         deck[r].suit_card = temp_s;
  62.     }
  63.  
  64.     for (int i = 0; i < 52; i++) {
  65.         printf("%d", deck[i].rank);
  66.         printf(" %s\n", deck[i].suit_card);
  67.     }
  68.  
  69.     Players* players = (Players*)malloc(players_count * sizeof(Players));//Создания набора игроков
  70.     int index = 0;
  71.     for (int i = 0; i < players_count; i++) {
  72.         for (int j = 0; j < 2; j++) {
  73.             players[i].hand[j].rank = deck[index].rank;
  74.             players[i].hand[j].suit_card = deck[index].suit_card;
  75.             index++;
  76.         }
  77.     }
  78.  
  79.     printf("\n");
  80.     for (int i = 0; i < players_count; i++) {
  81.         printf("Player number: %d\n", i + 1);
  82.         for (int j = 0; j < 2; j++) {
  83.             printf("%d ", players[i].hand[j].rank);
  84.             printf("%s\n", players[i].hand[j].suit_card);
  85.         }
  86.         printf("\n");
  87.     }
  88.  
  89.    
  90.  
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement