Quick actions

cmd+k|ctrl+k

Navigation

Languages

lb3_4

Snippet info

Language

C

Visibility

public

Author

amos11520

Created

2023-05-09T22:21:00.747341Z

Updated

2023-05-10T18:46:47.437228Z

/**
* @file main.c
* @author Мосяж А.М., гр. 515б
* @date 26 квітня 2023
* @brief Лабораторна робота № 3, варіант 4
* Робота з масивами Завдання 4
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

const char* ranks[] = { "2","3","4","5","6","7","8","9","10","J","D","K","A" };
//const char suits[] = { '\x03','\x04','\x05','\x06' };
const char suits[] = {'w','+','*','='};
char cardname[4];

//вертає карту складенe з ranks і suits
char* getcard(char c)
{
    sprintf(cardname, "%s%c", ranks[c % 13], suits[c / 13]);
    return cardname;
}

//заповнює колоду карт кодами карт
//перемішує її
void shuffle_cards(char* cards, int p)
{
    int i, j;
    char temp;
    for (int i = 0;i < 52;i++)
        cards[i] = i;

    for (int k = 0; k < 99 + p; k++)
    {
        i = rand() % 52;
        j = rand() % 52;
        temp = cards[i];
        cards[i] = cards[j];
        cards[j] = temp;
    }
}

/*void rozdaty(char*koloda, char plr[3][9])
{
  int k=0;
  for(int i=0; i<3; i++)
    for(int j=0; j<9; j++)
      plr[i][j]=koloda[k++];
}*/

//роздає гравцю перші 9 карт від позиції topcard
int rozdaty1(char* koloda, char plr[9], int topcard)
{ //topcard==0; -  колода повна
    for (int j = 0; j < 9; j++)
        plr[j] = koloda[topcard++];
    return topcard;
}

//перевіря чи три карти йдуть(можуть іти) в порядку зростання
int check3(char a, char b, char c)
{

    a = a % 13;b = b % 13;c = c % 13;
    return (c - b == 1 && b - a == 1);
}

void swap(char* a, char* b)
{
    char t = *a;*a = *b;*b = t;
}
void sort_cards(char h[9])
{
    int flag = 1;
    while (flag)
    {
        flag = 0;
        for (int j = 0; j < 9 - 1; j++)
            if (h[j] % 13 > h[j + 1] % 13)
            {
                swap(&h[j], &h[j + 1]);
                flag = 1;
            }
    }
}

int count_triplet(char h[9])
{
    int k = 0, j = 0;
    while (j < 9 - 2)
    {
        if (check3(h[j], h[j + 1], h[j + 2]))
        {
            k++;
            j += 3;
        }
        else
            j++;
    }
    return k;
}

int main()
{
    srand(time(0));
    char koloda[52];
    char player[3][9];
    int top = 0;
    //премішаємо карти
    shuffle_cards(koloda, 8);

    //rozdaty(koloda, player);
    //роздамо першому гравцю
    top = rozdaty1(koloda, player[0], top);
    sort_cards(player[0]);
    //роздамо другому гравцю
    top = rozdaty1(koloda, player[1], top);
    sort_cards(player[1]);
    //роздамо третьому гравцю
    top = rozdaty1(koloda, player[2], top);
    sort_cards(player[2]);

    //sort_cards(player);

    for (int i = 0; i < 3; i++)
    {
        printf("\n player %d hands ", i + 1);
        for (int j = 0; j < 9; j++)
            printf("%s ", getcard(player[i][j]));
        printf("- %d triplets", count_triplet(player[i]));
    }
}
INFO