players.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //ready
  2. #define _GNU_SOURCE
  3. #include "game_server.h"
  4. //TODO - implement this
  5. /*
  6. * Create a new player with the given name. Insert it at the tail of the list
  7. * of players whose head is pointed to by *player_ptr_add.
  8. */
  9. int create_player(const char *name, Player **player_ptr_add) {
  10. if(find_player(name, *player_ptr_add))
  11. return 1;
  12. Player * p = calloc(1, sizeof(Player));
  13. if(strlen(name) > MAX_NAME){
  14. strncpy(p->name, name, MAX_NAME - 1);
  15. p->total_score = 0;
  16. p->max_score = 0;
  17. p->total_games = 0;
  18. p->next = NULL;
  19. Player * curr = *player_ptr_add;
  20. if(curr == NULL){
  21. *player_ptr_add = p;
  22. return 0;
  23. }
  24. while(curr->next)
  25. curr = curr->next;
  26. curr->next = p;
  27. return 2;
  28. }
  29. //Player * p = calloc(1, sizeof(Player));
  30. strcpy(p->name, name);
  31. p->total_score = 0;
  32. p->max_score = 0;
  33. p->total_games = 0;
  34. p->next = NULL;
  35. Player * curr = *player_ptr_add;
  36. if(curr == NULL){
  37. *player_ptr_add = p;
  38. return 0;
  39. }
  40. while(curr->next)
  41. curr = curr->next;
  42. curr->next = p;
  43. return 0;
  44. }
  45. //TODO - implement this
  46. /*
  47. * Return a pointer to the player with this name in
  48. * the list starting with head. Return NULL if no such player exists.
  49. */
  50. Player *find_player(const char *name, Player *head) {
  51. Player* curr = (Player*) head;
  52. while (curr != NULL) {
  53. if (strcmp(curr->name, name) == 0) {
  54. return curr;
  55. }
  56. curr = curr->next;
  57. }
  58. return NULL;
  59. }