From 9780e8c24b45cfb47085d77ae64ba1f91d31419e Mon Sep 17 00:00:00 2001 From: Mario Kilies Date: Tue, 25 Jan 2011 15:50:27 +0100 Subject: Implemented card_stack_push(). Renamed card_stack_clear() to card_stack_replace() and card_stack_upper_card() to card_stack_top(). Added documentation. --- src/card_stack.c | 40 +++++++++++++++++++++++++++++++++++----- src/card_stack.h | 5 +++-- src/client_game_states.c | 15 +++------------ src/table_stacks.c | 2 +- 4 files changed, 42 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/card_stack.c b/src/card_stack.c index 13b14dd..f3bd217 100644 --- a/src/card_stack.c +++ b/src/card_stack.c @@ -2,6 +2,10 @@ #include #include +/** + * Calculates the points of a card stack. This will be the sum of the points of all cards contained in the card stack. + * @param[in] cs The card stack fir which the points will be calculated +*/ uint32_t card_stack_get_points(const card_stack_t *cs) { assert(cs != NULL); @@ -17,11 +21,15 @@ uint32_t card_stack_get_points(const card_stack_t *cs) return points; } -const card card_stack_upper_card(const card_stack_t *cs) +/** + * Determines the uppermost card on a card stack. The card will not be removed from the stack. + * @param[in] cs The card stack from which the uppermost card will be retrieved +*/ +const card card_stack_top(const card_stack_t *cs) { assert(cs != NULL); - for(int i=0; icards[MAX_CARD_STACK_SIZE-1-i]; if(cur != 0) @@ -31,13 +39,35 @@ const card card_stack_upper_card(const card_stack_t *cs) return 0; } -void card_stack_clear(card_stack_t *cs, const card new_card) +/** + * Places a card on top of a card stack. + * @param[in] cs The card stack to place the card on + * @param[in] c The card to place +*/ +void card_stack_push(card_stack_t *cs, const card c) { assert(cs != NULL); - for(int i=0; icards[i] != 0) + continue; + cs->cards[i] = c; + break; + } +} + +/** + * Replaces a card stack with a single card. All cards within the card stack will be removed adn the first card will be set to a given card. + * @param[in] cs The card stack to replace + * @param[in] new_card The new first card +*/ +void card_stack_replace(card_stack_t *cs, const card new_card) +{ + assert(cs != NULL); + + for(int i = 0; i < MAX_CARD_STACK_SIZE; i++) cs->cards[i] = 0; cs->cards[0] = new_card; } - diff --git a/src/card_stack.h b/src/card_stack.h index 61d2bf2..7e1d976 100644 --- a/src/card_stack.h +++ b/src/card_stack.h @@ -12,7 +12,8 @@ typedef struct } card_stack_t; uint32_t card_stack_get_points(const card_stack_t *cs); -const card card_stack_upper_card(const card_stack_t *cs); -void card_stack_clear(card_stack_t *cs, const card new_card); +const card card_stack_top(const card_stack_t *cs); +void card_stack_push(card_stack_t *cs, const card c); +void card_stack_replace(card_stack_t *cs, const card new_card); #endif // OXEN_CARD_STACK_H diff --git a/src/client_game_states.c b/src/client_game_states.c index 142693e..2b11d51 100644 --- a/src/client_game_states.c +++ b/src/client_game_states.c @@ -80,7 +80,7 @@ game_state_t state_client_play_cards(const int sock, const uint8_t round) net_recv(sock, msg_type_selected_stack_s); ds->player_list.players[i].score += card_stack_get_points(&ds->table_stacks.stacks[ds->stack_index]); - card_stack_clear(&ds->table_stacks.stacks[ds->stack_index], c); + card_stack_replace(&ds->table_stacks.stacks[ds->stack_index], c); } else // card fits on a stack -> place it { @@ -88,19 +88,10 @@ game_state_t state_client_play_cards(const int sock, const uint8_t round) if(cs->cards[MAX_CARD_STACK_SIZE-1] != 0) // stack is full { ds->player_list.players[i].score += card_stack_get_points(cs); - card_stack_clear(cs, c); + card_stack_replace(cs, c); } else - { - // put open card on top of stack - for(int j=0; jcards[j] != 0) - continue; - cs->cards[j] = c; - break; - } - } + card_stack_push(cs, c); } } #if 0 diff --git a/src/table_stacks.c b/src/table_stacks.c index 9a32977..7f76edd 100644 --- a/src/table_stacks.c +++ b/src/table_stacks.c @@ -14,7 +14,7 @@ const uint8_t get_stack_idx_for_card(const table_stacks_t* stack_list, const car for(int i=0; istacks[i]); + card stackcard = card_stack_top(&stack_list->stacks[i]); uint8_t new_diff = c - stackcard; assert(new_diff != 0); -- cgit v1.2.3