Advertisement
BlueBear

hra_FEI_zadanie.c

Nov 10th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. typedef struct zbran
  7. {
  8.     unsigned int MUL;
  9.     unsigned int SIZE;
  10.     unsigned int BONUS;
  11. }zbran_t;
  12.  
  13. typedef struct bojovnik
  14. {
  15.     unsigned int STR;
  16.     unsigned int DEX;
  17.     unsigned int ARM;
  18.     int HP;
  19.     struct zbran Zbran;
  20. }bojovnik_t;
  21.  
  22. void parseInput(const char *input, bojovnik_t *boj);
  23. unsigned int generator(int a, int b);
  24. int getDamage(zbran_t weapon);
  25. signed int fight(bojovnik_t *boj1, bojovnik_t *boj2);
  26. void init(bojovnik_t *boj, int hp);
  27. void vypis(FILE *fp, bojovnik_t b1, bojovnik_t b2);
  28.  
  29. int main()
  30. {
  31.     int hp1, hp2;
  32.     printf("nacitajte zivoty pre jednotlivych bojovnikov:\n");
  33.     scanf("%d %d", &hp1, &hp2);
  34.     fflush(stdin);
  35.     srand(time(NULL));
  36.     bojovnik_t *bojovnik1 = (bojovnik_t *)malloc(sizeof(bojovnik_t));
  37.     bojovnik_t *bojovnik2 = (bojovnik_t *)malloc(sizeof(bojovnik_t));
  38.     init(bojovnik1, hp1);
  39.     init(bojovnik2, hp2);
  40.     parseInput("2d6+1\n", bojovnik1);
  41.     parseInput("2d6+1\n", bojovnik2);
  42.     while(fight(bojovnik1, bojovnik2) == 2);
  43.  
  44.     return 0;
  45. }
  46.  
  47. void vypis(FILE *fp, bojovnik_t b1, bojovnik_t b2)
  48. {
  49.     fprintf(fp,"\n");
  50.     fprintf(fp,"Bojovnik1\t\tBojovnik2\n");
  51.     fprintf(fp,"arm=: %d\t\t\tarm=: %d\n", b1.ARM, b2.ARM);
  52.     fprintf(fp,"dex=: %d\t\t\tdex=: %d\n", b1.DEX, b2.DEX);
  53.     fprintf(fp,"str=: %d\t\t\tstr=: %d\n", b1.STR, b2.STR);
  54.     fprintf(fp,"hp=: %d\t\t\thp=: %d\n", b1.HP, b2.HP);
  55. }
  56.  
  57. signed int fight(bojovnik_t *boj1, bojovnik_t *boj2)
  58. {
  59.     int dmg = 0, advantage1, advantage2;
  60.  
  61.     dmg = 0;
  62.     advantage1 = generator(0, 10) + boj1->DEX;
  63.     advantage2 = generator(0, 10) + boj2->DEX;
  64.  
  65.     if(advantage1 == advantage2)
  66.     {
  67.         dmg = 0;
  68.         return 2;
  69.     }
  70.     if(advantage1 > advantage2)
  71.     {
  72.         dmg = getDamage(boj1->Zbran) + boj1->STR - boj2->ARM;
  73.         if(dmg <= 0)
  74.         {
  75.             dmg = 1;
  76.         }
  77.         boj2->HP -= dmg;
  78.     }
  79.     if(advantage1 < advantage2)
  80.     {
  81.         dmg = getDamage(boj2->Zbran) + boj1->STR - boj1->ARM;
  82.         if(dmg <= 0)
  83.         {
  84.             dmg = 1;
  85.         }
  86.         boj1->HP -= dmg;
  87.     }
  88.     vypis(stdout, *boj1, *boj2);
  89.     if(boj1->HP < 1)
  90.     {
  91.         return 1;
  92.     }
  93.     if(boj2->HP < 1)
  94.     {
  95.         return -1;
  96.     }
  97.     if(boj1->HP == boj2->HP)
  98.     {
  99.         return 0;
  100.     }
  101.     return 2;
  102. }
  103.  
  104.  
  105.  
  106. int getDamage(zbran_t weapon)
  107. {
  108.     int i;
  109.     int total = 0;
  110.     for(i = 0; i < weapon.MUL; i++)
  111.     {
  112.         total += generator(1, weapon.SIZE);
  113.     }
  114.     return total + weapon.BONUS;
  115. }
  116.  
  117. unsigned int generator(int a, int b)
  118. {
  119.     return a + rand() % (b-a+1);
  120. }
  121.  
  122. void init(bojovnik_t *boj, int hp)
  123. {
  124.     boj->STR = generator(0, 10);
  125.     boj->DEX = generator(0, 10);
  126.     boj->ARM = generator(0, 10);
  127.     boj->HP = hp;
  128. }
  129.  
  130. void parseInput(const char *input, bojovnik_t *boj)
  131. {
  132.     unsigned char *tempBuf = input;
  133.     sscanf(tempBuf, "%d%*c%d%*c%d%[^\n]", &boj->Zbran.MUL, &boj->Zbran.SIZE, &boj->Zbran.BONUS);
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement