/***************************************************************************** * ___ __ __ ___ _ __ * * / _ \\ \/ // _ \ '_ \ * * | (_) |> <| __/ | | | * * \___//_/\_\\___|_| |_| * * * * The card game * * * * Copyright (C) 2011, Reiner Herrmann * * Mario Kilies * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 3 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see . * * * *****************************************************************************/ #include "player.h" #include #include /** * Search for a player in the player list * @param[in] pl Pointer to player list to search * @param[in] pid Player ID of searched player * @return Pointer to player_list_entry_t; NULL if none found for specified player id */ player_list_entry_t *get_player_list_entry_by_player_id(player_list_t *pl, const player_id_t pid) { assert(pl != NULL); for(int i = 0; i < pl->count; i++) { if(pl->players[i].player_id != pid) continue; return &pl->players[i]; } return NULL; } /** * Compare player list entries via open_card */ static int ple_open_card_comparator(const void *a, const void *b) { player_list_entry_t ple1 = *(player_list_entry_t *)a; player_list_entry_t ple2 = *(player_list_entry_t *)b; return ple1.open_card - ple2.open_card; } /** * Compare player list entries via score */ static int ple_score_comparator(const void *a, const void *b) { player_list_entry_t ple1 = *(player_list_entry_t *)a; player_list_entry_t ple2 = *(player_list_entry_t *)b; return ple1.score - ple2.score; } /** * Sort a player list in ascending order by open card values * @param[in,out] pl Pointer to layer list to sort * @param[in] num_entries Number of player list entries in list */ void player_list_sort_by_open_card(player_list_t *pl, const uint8_t num_entries) { assert(pl != NULL); qsort(pl->players, num_entries, sizeof(player_list_entry_t), ple_open_card_comparator); } /** * Sort a player list in ascending order by scores * @param[in,out] pl Pointer to layer list to sort * @param[in] num_entries Number of player list entries in list */ void player_list_sort_by_score(player_list_t *pl, const uint8_t num_entries) { assert(pl != NULL); qsort(pl->players, num_entries, sizeof(player_list_entry_t), ple_score_comparator); }