Quick actions

cmd+k|ctrl+k

Navigation

Languages

Лабораторна 3 завдання 4

Snippet info

Language

C

Visibility

public

Author

liestayalive

Created

2024-05-26T12:27:19.617657Z

Updated

2024-05-26T12:27:19.617657Z

/**
* @file lab3_4.c
* @author Нездоймишапка А.О., гр. 515
* @date 03 березня 2023
* @brief Лабораторна робота № 3
*
* Використання масивів. Завдання 4
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <locale.h>

#define NUM_PLAYERS 4
#define NUM_CARDS_PER_PLAYER 6
#define NUM_CARDS 52

// Константи для мастей і чинів
const char* suits[] = { "\x03", "\x04", "\x05", "\x06" };  // Hearts, Diamonds, Clubs, Spades
const char* ranks[] = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };

// Прототипи функцій
void create_deck(char deck[][4]);
void shuffle_deck(char deck[][4]);
void deal_cards(char deck[][4], char players_hands[][NUM_CARDS_PER_PLAYER][4]);
bool check_flush(char hand[][4]);

int main() {
    setlocale(LC_ALL, "ukr");
    char deck[NUM_CARDS][4];
    char players_hands[NUM_PLAYERS][NUM_CARDS_PER_PLAYER][4];

    srand(time(NULL));  // Ініціалізація випадкового початкового числа

    create_deck(deck);
    shuffle_deck(deck);
    deal_cards(deck, players_hands);

    for (int i = 0; i < NUM_PLAYERS; i++) {
        printf("Карти %d гравця: ", i + 1);
        for (int j = 0; j < NUM_CARDS_PER_PLAYER; j++) {
            printf("%s ", players_hands[i][j]);
        }
        printf("\n");

        if (check_flush(players_hands[i])) {
            printf("Гравець %d має терц!\n", i + 1);
        }
        else {
            printf("Гравець %d не має терца.\n", i + 1);
        }
        printf("\n");
    }

    return 0;
}

void create_deck(char deck[][4]) {
    int index = 0;
    for (int suit = 0; suit < 4; suit++) {
        for (int rank = 0; rank < 13; rank++) {
            deck[index][0] = ranks[rank][0];
            deck[index][1] = suits[suit][0];
            deck[index][2] = suits[suit][1];  // Це для спеціального символу
            deck[index][3] = '\0';  // Завершити рядок нулем
            index++;
        }
    }
}

void shuffle_deck(char deck[][4]) {
    for (int i = NUM_CARDS - 1; i > 0; i--) {
        int j = rand() % (i + 1);
        char temp[4];
        // Поміняти місцями [i] i deck[j]
        for (int k = 0; k < 4; k++) {
            temp[k] = deck[i][k];
            deck[i][k] = deck[j][k];
            deck[j][k] = temp[k];
        }
    }
}

void deal_cards(char deck[][4], char players_hands[][NUM_CARDS_PER_PLAYER][4]) {
    int card_index = 0;
    for (int player = 0; player < NUM_PLAYERS; player++) {
        for (int card = 0; card < NUM_CARDS_PER_PLAYER; card++) {
            for (int char_index = 0; char_index < 4; char_index++) {
                players_hands[player][card][char_index] = deck[card_index][char_index];
            }
            card_index++;
        }
    }
}

bool check_flush(char hand[][4]) {
    int suits_count[4] = { 0 };
    for (int card = 0; card < NUM_CARDS_PER_PLAYER; card++) {
        char suit = hand[card][1];  // Отримай гідність
        if (suit == '\x03') suits_count[0]++;
        else if (suit == '\x04') suits_count[1]++;
        else if (suit == '\x05') suits_count[2]++;
        else if (suit == '\x06') suits_count[3]++;
    }

    for (int i = 0; i < 4; i++) {
        if (suits_count[i] >= 3) {
            return true;
        }
    }
    return false;
}
INFO