int deal(struct game_state *game) { if (NULL == game) { return ERR_UNINITIALIZED_GAME; } int ret = 0; for (uint8_t card_cnt = 0; card_cnt < get_hand_size(); card_cnt++) { ret = take_top_card(game->p_remote, game->pool); if (0 > ret) { return ret; } ret = take_top_card(game->p_bot, game->pool); if (0 > ret) { return ret; } } return SUCCESS; }
void apply_move(GAME *game, MOVE move) { PLAYER *player = &game->players[game->current_player]; int next_player; if (move != MOVE_PASS) { int move_value = MOVE_VALUE(move); int move_count = MOVE_COUNT(move); DECKP hand = player->hand; hand[move_value] -= move_count; game->pile = move; game->pile_owner = game->current_player; /* Is this player out? */ if (get_hand_size(player->hand) == 0) { player->rank = game->next_rank; game->next_rank++; } } /* Move to next player. */ next_player = game->current_player; do { next_player = (next_player+1) % game->num_players; if (next_player == game->current_player) { /* Game is over. */ game->players[next_player].rank = game->next_rank; return; } } while (game->players[next_player].rank != UNRANKED); game->current_player = next_player; }
int is_valid_move(GAME *game, MOVE move) { int move_value; int move_count; int pile_value; int pile_count; /* Pass is valid if the pile belongs to someone else, or the player is out. */ if (move == MOVE_PASS) { if (get_hand_size(game->players[game->current_player].hand) == 0) return 1; return (game->current_player != game->pile_owner); } /* Joker is always valid. */ if (move == MOVE_JOKER) { return 1; } /* If the pile is empty or belongs to us, the move is valid. */ pile_count = MOVE_COUNT(game->pile); if (pile_count == 0 || game->pile_owner == game->current_player) { return 1; } /* Otherwise, if the move is the same size of pile but higher value, then it's is valid. */ pile_value = MOVE_VALUE(game->pile); move_value = MOVE_VALUE(move); move_count = MOVE_COUNT(move); if (move_count == pile_count && move_value > pile_value) return 1; return 0; }