Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define SIZE 15
- #include<time.h>
- typedef struct TTFighter
- {
- long fnom;
- char name[SIZE];
- double randNum;
- struct TTFighter *next;
- } TFighter;
- TFighter* readList(char *fname,TFighter *head);
- TFighter* addLastElement(TFighter *head, long fnom,char name[],int randNum);
- TFighter* atEnd(TFighter*head);
- void tournament(TFighter*head);
- void delList(TFighter *head);
- void bubbleSort(TFighter *head);
- int isEven(TFighter *head);
- int roundsLeft(TFighter *head);
- TFighter* addClone(TFighter *head);
- void fight(TFighter *head);
- TFighter* fainted(TFighter *head, long a);
- int roundContinues (TFighter *head);
- void printList(TFighter* head);
- void swap(TFighter *a, TFighter *b);
- void winner (TFighter *head);
- int main()
- {
- TFighter *head=NULL;
- head = readList("bar.txt",head);
- if (head==NULL)
- return 1;
- printList(head);
- bubbleSort(head);
- printf("\n Linked list after sorting ");
- printList(head);
- tournament(head);
- delList(head);
- head=NULL;
- return 0;
- }
- void printList(TFighter* head)
- {
- TFighter *p;
- for(p=head;p!=NULL;p=p->next)
- printf("\n%ld\t%s\t%g\n",p->fnom,p->name,p->randNum);
- }
- TFighter* readList(char *fname,TFighter *head)
- {
- FILE *f;
- long fnom;
- char name[SIZE];
- srand(time(0));
- int i=0;
- if ((f=fopen(fname,"r"))==NULL)
- {
- delList(head);
- return NULL;
- }
- while(!feof(f))
- {
- if (i){
- fscanf(f,"%li%s\n",&fnom,name);
- head = addLastElement(head,fnom,name,rand());
- if (head==NULL)
- {
- break;
- }
- } else {
- fscanf(f,"%li%s\n",&fnom,name);
- head = addLastElement(head,fnom,name,0);
- }
- i++;
- }
- fclose(f);
- return head;
- }
- TFighter* addLastElement(TFighter *head, long fnom,char name[],int randNum)
- {
- TFighter *p,*end;
- if ((p=(TFighter*)malloc(sizeof(TFighter)))==NULL)
- return NULL;
- p->fnom = fnom;
- p->randNum = randNum;
- strcpy(p->name,name);
- p->next=NULL;
- if (head==NULL)
- {
- head=p;
- }
- else
- {
- end = atEnd(head);
- end->next = p;
- }
- return head;
- }
- TFighter* atEnd(TFighter* head)
- {
- TFighter *p;
- for (p=head;p->next!=NULL;p=p->next)
- ;
- return p;
- }
- void delList(TFighter *head)
- {
- TFighter *p,*q;
- for(p=head;p!=NULL;q=p,p=p->next,free(q))
- ;
- }
- void bubbleSort(TFighter *head)
- {
- int swapped;
- TFighter *ptr1;
- TFighter *lptr = NULL;
- if (head == NULL)
- return;
- do
- {
- swapped = 0;
- ptr1 = head;
- while (ptr1->next != lptr)
- {
- if (ptr1->randNum > ptr1->next->randNum)
- {
- swap(ptr1, ptr1->next);
- swapped = 1;
- }
- ptr1 = ptr1->next;
- }
- lptr = ptr1;
- }
- while (swapped);
- }
- void swap(TFighter *a, TFighter *b)
- {
- int temp = a->randNum;
- a->randNum = b->randNum;
- b->randNum = temp;
- long temp2 = a->fnom;
- a->fnom = b->fnom;
- b->fnom = temp2;
- int x;
- char temp3[SIZE];
- for (x=0;x<SIZE;x++) {
- temp3[x]=a->name[x];
- a->name[x]=b->name[x];
- b->name[x]=temp3[x];
- }
- }
- void tournament(TFighter*head)
- {
- while (roundsLeft(head)){
- if (isEven(head)) {
- addClone(head);
- fight(head);
- } else {
- fight(head);
- }
- }
- winner(head);
- }
- int isEven(TFighter *head){
- TFighter *p;
- long cnt;
- for(cnt=0,p=head;p!=NULL;p=p->next){
- cnt++;
- }
- if(cnt % 2 == 0)
- return 1;
- else
- return 0;
- }
- int roundsLeft(TFighter *head) {
- TFighter *p;
- int f;
- for(f=0,p=head;p!=NULL;p=p->next){
- f++;
- }
- if(f){
- if (f == 2){
- return 0;
- }
- return 1;
- }
- return 0;
- }
- TFighter* addClone(TFighter *head) {
- TFighter *p;
- long fnom;
- double randNum;
- char name[SIZE];
- int j;
- for(p=head;p!=NULL;p=p->next){
- fnom=p->fnom + 200;
- randNum=p->randNum;
- for (j=0;j<SIZE;j++) {
- name[j]=p->name[j];
- }
- }
- head = addLastElement(head,fnom,name,randNum);
- return head;
- }
- void fight(TFighter *head){
- TFighter *p;
- int i;
- int x = 0;
- int rcont = roundContinues(head);
- long a;
- long b;
- int lifeA;
- int lifeB ;
- int damage;
- srand(time(0));
- int d=1;
- while (x < rcont) {
- for(i=1,p=head;p!=NULL;p=p->next)
- {
- if(d){
- d = 0;
- } else {
- x = x+2;
- lifeA = 100;
- lifeB = 100;
- if (i==1){
- a = p->fnom;
- i++;
- } else {
- b = p->fnom;
- while(1){
- if (lifeB >= 0){
- damage = rand();
- while (damage>20){
- damage = damage/5;
- }
- lifeA = lifeA - damage;
- printf("%li lost %d life points has %d left\n", a, damage, lifeA);
- }
- if (lifeA >= 0){
- damage = rand();
- while (damage>20){
- damage = damage/5;
- }
- lifeB = lifeB - damage;
- printf("%li lost %d life points has %d left\n", b, damage,lifeB);
- }
- if (lifeA<0){
- printf("\n number %li died", a);
- fainted(head, a);
- break;
- }
- if (lifeB<0){
- printf("\n number %li died \n", b);
- fainted(head, b);
- break;
- }
- }
- i=1;
- }
- }
- }
- }
- }
- TFighter* fainted(TFighter *head, long a)
- {
- TFighter *p,*prev;
- long b;
- printf("\n This is the number of the deceased %li lol", a);
- for(prev=p=head;p!=NULL; )
- {
- b=p->fnom;
- if (b==a)
- {
- if (head==p)
- {
- head = head->next;
- free(p);
- p = prev = head;
- }
- else
- {
- prev->next = p->next;
- free(p);
- p = prev->next;
- }
- }
- else
- {
- prev = p;
- p=p->next;
- }
- }
- printList(head);
- return head;
- }
- int roundContinues (TFighter *head) {
- TFighter *p;
- int f;
- for(f=0,p=head;p!=NULL;p=p->next){
- f++;
- }
- return f;
- }
- void winner (TFighter *head){
- TFighter *p;
- long fnom;
- char name[SIZE];
- int x=0;
- for(p=head;p!=NULL;p=p->next){
- fnom=p->fnom;
- for (x=0;x<SIZE;x++){
- name[x]=p->name[x];
- }
- }
- while(fnom-200>0){
- fnom = fnom-200;
- }
- printf("\nAND THE ULTIMATE WINNER OF THE COMPETITION IS: \n%ld\t%s\n",fnom,name);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement