# 13787 - Pokemon Battle Manager >author: Utin ###### tags: `struct` --- ## Brief See the code below ## Solution 0 ```c= #include <stdio.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; // TODO: upgrade and recover the Pokémon void level_up(Pokemon *P) { if (P->level < 5) { P->level += 1; 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) { // 助教會拿兩隻死掉Pokemon的出來battle if (A->Hp != 0 && B->Hp != 0) { A->Mp += T; if (A->Mp > A->maxMp) A->Mp = A->maxMp; if (A->Mp >= A->costMp) { int temp = A->attack - B->defense; if (temp < 0) temp = 0; int damage = 2 * A->level * temp / 100; A->Mp -= A->costMp; B->Hp -= damage; 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); } else { 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); } } if (A->Hp == 0 && B->Hp == 0) printf("Draw.\n"); else if (B->Hp == 0) printf("%s is the winner! %s died in vain...\n\n", A->name, B->name); else battle(B, A); } // By Utin ``` ## Reference