back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/windows_client/card_stack.c
blob: fc4057caa6c49820b10bb6052442847579b74588 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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;
}