players.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. if(find_player(name, *player_ptr_add))
  10. return 1;
  11. Player * p = calloc(1, sizeof(Player));
  12. if(strlen(name) > MAX_NAME){
  13. strncpy(p->name, name, MAX_NAME - 1);
  14. p->total_score = 0;
  15. p->max_score = 0;
  16. p->total_games = 0;
  17. p->next = NULL;
  18. Player * curr = *player_ptr_add;
  19. if(curr == NULL){
  20. *player_ptr_add = p;
  21. return 0;
  22. }
  23. while(curr->next)
  24. curr = curr->next;
  25. curr->next = p;
  26. return 2;
  27. }
  28. //Player * p = calloc(1, sizeof(Player));
  29. strcpy(p->name, name);
  30. p->total_score = 0;
  31. p->max_score = 0;
  32. p->total_games = 0;
  33. p->next = NULL;
  34. Player * curr = *player_ptr_add;
  35. if(curr == NULL){
  36. *player_ptr_add = p;
  37. return 0;
  38. }
  39. while(curr->next)
  40. curr = curr->next;
  41. curr->next = p;
  42. return 0;
  43. }
  44. //TODO - implement this
  45. /*
  46. * Return a pointer to the player with this name in
  47. * the list starting with head. Return NULL if no such player exists.
  48. */
  49. Player *find_player(const char *name, Player *head) {
  50. Player * curr = head;
  51. while(curr){
  52. if(strcmp(curr->name, name) == 0)
  53. return curr;
  54. curr = curr->next;
  55. }
  56. return NULL;
  57. }