diff options
| author | Reiner Herrmann <reiner@reiner-h.de> | 2011-01-23 14:16:07 +0100 |
|---|---|---|
| committer | Reiner Herrmann <reiner@reiner-h.de> | 2011-01-23 14:16:07 +0100 |
| commit | ea8901deb1704ec62a13aa491ee965d31ea1ff1b (patch) | |
| tree | bc6b352dce03972df1be3c84ec0ae11380ba3c93 | |
| parent | 753394bf3ae47fa95f8cee4cb2280d2cfdeaa3c3 (diff) | |
completed game loop with all states and pseudo code in real life session :)
| -rw-r--r-- | src/game.c | 124 |
1 files changed, 114 insertions, 10 deletions
@@ -80,26 +80,130 @@ void start_game(const bool servermode, const char *addr, const uint16_t port) // main game loop while(running) { + /* switch(state) { - case STATE_DEALCARDS: // deal cards to players (on host) - // dealcards(mainstack); - state = STATE_SELECTCARD; + case STATE_CLIENT_WAIT_CARDS: + if(round == 1) + receive_and_place_table_stacks(); + wait_for_cards_from_server(); + state = STATE_CLIENT_SELECT_OPEN_CARD; break; - case STATE_WAIT_CARDS: // wait on client until host has dealt cards + + case STATE_SERVER_DEAL_CARDS: + if(round == 1) + send_and_place_table_stacks(); + deal_cards_to_clients(); + state = STATE_SERVER_WAIT_OPEN_CARDS; break; - case STATE_SELECTCARD: // player has to select own card, if done, set state to STATE_WAIT_OPPONENTCARDS - ui_choose_card(&h); - running = false; + + case STATE_CLIENT_SELECT_OPEN_CARD: + select_open_card(); + send_open_card_to_server(); + state = STATE_CLIENT_WAIT_FOR_OPEN_CARDS; break; - case STATE_WAIT_OPPONENTCARDS: // wait until all opponents have selected their open card. Then check if we have the lowest open card. If so, set state to STATE_SELECTSTACK, otherwise to STATE_WAIT_OPPONENTSTACK + + case STATE_SERVER_WAIT_FOR_OPEN_CARDS: + receive_open_cards_from_clients(); + sort_open_card_list(); // in ascending order + send_open_card_list_to_clients(); + state = STATE_SERVER_PLAY_CARDS; break; - case STATE_WAIT_OPPONENTSTACK: // wait until opponent has selected a stack + + case STATE_CLIENT_WAIT_FOR_OPEN_CARDS: + receive_sorted_list_of_open_cards(); + state = STATE_CLIENT_PLAY_CARDS; break; - case STATE_SELECTSTACK: // player has to select a stack and update it (add card/take whole stack). If it was the last player's turn, set state to STATE_SELECTCARD, otherwise to STATE_WAIT_OPPONENTSTACK + + 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_CARDS); + state = STATE_SERVER_DEAL_CARDS; + } + else + { + send_action_to_client(GAME_FINISHED); + state = STATE_SERVER_GAME_FINISHED; + } + } break; + + case STATE_CLIENT_PLAY_CARDS: + foreach(open_card) + { + play_lowest_open_card + { + determine_stack_for_open_card + { + if (stack_has_to_be_picked) + { + if (we_have_to_pick) + { + pick_stack(); + send_stack_to_server(); + } + else // another client has to pick + { + receive_stack(); + } + + clear_stack(stack_id); + } + } + place_card() + { + if(count_stack_cards == 6) + clear_stack(stack_id); + } + } + + } + if (we_have_hand_cards) + { + state = STATE_CLIENT_SELECT_OPEN_CARD; + } + else + { + receive_next_server_action(); + if (server_action == DEAL_CARDS) + { + state = STATE_CLIENT_WAIT_CARDS; + } + else if (server_action == GAME_FINISHED) + state = STATE_CLIENT_GAME_FINISHED; + } + break; + default: assert(false); // should never happen } + */ } } |
