/*
* @file lab3_4.c
* @author Герасимюк Д.В., гр. 515і1
* @date 29 березня 2024
* @brief Лабораторна робота №3, варіант 3
* Використання масивів. Завдання 4
*/
#include "struct.h"
#include "cards.h"
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#define NUM_RANKS 13
#define NUM_SUITS 4
#define NUM_CARDS 52
#define NUM_PLAYERS 4
int main() {
setlocale(LC_ALL, "Ukr");
Card deck[NUM_CARDS];
Card hands[NUM_PLAYERS][6];
koloda(deck);
zdachaKart(deck, hands, NUM_PLAYERS);
DribniCards(hands, NUM_PLAYERS);
return 0;
}
/*
* @file cards.h
* @author Герасимюк Д.В., гр. 515і1
* @date 29 березня 2024
* @brief Лабораторна робота №3, варіант 3
* Використання масивів. Завдання 4
*/
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM_RANKS 13
#define NUM_SUITS 4
#define NUM_CARDS 52
#define NUM_PLAYERS 4
// генерація колоди
void koloda(Card deck[]) {
char masti[NUM_SUITS] = { '\x03', '\x04', '\x05', '\x06' }; // масті
char ranks[NUM_RANKS][3] = { "2", "3", "4", "5", "6", "7", "8", "9", "t", "J", "Q", "K", "A" };
int card_count = 0;
for (int suit = 0; suit < NUM_SUITS; suit++) {
for (int rank = 0; rank < NUM_RANKS; rank++) {
deck[card_count].suit = masti[suit];
deck[card_count].rank = *ranks[rank];
card_count++;
}
}
}
// здача карт гравцям
void zdachaKart(Card deck[], Card hands[][6], int num_players) {
srand(time(NULL)); // rand
int cards_per_player = NUM_CARDS / num_players;
for (int card = 0; card < NUM_CARDS; card++) {
int random_index = rand() % (NUM_CARDS - card) + card;
Card temp = deck[card];
deck[card] = deck[random_index];
deck[random_index] = temp;
}
for (int player = 0; player < num_players; player++) {
for (int card = 0; card < 6; card++) {
hands[player][card] = deck[player * cards_per_player + card];
}
}
}
// Кількість дрібних карт у кожного з гравців
void DribniCards(Card hands[][6], int num_players) {
char masti_symbols[NUM_SUITS] = { '\x03', '\x04', '\x05', '\x06' }; // масті
int* dribni_card_counts = (int*)malloc(num_players * sizeof(int));
if (dribni_card_counts == NULL) {
printf("Помилка виділення пам'яті.\n");
return;
}
for (int i = 0; i < num_players; i++) {
dribni_card_counts[i] = 0;
}
for (int player = 0; player < num_players; player++) {
printf("Гравець %d: ", player + 1);
for (int card = 0; card < 6; card++) {
printf("%c%c ", hands[player][card].rank, hands[player][card].suit);
if (hands[player][card].rank >= '2' && hands[player][card].rank <= '9') {
dribni_card_counts[player]++;
}
else if (hands[player][card].rank == 't') {
dribni_card_counts[player]++;
}
}
printf("\nКількість \"дрібних\" карт у гравця %d: %d\n\n", player + 1, dribni_card_counts[player]);
}
free(dribni_card_counts);
}
/*
* @file struct.h
* @author Герасимюк Д.В., гр. 515і1
* @date 29 березня 2024
* @brief Лабораторна робота №3, варіант 3
* Використання масивів. Завдання 4
*/
#pragma once
#define NUM_SUITS 4
#define NUM_RANKS 13
typedef struct {
char suit;
char rank;
} Card;