tonibiro

Tutoriat2 pp gladiator

Nov 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct
  6. {
  7.    char nume[100];
  8.    int viata, putere;
  9. } gladiator;
  10.  
  11. void cit_glad(FILE *f, gladiator  *a)
  12. {
  13.     fscanf(f, "%s %d %d", a->nume, &a->viata, &a->putere);
  14. }
  15.  
  16. int codific(char *a)
  17. {
  18.     if(strcmp(a, "strike_high") == 0)
  19.         return 1;
  20.     if(strcmp(a, "strike_low") == 0)
  21.         return 2;
  22.     if(strcmp(a, "block_high") == 0)
  23.         return 3;
  24.     if(strcmp(a, "block_low") == 0)
  25.         return 4;
  26. }
  27.  
  28. void realizeaza_o_actiune(int x, int y, gladiator *a, gladiator *b)
  29. {
  30.     if(x == 2)
  31.     {
  32.         if(y != 4)
  33.             b->viata -= a->putere;
  34.         if(y == 1 || y == 2)
  35.             a->viata -= b->putere/2;
  36.     }
  37.     else
  38.     {
  39.         if(y != 3)
  40.             b->viata -= a->putere;
  41.         if(y == 1  ||  y == 2)
  42.             a->viata -= b->putere/2;
  43.     }
  44. }
  45.  
  46. int cit_lovitura(FILE *f)
  47. {
  48.     char actiune[100];
  49.     fscanf(f, "%s", actiune);
  50.     if(feof(f))
  51.         return -1;
  52.     return codific(actiune);
  53. }
  54.  
  55.  
  56.  
  57. int main()
  58. {
  59.  
  60.     FILE *f = fopen("intrare.in", "r");
  61.     if(f == NULL)
  62.         return 1;
  63.     gladiator a, b;
  64.     cit_glad(f, &a);
  65.     cit_glad(f, &b);
  66.     gladiator *g1 = &a, *g2 = &b;
  67.     while(1)
  68.     {
  69.         int x = cit_lovitura(f);
  70.         if(x == -1)
  71.             break;
  72.         int y = cit_lovitura(f);
  73.         if(y == -1)
  74.             break;
  75.         realizeaza_o_actiune(x, y, g1, g2);
  76.         gladiator *t;
  77.         t = g1;
  78.         g1 = g2;
  79.         g2 = t;
  80.     }
  81.     if(g1->viata > g2->viata)
  82.         printf("a castigat %s v: ", g1->nume);
  83.     else
  84.         printf("a castigat %s", g2->nume);
  85.     return 0;
  86. }
Add Comment
Please, Sign In to add comment