diff options
author | scratko <m@scratko.xyz> | 2024-08-30 12:46:56 +0300 |
---|---|---|
committer | scratko <m@scratko.xyz> | 2024-08-30 14:59:44 +0300 |
commit | 831f9f01fbe4088eb6bd378c0e417d9996b676fd (patch) | |
tree | 53297459d35ad795618c351a79b1829776e5e1f4 /linux_client/card_handling.c | |
parent | 4b6c15f780d59895f067383a5041edcfe86f504e (diff) | |
download | durak-831f9f01fbe4088eb6bd378c0e417d9996b676fd.tar.gz durak-831f9f01fbe4088eb6bd378c0e417d9996b676fd.tar.bz2 durak-831f9f01fbe4088eb6bd378c0e417d9996b676fd.zip |
Final version v2.0
Added windows client.
SIGPIPE signal was being sent to the server when the client was disconnected.
Now there is handling of this signal.
Added a delay when displaying some informational messages.
Diffstat (limited to 'linux_client/card_handling.c')
-rw-r--r-- | linux_client/card_handling.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/linux_client/card_handling.c b/linux_client/card_handling.c new file mode 100644 index 0000000..011ad40 --- /dev/null +++ b/linux_client/card_handling.c @@ -0,0 +1,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; +} |