Quick actions

cmd+k|ctrl+k

Navigation

Languages

lab3 task4

Snippet info

Language

C

Visibility

public

Author

t.m.kozynets

Created

2025-05-07T10:36:03.711431Z

Updated

2025-05-07T10:36:03.711431Z

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define DECK_SIZE 52
#define PLAYERS 3
#define CARDS_PER_PLAYER 9

const char suits[] = {'\x03', '\x04', '\x05', '\x06'}; // ♥ ♦ ♣ ♠

// Ранг від 2 до 14 (2–10, В=11, Д=12, К=13, Т=14)
const char* rankStr(int rank) {
    switch (rank) {
        case 11: return "В";
        case 12: return "Д";
        case 13: return "К";
        case 14: return "Т";
        default: {
            static char buffer[3];
            sprintf(buffer, "%d", rank);
            return buffer;
        }
    }
}

typedef struct {
    int rank; // 2–14
    int suit; // 0–3
} Card;

void createDeck(Card* deck) {
    int index = 0;
    for (int s = 0; s < 4; s++) {
        for (int r = 2; r <= 14; r++) {
            deck[index].rank = r;
            deck[index].suit = s;
            index++;
        }
    }
}

void shuffleDeck(Card* deck) {
    for (int i = 0; i < DECK_SIZE; i++) {
        int j = rand() % DECK_SIZE;
        Card temp = deck[i];
        deck[i] = deck[j];
        deck[j] = temp;
    }
}

void printCard(Card c) {
    printf("%s%c ", rankStr(c.rank), suits[c.suit]);
}

void printPlayerCards(Card* cards) {
    for (int i = 0; i < CARDS_PER_PLAYER; i++) {
        printCard(cards[i]);
    }
    printf("\n");
}

int countStrictConsecutiveSequences(Card* cards) {
    int count = 0;
    for (int i = 0; i <= CARDS_PER_PLAYER - 3; i++) {
        int r1 = cards[i].rank;
        int r2 = cards[i + 1].rank;
        int r3 = cards[i + 2].rank;
        if (r2 == r1 + 1 && r3 == r2 + 1) {
            count++;
            i += 2; // пропускаємо використані карти (не обов'язково, але можна)
        }
    }
    return count;
}

int main() {
    srand(time(NULL));

    Card deck[DECK_SIZE];
    Card players[PLAYERS][CARDS_PER_PLAYER];

    createDeck(deck);
    shuffleDeck(deck);

    // Роздача карт
    int index = 0;
    for (int p = 0; p < PLAYERS; p++) {
        for (int c = 0; c < CARDS_PER_PLAYER; c++) {
            players[p][c] = deck[index++];
        }
    }

    // Виведення результатів
    for (int p = 0; p < PLAYERS; p++) {
        printf("Гравець %d: ", p + 1);
        printPlayerCards(players[p]);
        int seq = countStrictConsecutiveSequences(players[p]);
        printf("Кількість послідовностей: %d\n\n", seq);
    }

    return 0;
}
INFO