Untitled

 avatar
unknown
c_cpp
3 years ago
2.7 kB
6
Indexable
#include <stdio.h>
#include <string.h>

#define lvup_atk 200
#define lvup_dfn 100
#define lvup_maxHp 20
#define lvup_maxMp 15
#define T 2

typedef struct pokemon{
    int id;
    char name[10];
    int level;
    int attack;
    int defense;
    int Hp;
    int Mp;
    int maxHp;
    int maxMp;
    int costMp;
}Pokemon;

int max(int a, int b){
    if(a > b) return a;
    else return b;
}
// TODO: upgrade and recover the Pokémon
void level_up(Pokemon *P){
    if(P->level < 5){
        P->level++;
        P->attack += lvup_atk;
        P->defense += lvup_dfn;
        P->maxHp += lvup_maxHp;
        P->maxMp += lvup_maxMp;
        P->Hp = P->maxHp;
        P->Mp = P->maxMp;
    }
}

// TODO: simulate and print the process of the battle
void battle(Pokemon *A, Pokemon *B){
    int turn = 0;
    while(1){

        if(A->Hp == 0 && B->Hp != 0){
            printf("%s is the winner! %s died in vain...\n\n", B->name, A->name);
            return;
        }
        else if(B->Hp == 0 && A->Hp != 0){
            printf("%s is the winner! %s died in vain...\n\n", A->name, B->name);
            return;
        }
        else if(A->Hp == 0 && B->Hp == 0){
            printf("Draw.\n\n");
            return;
        }

        if(turn == 0){
            A->Mp += T;
            if(A->Mp >= A->maxMp) A->Mp = A->maxMp;//注意Mp不能大於最大Mp
            if(A->Mp < A->costMp){
                A->Mp += T;
                if(A->Mp >= A->maxMp) A->Mp = A->maxMp;
                printf("%s used Rest!\n", A->name);
                printf("%s now has %d MP.\n", A->name, A->Mp);
            }
            else{
                A->Mp -= A->costMp;
                B->Hp -= 2*(A->level)*max(A->attack-B->defense, 0)/100;
                if(B->Hp < 0) B->Hp = 0;
                printf("%s used Headbutt!\n", A->name);
                printf("%s now has %d HP.\n", B->name, B->Hp);
            }
            turn = 1;
        }
        else{
            B->Mp += T;
            if(B->Mp >= B->maxMp) B->Mp = B->maxMp;
            if(B->Mp < B->costMp){
                B->Mp += T;
                if(B->Mp > B->maxMp) B->Mp = B->maxMp;
                printf("%s used Rest!\n", B->name);
                printf("%s now has %d MP.\n", B->name, B->Mp);
            }
            else{
                B->Mp -= B->costMp;
                A->Hp -= 2*(B->level)*max(B->attack-A->defense, 0)/100;
                if(A->Hp < 0) A->Hp = 0;
                printf("%s used Headbutt!\n", B->name);
                printf("%s now has %d HP.\n", A->name, A->Hp);
            }
            turn = 0;
        }
    }
}
Editor is loading...