From 4b9e56ebf47db8be740f4ff2a4b9fceb9f143055 Mon Sep 17 00:00:00 2001 From: Mario Kilies Date: Sun, 16 Jan 2011 08:41:04 +0100 Subject: Refactored cardstack and tablestacks to card_stack_t and table_stacks_t, that both now are defined as structs rather than arrays. Inserted lots of assert() calls to functions that take pointers as arguments. --- src/card.c | 1 + src/card_stack.c | 18 ++++++++++++++++++ src/card_stack.h | 16 ++++++++++++++++ src/cardstack.c | 14 -------------- src/cardstack.h | 13 ------------- src/game.c | 17 ++++++++++++----- src/hand.c | 1 + src/net.c | 3 +-- src/net_client.c | 10 +++++++++- src/net_server.c | 10 +++++++++- src/player.c | 4 +++- src/pnoc.c | 3 +++ src/table_stacks.h | 13 +++++++++++++ src/tablestacks.h | 10 ---------- src/ui.c | 50 ++++++++++++++++++++++++++++++++++---------------- src/ui.h | 9 ++++----- 16 files changed, 124 insertions(+), 68 deletions(-) create mode 100644 src/card_stack.c create mode 100644 src/card_stack.h delete mode 100644 src/cardstack.c delete mode 100644 src/cardstack.h create mode 100644 src/table_stacks.h delete mode 100644 src/tablestacks.h diff --git a/src/card.c b/src/card.c index 73808a4..25db247 100644 --- a/src/card.c +++ b/src/card.c @@ -4,6 +4,7 @@ uint32_t card_get_points(const card c) { assert(c > 0 && c <= 105); + if (55 == c) return 7; // Card 55 is worth of 7 points if (c % 11 == 0) return 5; // Cards that are multiples of 11 are worth 5 points if (c % 10 == 0) return 3; // ... 10 are worth 3 points diff --git a/src/card_stack.c b/src/card_stack.c new file mode 100644 index 0000000..206a9d8 --- /dev/null +++ b/src/card_stack.c @@ -0,0 +1,18 @@ +#include "card_stack.h" +#include +#include + +uint32_t card_stack_get_points(const card_stack_t *cs) +{ + assert(cs != NULL); + + uint32_t points = 0; + + for(uint8_t i = 0; i < MAX_CARD_STACK_SIZE; i++) + { + if(cs->cards[i] > 0) + points += card_get_points(cs->cards[i]); + } + + return points; +} diff --git a/src/card_stack.h b/src/card_stack.h new file mode 100644 index 0000000..86ec526 --- /dev/null +++ b/src/card_stack.h @@ -0,0 +1,16 @@ +#ifndef OXEN_CARD_STACK_H +#define OXEN_CARD_STACK_H + +#include +#include "card.h" + +#define MAX_CARD_STACK_SIZE 5 + +typedef struct +{ + card cards[MAX_CARD_STACK_SIZE]; +} card_stack_t; + +uint32_t card_stack_get_points(const card_stack_t *cs); + +#endif // OXEN_CARD_STACK_H diff --git a/src/cardstack.c b/src/cardstack.c deleted file mode 100644 index 66cede9..0000000 --- a/src/cardstack.c +++ /dev/null @@ -1,14 +0,0 @@ -#include "cardstack.h" - -uint32_t cardstack_get_points(const cardstack cs) -{ - uint32_t points = 0; - - for(uint8_t i = 0; i < MAX_CARDSTACK_SIZE; i++) - { - if(cs[i] > 0) - points += card_get_points(cs[i]); - } - - return points; -} diff --git a/src/cardstack.h b/src/cardstack.h deleted file mode 100644 index 119bd4d..0000000 --- a/src/cardstack.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef OXEN_CARDSTACK_H -#define OXEN_CARDSTACK_H - -#include -#include "card.h" - -#define MAX_CARDSTACK_SIZE 5 - -typedef card cardstack[MAX_CARDSTACK_SIZE]; - -uint32_t cardstack_get_points(const cardstack cs); - -#endif // OXEN_CARDSTACK_H diff --git a/src/game.c b/src/game.c index 96a987c..d872bca 100644 --- a/src/game.c +++ b/src/game.c @@ -9,8 +9,10 @@ #include #include "ui.h" -void init_mainstack(card* stack, const uint32_t size) +void init_mainstack(card *stack, const uint32_t size) { + assert(stack != NULL); + // assign card values to main stack for(uint32_t i=0, val=MIN_CARD; icards, MAX_HAND_CARDS, sizeof(card), hand_comparator); } diff --git a/src/net.c b/src/net.c index ed7e69f..44c9e9a 100644 --- a/src/net.c +++ b/src/net.c @@ -5,7 +5,7 @@ #include #include "net.h" -void* net_recv(int sock, msg_type_t type) +void *net_recv(int sock, msg_type_t type) { msg_t m; void *result; @@ -44,4 +44,3 @@ void* net_recv(int sock, msg_type_t type) return result; } - diff --git a/src/net_client.c b/src/net_client.c index a225529..895041e 100644 --- a/src/net_client.c +++ b/src/net_client.c @@ -16,6 +16,8 @@ */ void client_hello(int sock, const char* username) { + assert(username != NULL); + uint8_t* buf; uint8_t namelen = strlen(username); @@ -43,6 +45,9 @@ void client_hello(int sock, const char* username) */ int client_connect_server(const char* host, const char* port) { + assert(host != NULL); + assert(port != NULL); + int status; int sock; struct addrinfo hints, *result, *tmp; @@ -85,6 +90,8 @@ int client_connect_server(const char* host, const char* port) player_list* client_recv_player_list(const uint8_t* payload, const uint8_t data_len) { + assert(payload != NULL); + player_list* players; uint32_t pos = 0; @@ -112,6 +119,8 @@ player_list* client_recv_player_list(const uint8_t* payload, const uint8_t data_ hand_t *client_recv_deal_cards(const uint8_t* payload, const uint8_t payload_len) { + assert(payload != NULL); + hand_t *h = malloc(sizeof(hand_t)); assert(payload_len == MAX_HAND_CARDS); // deal_cards packet have fixed size @@ -123,4 +132,3 @@ hand_t *client_recv_deal_cards(const uint8_t* payload, const uint8_t payload_len return h; } - diff --git a/src/net_server.c b/src/net_server.c index 1b60eec..a0dc0e2 100644 --- a/src/net_server.c +++ b/src/net_server.c @@ -16,6 +16,8 @@ */ int server_start(const char* port) { + assert(port != NULL); + int status; int serversock; struct addrinfo hints, *result, *tmp; @@ -119,6 +121,9 @@ int* server_get_players(int serversock, const uint8_t count) */ void server_start_game(int* clients, const uint8_t clientcount, const player_list* players) { + assert(clients != NULL); + assert(players != NULL); + uint8_t* buf; uint8_t usercount = players->count; uint32_t pos; @@ -160,6 +165,8 @@ void server_start_game(int* clients, const uint8_t clientcount, const player_lis */ char* server_recv_hello(const uint8_t* payload, const uint8_t payload_len) { + assert(payload != NULL); + char* name; name = malloc(payload_len+1); @@ -182,6 +189,8 @@ char* server_recv_hello(const uint8_t* payload, const uint8_t payload_len) */ void server_deal_cards(int sock, const hand_t *h) { + assert(h != NULL); + uint8_t buf[2+MAX_HAND_CARDS]; buf[NET_MSG_OFFSET_TYPE] = msg_type_deal_cards; @@ -191,4 +200,3 @@ void server_deal_cards(int sock, const hand_t *h) send(sock, buf, 2+MAX_HAND_CARDS, 0); } - diff --git a/src/player.c b/src/player.c index ee21245..28914d4 100644 --- a/src/player.c +++ b/src/player.c @@ -1,6 +1,7 @@ #include "player.h" #include #include +#include player_list* create_playerlist(void) { @@ -12,8 +13,9 @@ player_list* create_playerlist(void) void cleanup_playerlist(player_list* pl) { + assert(pl != NULL); + for(int i=0; inames[i]); free(pl); } - diff --git a/src/pnoc.c b/src/pnoc.c index 0bfacad..387f127 100644 --- a/src/pnoc.c +++ b/src/pnoc.c @@ -1,4 +1,5 @@ #include "pnoc.h" +#include #include static int pnoc_comparator(const void *a, const void *b) @@ -11,5 +12,7 @@ static int pnoc_comparator(const void *a, const void *b) void pnoc_sort(pnoc_t pnoc[], const uint8_t num_tuples) { + assert(pnoc != NULL); + qsort(pnoc, num_tuples, sizeof(pnoc_t), pnoc_comparator); } diff --git a/src/table_stacks.h b/src/table_stacks.h new file mode 100644 index 0000000..57fddba --- /dev/null +++ b/src/table_stacks.h @@ -0,0 +1,13 @@ +#ifndef OXEN_TABLE_STACKS_H +#define OXEN_TABLE_STACKS_H + +#include "card_stack.h" + +#define NUM_TABLE_STACKS 4 + +typedef struct +{ + card_stack_t stacks[NUM_TABLE_STACKS]; +} table_stacks_t; + +#endif // OXEN_TABLE_STACKS_H diff --git a/src/tablestacks.h b/src/tablestacks.h deleted file mode 100644 index 045ece3..0000000 --- a/src/tablestacks.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef OXEN_TABLESTACKS_H -#define OXEN_TABLESTACKS_H - -#include "cardstack.h" - -#define NUM_TABLESTACKS 4 - -typedef cardstack tablestacks[NUM_TABLESTACKS]; - -#endif // OXEN_TABLESTACKS_H diff --git a/src/ui.c b/src/ui.c index a42ecf4..406ecb3 100644 --- a/src/ui.c +++ b/src/ui.c @@ -1,5 +1,6 @@ #include "ui.h" -#include "cardstack.h" +#include +#include "card_stack.h" // Definition of ncurses color pair identifiers #define CP_WHITE_ON_BLACK 1 @@ -24,6 +25,8 @@ static WINDOW *w_hand_cards; static void draw_card(WINDOW *w, const uint8_t row, const uint8_t col, const bool highlight, const card c) { + assert(w != NULL); + unsigned color_pair = 0; // If card is 0, draw a placeholder (empty space) instead of a card. This is used to remove a card that does not longer exist but has visible remains on the screen. @@ -123,8 +126,11 @@ static void draw_card(WINDOW *w, const uint8_t row, const uint8_t col, const boo wattroff(w, A_BOLD); } -static void draw_cardstack(WINDOW *w, const uint8_t row, const uint8_t col, const bool highlight, const cardstack cs) +static void draw_card_stack(WINDOW *w, const uint8_t row, const uint8_t col, const bool highlight, const card_stack_t *cs) { + assert(w != NULL); + assert(cs != NULL); + // Clear old card stack first mvwhline(w, row, col, ' ', 15); mvwhline(w, row+1, col, ' ', 15); @@ -132,11 +138,11 @@ static void draw_cardstack(WINDOW *w, const uint8_t row, const uint8_t col, cons mvwhline(w, row+3, col, ' ', 15); mvwhline(w, row+4, col, ' ', 15); - for (uint8_t i = 0; i < MAX_CARDSTACK_SIZE; i++) + for (uint8_t i = 0; i < MAX_CARD_STACK_SIZE; i++) { - if (cs[i] != 0) + if (cs->cards[i] != 0) { - draw_card(w, row, i*2, highlight, cs[i]); + draw_card(w, row, i*2, highlight, cs->cards[i]); } } } @@ -147,21 +153,23 @@ static void draw_cardstack(WINDOW *w, const uint8_t row, const uint8_t col, cons * @param[in] highlight If true, a stack will be highlighted * @param[in] highlighted_stack The stack to highlight. Only used, if highlight is true */ -void ui_display_wnd_table_cards(const tablestacks ts, const bool highlight, const uint8_t highlighted_stack) +void ui_display_wnd_table_cards(const table_stacks_t *ts, const bool highlight, const uint8_t highlighted_stack) { + assert(ts != NULL); + wattron(w_table_cards, A_BOLD); mvwprintw(w_table_cards, 0, 2, "Table Cards:"); wattroff(w_table_cards, A_BOLD); - for (uint8_t i = 0; i < NUM_TABLESTACKS; i++) + for (uint8_t i = 0; i < NUM_TABLE_STACKS; i++) { if (highlight && i == highlighted_stack) { - draw_cardstack(w_table_cards, 1 + i*5, 0, true, ts[i]); + draw_card_stack(w_table_cards, 1 + i*5, 0, true, &ts->stacks[i]); continue; } - draw_cardstack(w_table_cards, 1 + i*5, 0, false, ts[i]); + draw_card_stack(w_table_cards, 1 + i*5, 0, false, &ts->stacks[i]); } wrefresh(w_table_cards); @@ -173,23 +181,25 @@ void ui_display_wnd_table_cards(const tablestacks ts, const bool highlight, cons * @param[in] highlight If true, stack poins will be highlighted * @param[in] highlighted_points The stack points to highlight. Only used, if highlight is true */ -void ui_display_wnd_stack_points(const tablestacks ts, const bool highlight, const uint8_t highlighted_points) +void ui_display_wnd_stack_points(const table_stacks_t *ts, const bool highlight, const uint8_t highlighted_points) { + assert(ts != NULL); + wattron(w_stack_points, A_BOLD); mvwprintw(w_stack_points, 0, 0, "Pts:"); wattroff(w_stack_points, A_BOLD); - for (uint8_t i = 0; i < NUM_TABLESTACKS; i++) + for (uint8_t i = 0; i < NUM_TABLE_STACKS; i++) { if (highlight && i == highlighted_points) { wattron(w_stack_points, A_BOLD); - mvwprintw(w_stack_points, 3 + i*5, 1, "%2d", cardstack_get_points(ts[i])); + mvwprintw(w_stack_points, 3 + i*5, 1, "%2d", card_stack_get_points(&ts->stacks[i])); wattroff(w_stack_points, A_BOLD); continue; } - mvwprintw(w_stack_points, 3 + i*5, 1, "%2d", cardstack_get_points(ts[i])); + mvwprintw(w_stack_points, 3 + i*5, 1, "%2d", card_stack_get_points(&ts->stacks[i])); } wrefresh(w_stack_points); @@ -204,6 +214,8 @@ void ui_display_wnd_stack_points(const tablestacks ts, const bool highlight, con */ void ui_display_wnd_current_state(const pnoc_t pnoc[], const uint8_t num_players, const uint8_t active_player, const uint32_t score) { + assert(pnoc != NULL); + wattron(w_current_state, A_BOLD); mvwprintw(w_current_state, 0, 0, "Current state:"); mvwprintw(w_current_state, 0, 22, "Your Score: %3d", score); @@ -243,6 +255,8 @@ void ui_display_wnd_current_state(const pnoc_t pnoc[], const uint8_t num_players */ void ui_display_wnd_hand_cards(const hand_t *h, const bool highlight, const uint8_t highlighted_card) { + assert(h != NULL); + uint8_t num_zero_cards = 0; wattron(w_hand_cards, A_BOLD); @@ -287,6 +301,8 @@ void ui_display_wnd_hand_cards(const hand_t *h, const bool highlight, const uint uint8_t ui_choose_card(hand_t *h) { + assert(h != NULL); + int key; uint8_t chosen_card_idx = 0; int8_t i; // Has to be signed for modulo calculation @@ -338,8 +354,10 @@ uint8_t ui_choose_card(hand_t *h) return chosen_card_idx; } -uint8_t ui_choose_stack(const tablestacks ts) +uint8_t ui_choose_stack(const table_stacks_t *ts) { + assert(ts != NULL); + int key; uint8_t chosen_stack_idx = 0; @@ -356,7 +374,7 @@ uint8_t ui_choose_stack(const tablestacks ts) // Fall through case KEY_UP: if (0 == chosen_stack_idx) - chosen_stack_idx = NUM_TABLESTACKS - 1; + chosen_stack_idx = NUM_TABLE_STACKS - 1; else chosen_stack_idx--; break; @@ -366,7 +384,7 @@ uint8_t ui_choose_stack(const tablestacks ts) case KEY_RIGHT: // Fall through case KEY_DOWN: - if (NUM_TABLESTACKS - 1 == chosen_stack_idx) + if (NUM_TABLE_STACKS - 1 == chosen_stack_idx) chosen_stack_idx = 0; else chosen_stack_idx++; diff --git a/src/ui.h b/src/ui.h index 8c001bb..40252ce 100644 --- a/src/ui.h +++ b/src/ui.h @@ -3,17 +3,16 @@ #include #include "card.h" -#include "cardstack.h" -#include "tablestacks.h" +#include "table_stacks.h" #include "hand.h" #include "pnoc.h" -void ui_display_wnd_table_cards(const tablestacks ts, const bool highlight, const uint8_t highlighted_stack); -void ui_display_wnd_stack_points(const tablestacks ts, const bool highlight, const uint8_t highlighted_points); +void ui_display_wnd_table_cards(const table_stacks_t *ts, const bool highlight, const uint8_t highlighted_stack); +void ui_display_wnd_stack_points(const table_stacks_t *ts, const bool highlight, const uint8_t highlighted_points); void ui_display_wnd_current_state(const pnoc_t pnoc[], const uint8_t num_players, const uint8_t active_player, const uint32_t score); void ui_display_wnd_hand_cards(const hand_t *h, const bool highlight, const uint8_t highlighted_card); uint8_t ui_choose_card(hand_t *h); -uint8_t ui_choose_stack(const tablestacks ts); +uint8_t ui_choose_stack(const table_stacks_t *ts); void ui_init(void); void ui_fini(void); -- cgit v1.2.3