BlueBear icon

hra_FEI_zadanie.c

BlueBear | PRO | 11/10/13 08:18:08 PM UTC | 0 ⭐ | 359 👁️ | Never ⏰ | []
C |

3 KB

|

None

|

0 👍

/

0 👎

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
 
typedef struct zbran
{
    unsigned int MUL;
    unsigned int SIZE;
    unsigned int BONUS;
}zbran_t;
 
typedef struct bojovnik
{
    unsigned int STR;
    unsigned int DEX;
    unsigned int ARM;
    int HP;
    struct zbran Zbran;
}bojovnik_t;
 
void parseInput(const char *input, bojovnik_t *boj);
unsigned int generator(int a, int b);
int getDamage(zbran_t weapon);
signed int fight(bojovnik_t *boj1, bojovnik_t *boj2);
void init(bojovnik_t *boj, int hp);
void vypis(FILE *fp, bojovnik_t b1, bojovnik_t b2);
 
int main()
{
    int hp1, hp2;
    printf("nacitajte zivoty pre jednotlivych bojovnikov:\n");
    scanf("%d %d", &hp1, &hp2);
    fflush(stdin);
    srand(time(NULL));
    bojovnik_t *bojovnik1 = (bojovnik_t *)malloc(sizeof(bojovnik_t));
    bojovnik_t *bojovnik2 = (bojovnik_t *)malloc(sizeof(bojovnik_t));
    init(bojovnik1, hp1);
    init(bojovnik2, hp2);
    parseInput("2d6+1\n", bojovnik1);
    parseInput("2d6+1\n", bojovnik2);
    while(fight(bojovnik1, bojovnik2) == 2);
 
    return 0;
}
 
void vypis(FILE *fp, bojovnik_t b1, bojovnik_t b2)
{
    fprintf(fp,"\n");
    fprintf(fp,"Bojovnik1\t\tBojovnik2\n");
    fprintf(fp,"arm=: %d\t\t\tarm=: %d\n", b1.ARM, b2.ARM);
    fprintf(fp,"dex=: %d\t\t\tdex=: %d\n", b1.DEX, b2.DEX);
    fprintf(fp,"str=: %d\t\t\tstr=: %d\n", b1.STR, b2.STR);
    fprintf(fp,"hp=: %d\t\t\thp=: %d\n", b1.HP, b2.HP);
}
 
signed int fight(bojovnik_t *boj1, bojovnik_t *boj2)
{
    int dmg = 0, advantage1, advantage2;
 
    dmg = 0;
    advantage1 = generator(0, 10) + boj1->DEX;
    advantage2 = generator(0, 10) + boj2->DEX;
 
    if(advantage1 == advantage2)
    {
        dmg = 0;
        return 2;
    }
    if(advantage1 > advantage2)
    {
        dmg = getDamage(boj1->Zbran) + boj1->STR - boj2->ARM;
        if(dmg <= 0)
        {
            dmg = 1;
        }
        boj2->HP -= dmg;
    }
    if(advantage1 < advantage2)
    {
        dmg = getDamage(boj2->Zbran) + boj1->STR - boj1->ARM;
        if(dmg <= 0)
        {
            dmg = 1;
        }
        boj1->HP -= dmg;
    }
    vypis(stdout, *boj1, *boj2);
    if(boj1->HP < 1)
    {
        return 1;
    }
    if(boj2->HP < 1)
    {
        return -1;
    }
    if(boj1->HP == boj2->HP)
    {
        return 0;
    }
    return 2;
}
 
 
 
int getDamage(zbran_t weapon)
{
    int i;
    int total = 0;
    for(i = 0; i < weapon.MUL; i++)
    {
        total += generator(1, weapon.SIZE);
    }
    return total + weapon.BONUS;
}
 
unsigned int generator(int a, int b)
{
    return a + rand() % (b-a+1);
}
 
void init(bojovnik_t *boj, int hp)
{
    boj->STR = generator(0, 10);
    boj->DEX = generator(0, 10);
    boj->ARM = generator(0, 10);
    boj->HP = hp;
}
 
void parseInput(const char *input, bojovnik_t *boj)
{
    unsigned char *tempBuf = input;
    sscanf(tempBuf, "%d%*c%d%*c%d%[^\n]", &boj->Zbran.MUL, &boj->Zbran.SIZE, &boj->Zbran.BONUS);
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  • Syutrsz icon
    03/31/26 10:26:22 PM UTC
    text |

    0 B

    |

    0 👍

    /

    0 👎

    Best t33n active cpx link daily updates!
     Copy and paste the link in a new browser tab and then hit enter!
     https://crazyporn.@xxx/members/157354/?asgtbndr=1&play=true&spon=adworld&zone=400
     Remove the @ 
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎