back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/linux_client/card_handling.c
blob: 011ad40cbd004eb5a1b3703c87d209afc3f2ca50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "card_handling.h"

#include <stdlib.h>
#include <string.h>

int convert_rank_to_int(const char *card)
{
    int length;
    char str_rank[2];

    length = strlen(card);
    /* 10 - the only one of its kind */
    if(length == 3)
        return 10;

    str_rank[0] = card[0];
    str_rank[1] = '\0';

    switch(card[0]) {
    case 'J':
        return 11;
    case 'Q':
        return 12;
    case 'K':
        return 13;
    case 'A':
        return 14;
    default:
        return strtol(str_rank, NULL, 10);
    }
    return 0;
}

int is_card_beaten(const char *attack_card, const char *defend_card,
                   const char *trump_suit)
{
    int length, attack_rank, defend_rank;
    const char *attack_suit, *defend_suit;

    length = strlen(attack_card);
    attack_suit= attack_card + length - 1;
    length = strlen(defend_card);
    defend_suit = defend_card + length - 1;

    /* suits matched */
    if(!strcmp(attack_suit, defend_suit)) {
        attack_rank = convert_rank_to_int(attack_card);
        defend_rank = convert_rank_to_int(defend_card);
        if(defend_rank > attack_rank)
            return 1;
        /* defender has a trump suit */
    } else if(!strcmp(defend_suit, trump_suit))
        return 1;
    return 0;
}

int check_matched_ranks(const char *attack_card, const char *not_defender_card)
{
    int attack_rank, not_defender_rank;

    attack_rank = convert_rank_to_int(attack_card);
    not_defender_rank = convert_rank_to_int(not_defender_card);
    return attack_rank == not_defender_rank;
}