players.c 961 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #define _GNU_SOURCE
  2. #include "game_server.h"
  3. //TODO - implement this
  4. /*
  5. * Create a new player with the given name. Insert it at the tail of the list
  6. * of players whose head is pointed to by *player_ptr_add.
  7. */
  8. int create_player(const char *name, Player **player_ptr_add) {
  9. Player * new = malloc(sizeof(Player));
  10. strcpy(new->name, name);
  11. new->max_score = 0;
  12. new->total_games = 0;
  13. new->total_score = 0;
  14. new->next = NULL;
  15. Player * temp = *player_ptr_add;
  16. if (temp) {
  17. while (temp->next) {
  18. temp = temp->next;
  19. }
  20. temp->next = new;
  21. }
  22. else {
  23. player_ptr_add = &new;
  24. }
  25. return 0;
  26. }
  27. //TODO - implement this
  28. /*
  29. * Return a pointer to the player with this name in
  30. * the list starting with head. Return NULL if no such player exists.
  31. */
  32. Player *find_player(const char *name, const Player *head) {
  33. Player * temp = (Player *)head;
  34. while (temp && !strcmp(temp->name, name)) {
  35. temp = temp->next;
  36. }
  37. return temp;
  38. }