players.c 1.2 KB

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