Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct Player {
  6. char* name;
  7. long id;
  8. struct Player* nextPlayer;
  9. };
  10.  
  11. void Kill(Player* deadman)
  12. {
  13. free(deadman->name);
  14. free(deadman);
  15. }
  16.  
  17. Player* createNewPlayer()
  18. {
  19. const int MAX_PLAYER_NAME_LENGTH = 80;
  20. Player* new_player = (Player*)malloc(sizeof(Player));
  21. new_player->nextPlayer = NULL;
  22. printf("name:\n");
  23. new_player->name = (char*)malloc(MAX_PLAYER_NAME_LENGTH + 1);
  24. scanf("%s", new_player->name);
  25. //gets_s(new_player->name, MAX_PLAYER_NAME_LENGTH);
  26. //fgets(new_player->name, MAX_PLAYER_NAME_LENGTH, stdin);
  27. //new_player->name[strlen(new_player->name) - 1] = '\0';
  28. printf("id:\n");
  29. scanf("%d", &new_player->id);
  30. return new_player;
  31. }
  32.  
  33. void setNextPlayer(Player* head)
  34. {
  35. Player* list = head;
  36. Player* new_player = createNewPlayer();
  37.  
  38. while (list->nextPlayer != NULL)
  39. list = list->nextPlayer;
  40.  
  41. list->nextPlayer = new_player;
  42. return;
  43. }
  44.  
  45. int check_add_player() {
  46. int should_add_player=1;
  47. printf("Add a person to the game? 1 for yes, 0 for no\n");
  48. scanf("%d", &should_add_player);
  49. return should_add_player;
  50. }
  51.  
  52. Player* InitTheHungerGamePlayers()
  53. {
  54. Player* firstPlayer = NULL;
  55. Player* lastPlayer = NULL;
  56. while (check_add_player() == 1)
  57. {
  58. if (!firstPlayer)
  59. {
  60. firstPlayer = createNewPlayer();
  61. lastPlayer = firstPlayer;
  62. continue;
  63. }
  64.  
  65. lastPlayer->nextPlayer = createNewPlayer();
  66. lastPlayer = lastPlayer->nextPlayer;
  67. }
  68.  
  69. if (firstPlayer && lastPlayer)
  70. lastPlayer->nextPlayer = firstPlayer;
  71.  
  72. return firstPlayer;
  73. }
  74.  
  75. void LetTheGamesBegin(Player* first)
  76. {
  77. Player* temp;
  78. if (first == NULL)
  79. return;
  80. while (first->nextPlayer != first)
  81. {
  82. temp = first->nextPlayer;
  83. printf("%s kills %s\n", first->name, temp->name);
  84. first->nextPlayer = temp->nextPlayer;
  85. Kill(temp);
  86. first = first->nextPlayer;
  87. }
  88. printf("%s stayin alive!!", first->name);
  89. free(first);
  90. }
  91.  
  92. int main()
  93. {
  94. Player* first = InitTheHungerGamePlayers();
  95. LetTheGamesBegin(first);
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement