diff options
| author | Mario Kilies <MarioKilies@GMX.net> | 2011-01-25 11:11:47 +0100 |
|---|---|---|
| committer | Mario Kilies <MarioKilies@GMX.net> | 2011-01-25 11:11:47 +0100 |
| commit | 3fc586325f8fc8fb4f1990bf87ad4c37e722f381 (patch) | |
| tree | c6e5dfcb251eddc3cc7a413c562849cb14fccedc /src/game.c | |
| parent | 6dad43a768646ef0d8f06958446761eb8a1b3f93 (diff) | |
Created a mainstack datatype. Refactored server game states into separate functions.
Diffstat (limited to 'src/game.c')
| -rw-r--r-- | src/game.c | 140 |
1 files changed, 8 insertions, 132 deletions
@@ -15,50 +15,7 @@ #include "net/client.h" #include "net/server.h" #include "game_states.h" - -static 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; i<size; i++, val++) - stack[i] = val; - - // shuffle stack - for(uint32_t i=0; i<3*size; i++) - { - uint32_t x = rand() % size; - uint32_t y = rand() % size; - card tmp = stack[x]; - stack[x] = stack[y]; - stack[y] = tmp; - } -} - -static const card mainstack_remove_card(card *stack, const uint32_t size) -{ - for(uint32_t i=0; i<size; i++) - { - card c = stack[i]; - if(c == 0) - continue; - stack[i] = 0; - return c; - } - return 0; // stack empty -} - -static const uint8_t num_cards_in_stack(const card *stack, const uint32_t size) -{ - uint8_t count = 0; - for(int i=0; i<size; i++) - { - if(stack[i] == 0) - continue; - count++; - } - return count; -} +#include "main_stack.h" static void main_loop_client(int sock) { @@ -97,108 +54,25 @@ static void main_loop_server(socket_list_t* client_socks) { bool running = true; uint8_t round = 1; - int cards = MAX_CARD - MIN_CARD + 1; - card mainstack[cards]; game_state_t state = STATE_SERVER_DEAL_HAND_CARDS; - data_store *data = datamodel(); - - srand(time(0)); - init_mainstack(mainstack, cards); + main_stack_t m; + main_stack_init(&m); while(running) { switch(state) { case STATE_SERVER_DEAL_HAND_CARDS: - if(round == 1) - { - // Draw cards for initial stacks and send them to clients - data->table_stacks.stacks[0].cards[0] = mainstack_remove_card(mainstack, cards); - data->table_stacks.stacks[1].cards[0] = mainstack_remove_card(mainstack, cards); - data->table_stacks.stacks[2].cards[0] = mainstack_remove_card(mainstack, cards); - data->table_stacks.stacks[3].cards[0] = mainstack_remove_card(mainstack, cards); - for(int i = 0; i < data->players.count; i++) - { - net_send(client_socks->sockets[i], msg_type_initial_stacks, NULL); - } - } - - int num_dealcards = num_cards_in_stack(mainstack, cards) / data->players.count; - if(num_dealcards > 10) - num_dealcards = 10; - - // Deal hand cards to clients - for(int i = 0; i < data->players.count; i++) - { - hand_t h; - memset(h.cards, 0, MAX_HAND_CARDS); - for(int j=0; j<num_dealcards; j++) - h.cards[j] = mainstack_remove_card(mainstack, cards); - hand_sort(&h); - - net_send(client_socks->sockets[i], msg_type_deal_hand, &h); - } - - state = STATE_SERVER_WAIT_FOR_OPEN_CARDS; + state = state_server_deal_hand_cards(client_socks, round, &m); break; case STATE_SERVER_WAIT_FOR_OPEN_CARDS: - // Receive open cards from clients - for(int i = 0; i < data->players.count; i++) - { - pnoc_t* pl = get_pnoc_from_playerid(&data->players, client_socks->player_ids[i]); - assert(pl != NULL); - net_recv(client_socks->sockets[i], msg_type_selected_card); - pl->open_card = data->selected_card; - } - - for(int i=0; i<data->players.count; i++) - net_send(client_socks->sockets[i], msg_type_selected_card_all, NULL); - - //state = STATE_SERVER_PLAY_CARDS; - state = STATE_SERVER_WAIT_FOR_OPEN_CARDS; // just for testing + state = state_server_wait_for_open_cards(client_socks); break; #if 0 case STATE_SERVER_PLAY_CARDS: - foreach(open_card) - { - play_lowest_open_card - { - determine_stack_for_open_card - { - if (stack_has_to_be_picked) { - receive_stack_from_client(); - send_received_stack_to_other_clients(); - clear_stack(stack_id); - } - } - place_card() - { - if(count_stack_cards == 6) - clear_stack(stack_id); - } - } - - } - if (clients_have_hand_cards) - { - state = STATE_SERVER_WAIT_FOR_OPEN_CARDS; - } - else - { - if (main_stack_has_enough_cards_for_clients) - { - send_action_to_client(DEAL_HAND_CARDS); - round++; - state = STATE_SERVER_DEAL_HAND_CARDS; - } - else - { - send_action_to_client(GAME_FINISHED); - state = STATE_SERVER_GAME_FINISHED; - } - } + state = state_server_play_cards(client_socks, round); break; #endif default: @@ -226,6 +100,8 @@ void start_game(const bool servermode, const char* addr, const char* port) socket_list_t client_socks; uint8_t num_players = 2; + srand(time(0)); // Initialize RNG + data_store* data = datamodel(); server_sock = server_start(port); |
