diff options
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; +} |