Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define SIZE_RANK 13
- #define SIZE_SUIT 4
- #define SIZE_DECK 52
- typedef struct {//структура масти карты, используется для последующего помещения каждой масти в массив
- char suit_for_card[10];
- } suitStruct;
- typedef struct {//структура карты
- int rank;
- char* suit_card;//массив не работал, пришлось использовать указатель
- } Cards;
- typedef struct {
- Cards hand[2];
- } Players;
- int main() {
- printf("Input players count\n");
- int players_count;
- scanf("%d", &players_count);
- suitStruct spades = { "spades" };
- suitStruct hearts = { "hearts" };
- suitStruct clubs = { "clubs" };
- suitStruct diamonds = { "diamonds" };
- suitStruct suit[SIZE_SUIT] = { spades, hearts, clubs, diamonds };//массив содержит в себе 4 экземляра структуры
- //printf("%s\n", suit[1].suit_for_card); //тест на правильный вывод
- int ranks[SIZE_RANK] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };//ранки от 2 до туза(14)
- Cards deck[SIZE_DECK];//колода, содержит 52 экземпляра структуры(52 карты)
- int k = 0;//я до сих пор не понял как это работает, но колода заполняется
- for (int i = 0; i < SIZE_RANK; ++i) {
- for (int j = 0; j < SIZE_SUIT; ++j) {
- deck[k].rank = ranks[i];
- deck[k].suit_card = suit[j].suit_for_card;
- k++;
- }
- }
- /*for (int i = 0; i < 52; i++) {//проверка выхода
- printf("%d", deck[i].rank);
- printf(" %s\n", deck[i].suit_card);
- }*/
- srand(time(NULL));
- for (int i = 0; i < SIZE_DECK; i++) {
- int r = 0 + rand() % 52;
- int temp_r = deck[i].rank;
- char* temp_s = deck[i].suit_card;
- deck[i].rank = deck[r].rank;
- deck[i].suit_card = deck[r].suit_card;
- deck[r].rank = temp_r;
- deck[r].suit_card = temp_s;
- }
- for (int i = 0; i < 52; i++) {
- printf("%d", deck[i].rank);
- printf(" %s\n", deck[i].suit_card);
- }
- Players* players = (Players*)malloc(players_count * sizeof(Players));//Создания набора игроков
- int index = 0;
- for (int i = 0; i < players_count; i++) {
- for (int j = 0; j < 2; j++) {
- players[i].hand[j].rank = deck[index].rank;
- players[i].hand[j].suit_card = deck[index].suit_card;
- index++;
- }
- }
- printf("\n");
- for (int i = 0; i < players_count; i++) {
- printf("Player number: %d\n", i + 1);
- for (int j = 0; j < 2; j++) {
- printf("%d ", players[i].hand[j].rank);
- printf("%s\n", players[i].hand[j].suit_card);
- }
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement