Advertisement
PowerhouseG

Fighting Tournament

May 29th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define SIZE 15
  5. #include<time.h>
  6. typedef struct TTFighter
  7. {
  8.     long fnom;
  9.     char name[SIZE];
  10.     double randNum;
  11.     struct TTFighter *next;
  12. } TFighter;
  13.  
  14. TFighter* readList(char *fname,TFighter *head);
  15. TFighter* addLastElement(TFighter *head, long fnom,char name[],int randNum);
  16. TFighter* atEnd(TFighter*head);
  17. void tournament(TFighter*head);
  18. void delList(TFighter *head);
  19. void bubbleSort(TFighter *head);
  20. int isEven(TFighter *head);
  21. int roundsLeft(TFighter *head);
  22. TFighter* addClone(TFighter *head);
  23. void fight(TFighter *head);
  24. TFighter* fainted(TFighter *head, long a);
  25. int roundContinues (TFighter *head);
  26. void printList(TFighter* head);
  27. void swap(TFighter *a, TFighter *b);
  28. void winner (TFighter *head);
  29.  
  30. int main()
  31. {
  32.     TFighter *head=NULL;
  33.     head = readList("bar.txt",head);
  34.     if (head==NULL)
  35.         return 1;
  36.     printList(head);
  37.     bubbleSort(head);
  38.     printf("\n Linked list after sorting ");
  39.     printList(head);
  40.     tournament(head);
  41.     delList(head);
  42.     head=NULL;
  43.     return 0;
  44. }
  45. void printList(TFighter* head)
  46. {
  47.     TFighter *p;
  48.  
  49.     for(p=head;p!=NULL;p=p->next)
  50.         printf("\n%ld\t%s\t%g\n",p->fnom,p->name,p->randNum);
  51. }
  52. TFighter* readList(char *fname,TFighter *head)
  53. {
  54.     FILE *f;
  55.     long fnom;
  56.     char name[SIZE];
  57.     srand(time(0));
  58.     int i=0;
  59.  
  60.     if ((f=fopen(fname,"r"))==NULL)
  61.     {
  62.         delList(head);
  63.         return NULL;
  64.     }
  65.  
  66.     while(!feof(f))
  67.     {
  68.         if (i){
  69.         fscanf(f,"%li%s\n",&fnom,name);
  70.         head = addLastElement(head,fnom,name,rand());
  71.         if (head==NULL)
  72.         {
  73.             break;
  74.         }
  75.         } else {
  76.         fscanf(f,"%li%s\n",&fnom,name);
  77.         head = addLastElement(head,fnom,name,0);
  78.  
  79.         }
  80.         i++;
  81.     }
  82.     fclose(f);
  83.     return head;
  84. }
  85. TFighter* addLastElement(TFighter *head, long fnom,char name[],int randNum)
  86. {
  87.     TFighter *p,*end;
  88.  
  89.     if ((p=(TFighter*)malloc(sizeof(TFighter)))==NULL)
  90.         return NULL;
  91.  
  92.     p->fnom = fnom;
  93.     p->randNum = randNum;
  94.     strcpy(p->name,name);
  95.     p->next=NULL;
  96.     if (head==NULL)
  97.     {
  98.         head=p;
  99.     }
  100.     else
  101.     {
  102.         end = atEnd(head);
  103.         end->next = p;
  104.     }
  105.     return head;
  106. }
  107. TFighter* atEnd(TFighter* head)
  108. {
  109.     TFighter *p;
  110.  
  111.     for (p=head;p->next!=NULL;p=p->next)
  112.         ;
  113.     return p;
  114. }
  115. void delList(TFighter *head)
  116. {
  117.     TFighter *p,*q;
  118.  
  119.     for(p=head;p!=NULL;q=p,p=p->next,free(q))
  120.         ;
  121.  
  122. }
  123. void bubbleSort(TFighter *head)
  124. {
  125.     int swapped;
  126.     TFighter *ptr1;
  127.     TFighter *lptr = NULL;
  128.  
  129.     if (head == NULL)
  130.         return;
  131.  
  132.     do
  133.     {
  134.         swapped = 0;
  135.         ptr1 = head;
  136.  
  137.         while (ptr1->next != lptr)
  138.         {
  139.  
  140.             if (ptr1->randNum > ptr1->next->randNum)
  141.             {
  142.                 swap(ptr1, ptr1->next);
  143.                 swapped = 1;
  144.  
  145.             }
  146.             ptr1 = ptr1->next;
  147.         }
  148.         lptr = ptr1;
  149.     }
  150.     while (swapped);
  151. }
  152. void swap(TFighter *a, TFighter *b)
  153. {
  154.  
  155.     int temp = a->randNum;
  156.     a->randNum = b->randNum;
  157.     b->randNum = temp;
  158.  
  159.     long temp2 = a->fnom;
  160.     a->fnom = b->fnom;
  161.     b->fnom = temp2;
  162.  
  163.     int x;
  164.     char temp3[SIZE];
  165.  
  166.     for (x=0;x<SIZE;x++) {
  167.         temp3[x]=a->name[x];
  168.         a->name[x]=b->name[x];
  169.         b->name[x]=temp3[x];
  170.     }
  171.     }
  172. void tournament(TFighter*head)
  173. {
  174.  
  175.     while (roundsLeft(head)){
  176.  
  177.         if (isEven(head)) {
  178.             addClone(head);
  179.             fight(head);
  180.         } else {
  181.  
  182.         fight(head);
  183.         }
  184.     }
  185.     winner(head);
  186. }
  187. int isEven(TFighter *head){
  188.     TFighter *p;
  189.     long cnt;
  190.  
  191.     for(cnt=0,p=head;p!=NULL;p=p->next){
  192.         cnt++;
  193.     }
  194.     if(cnt % 2 == 0)
  195.         return 1;
  196.     else
  197.         return 0;
  198. }
  199. int roundsLeft(TFighter *head) {
  200. TFighter *p;
  201.     int f;
  202.  
  203.     for(f=0,p=head;p!=NULL;p=p->next){
  204.         f++;
  205. }
  206.  
  207.     if(f){
  208.         if (f == 2){
  209.             return 0;
  210.         }
  211.         return 1;
  212.     }
  213. return 0;
  214. }
  215. TFighter* addClone(TFighter *head) {
  216.  
  217.     TFighter *p;
  218.     long fnom;
  219.     double randNum;
  220.     char name[SIZE];
  221.     int j;
  222.     for(p=head;p!=NULL;p=p->next){
  223.     fnom=p->fnom + 200;
  224.     randNum=p->randNum;
  225.     for (j=0;j<SIZE;j++) {
  226.     name[j]=p->name[j];
  227.     }
  228.     }
  229.     head = addLastElement(head,fnom,name,randNum);
  230.  
  231.     return head;
  232. }
  233. void fight(TFighter *head){
  234.     TFighter *p;
  235.     int i;
  236.     int x = 0;
  237.     int rcont = roundContinues(head);
  238.     long a;
  239.     long b;
  240.     int lifeA;
  241.     int lifeB ;
  242.     int damage;
  243.     srand(time(0));
  244.     int d=1;
  245.  
  246.     while (x < rcont) {
  247.  
  248.     for(i=1,p=head;p!=NULL;p=p->next)
  249.     {
  250.         if(d){
  251.             d = 0;
  252.         } else {
  253.  
  254.         x = x+2;
  255.  
  256.         lifeA = 100;
  257.         lifeB = 100;
  258.         if (i==1){
  259.         a = p->fnom;
  260.         i++;
  261.         } else {
  262.         b = p->fnom;
  263.  
  264.         while(1){
  265.             if (lifeB >= 0){
  266.             damage = rand();
  267.             while (damage>20){
  268.             damage = damage/5;
  269.             }
  270.             lifeA = lifeA - damage;
  271.             printf("%li lost %d life points has %d left\n", a, damage, lifeA);
  272.         }
  273.             if (lifeA >= 0){
  274.              damage = rand();
  275.             while (damage>20){
  276.             damage = damage/5;
  277.             }
  278.             lifeB = lifeB - damage;
  279.             printf("%li lost %d life points has %d left\n", b, damage,lifeB);
  280.  
  281.             }
  282.  
  283.                     if (lifeA<0){
  284.                         printf("\n number %li died", a);
  285.                     fainted(head, a);
  286.                         break;
  287.                     }
  288.                     if (lifeB<0){
  289.                         printf("\n number %li died \n", b);
  290.                        fainted(head, b);
  291.                         break;
  292.             }
  293.         }
  294.         i=1;
  295.         }
  296.     }
  297.     }
  298.     }
  299. }
  300. TFighter* fainted(TFighter *head, long a)
  301. {
  302.     TFighter *p,*prev;
  303.     long b;
  304.     printf("\n This is the number of the deceased %li lol", a);
  305.     for(prev=p=head;p!=NULL; )
  306.     {
  307.         b=p->fnom;
  308.         if (b==a)
  309.         {
  310.             if (head==p)
  311.             {
  312.                 head = head->next;
  313.                 free(p);
  314.                 p = prev = head;
  315.             }
  316.             else
  317.             {
  318.                 prev->next = p->next;
  319.                 free(p);
  320.                 p = prev->next;
  321.             }
  322.         }
  323.         else
  324.         {
  325.             prev = p;
  326.             p=p->next;
  327.         }
  328.      }
  329.      printList(head);
  330.     return head;
  331. }
  332. int roundContinues (TFighter *head) {
  333.  
  334.     TFighter *p;
  335.     int f;
  336.     for(f=0,p=head;p!=NULL;p=p->next){
  337.         f++;
  338. }
  339.     return f;
  340. }
  341. void winner (TFighter *head){
  342.     TFighter *p;
  343.     long fnom;
  344.     char name[SIZE];
  345.     int x=0;
  346.         for(p=head;p!=NULL;p=p->next){
  347.             fnom=p->fnom;
  348.                 for (x=0;x<SIZE;x++){
  349.                     name[x]=p->name[x];
  350.                     }
  351.                     }
  352.  
  353.             while(fnom-200>0){
  354.               fnom = fnom-200;
  355.                 }
  356.  
  357. printf("\nAND THE ULTIMATE WINNER OF THE COMPETITION IS: \n%ld\t%s\n",fnom,name);
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement