back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/windows_client/card_stack.c
diff options
context:
space:
mode:
Diffstat (limited to 'windows_client/card_stack.c')
-rw-r--r--windows_client/card_stack.c173
1 files changed, 173 insertions, 0 deletions
diff --git a/windows_client/card_stack.c b/windows_client/card_stack.c
new file mode 100644
index 0000000..fc4057c
--- /dev/null
+++ b/windows_client/card_stack.c
@@ -0,0 +1,173 @@
+#include "card_stack.h"
+#include "card_handling.h"
+#include "client.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+void init_stack(player_cards *deck)
+{
+ *deck = NULL;
+}
+
+void push_stack(player_cards *deck, char *str)
+{
+ struct card_stack_item *tmp = malloc(sizeof(struct card_stack_item));
+ tmp->str = str;
+ tmp->tip = ' ';
+ tmp->is_usable = 0;
+ tmp->next = *deck;
+ *deck = tmp;
+}
+
+void clear_stack(player_cards *deck)
+{
+ struct card_stack_item *tmp;
+
+ while(*deck) {
+ tmp = *deck;
+ *deck = tmp->next;
+ if(tmp->str)
+ free(tmp->str);
+ free(tmp);
+ }
+}
+
+int is_empty_stack(player_cards deck)
+{
+ return deck == NULL;
+}
+
+void add_hint_letters_stack(player_cards deck)
+{
+ unsigned char letter = 'a';
+
+ while(deck) {
+ deck->tip = letter;
+ ++letter;
+ if(letter > 'z')
+ letter = 'A';
+ deck = deck->next;
+ }
+}
+
+static int check_no_attackers_cards_marked(player_cards deck)
+{
+ while(deck) {
+ if(deck->is_usable)
+ return 0;
+ deck = deck->next;
+ }
+ return 1;
+}
+
+static void mark_all_card_stack(player_cards deck)
+{
+ player_cards tmp_deck = deck;
+
+ while(tmp_deck) {
+ tmp_deck->is_usable = 1;
+ tmp_deck = tmp_deck->next;
+ }
+}
+
+static void clear_is_usable_status(player_cards deck)
+{
+ while(deck) {
+ if(deck->is_usable)
+ deck->is_usable = 0;
+ deck = deck->next;
+ }
+}
+
+void mark_card_for_attackers_stack(player_cards deck)
+{
+ char *card = NULL, *found_card;
+ struct card_stack_item *search_deck = NULL;
+ int target_rank, found_rank;
+ player_cards tmp_begin_deck = deck;
+
+ clear_is_usable_status(deck);
+ while(deck) {
+ /* is the card already marked? */
+ if(deck->is_usable) {
+ deck = deck->next;
+ continue;
+ }
+ card = deck->str;
+ target_rank = convert_rank_to_int(card);
+ search_deck = deck->next;
+ while(search_deck) {
+ if(search_deck->is_usable) {
+ search_deck = search_deck->next;
+ continue;
+ }
+ found_card = search_deck->str;
+ found_rank = convert_rank_to_int(found_card);
+ if(found_rank == target_rank) {
+ deck->is_usable = 1;
+ search_deck->is_usable = 1;
+ }
+ search_deck = search_deck->next;
+ }
+ deck = deck->next;
+ }
+ if(check_no_attackers_cards_marked(tmp_begin_deck))
+ mark_all_card_stack(tmp_begin_deck);
+}
+
+void mark_card_for_defenders_stack(player_cards deck,
+ struct cards_on_table *cot, char *trump_suit)
+{
+ int i;
+
+ while(deck) {
+ for(i = 0; i <= cot->card_arr_idx; ++i)
+ if(((i == 0 || !(i % 3)) && cot->card_arr[i+2][0] == '-') &&
+ is_card_beaten(cot->card_arr[i], deck->str, trump_suit))
+ {
+ deck->is_usable = 1;
+ break;
+ }
+ deck = deck->next;
+ }
+}
+
+void mark_card_for_tossing_stack(player_cards deck, struct cards_on_table *cot)
+{
+ int i;
+
+ while(deck) {
+ for(i = 0; i <= cot->card_arr_idx; ++i) {
+ if(cot->card_arr[i][0] == '-' || cot->card_arr[i][0] == '\\')
+ continue;
+ if(check_matched_ranks(cot->card_arr[i], deck->str)) {
+ deck->is_usable = 1;
+ break;
+ }
+ }
+ deck = deck->next;
+ }
+}
+
+char* card_search_by_marked_letter(player_cards deck, int letter)
+{
+ while(deck) {
+ if(deck->tip == letter && deck->is_usable) {
+ deck->is_usable = 0;
+ return deck->str;
+ }
+ deck = deck->next;
+ }
+ return NULL;
+}
+
+char* card_search_by_unmarked_letter(player_cards deck, int letter)
+{
+ while(deck) {
+ if(deck->tip == letter)
+ return deck->str;
+ deck = deck->next;
+ }
+ return NULL;
+}