该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
#include
#include
#include
#define MAXLEVEL 5
#define LEVELS 4
#define ENERMYHEAL 50000
#define MYHEAL 500
#define MINFANG -20
struct character { /* 一个人物属性结构体 */
int health;
int shanghai;
int huihe;
int duoduan;
int fang;
} enermy, me;
void enaction(void);
void myaction(void);
int gongji(struct character *zhudong, struct character *beidong);
void showheal(int enheal, int myheal);
int ismax(struct character *ptr);
void showlevel(struct character *ptr);
/* 一个非常简单的文字游戏 */
int main(int argc, char *argv[])
{
getch();
enermy.duoduan = enermy.huihe = enermy.shanghai = enermy.fang = 1; /* 初始化 */
enermy.health = ENERMYHEAL;
me.health = MYHEAL;
me.duoduan = me.huihe = me.shanghai = me.fang = 1;
printf("对方血量:%d\n", ENERMYHEAL);
getch();
printf("我方血量:%d\n", MYHEAL);
getch();
puts("战斗开始!!");
getch();
do { /* 开始 */
enaction();
if(me.health > 0)
myaction();
} while(me.health > 0 && enermy.health > 0);
puts((enermy.health > 0) ? "你输了" : "你赢了");
getch();
return 0;
}
/* 敌人动作():敌人这里进行的动作 */
void enaction(void)
{
int huihe, win = 0, shengji, *jibie = NULL;
struct character *ptr;
static int cishu;
ptr = &me;
srand(time(NULL));
huihe = (enermy.huihe > 1) ? rand() % enermy.huihe + 1 : 1; /* 多回合模式确定 */
if(huihe > 1) {
puts("对方进入多回合模式");
getch();
printf("他霸占了%d回合\n", huihe);
getch();
}
while(huihe-- > 0) {
jibie = NULL;
if(cishu++ < 10 || rand() % 5 < 4 || ismax(&enermy)) { /* 4/5(前10次为100%)概率:进攻 */
puts("对手向你发起进攻");
getch();
win = gongji(&enermy, ptr);
if(win)
break;
} else { /* 升级 */
printf("对方升级了");
do {
shengji = rand() % LEVELS;
switch (shengji) { /* 进行升级 */
case 0:
if(enermy.duoduan != MAXLEVEL) {
puts("多段等级");
jibie = &enermy.duoduan;
}
break;
case 1:
if(enermy.huihe != MAXLEVEL) {
puts("回合");
jibie = &enermy.huihe;
}
break;
case 2:
if(enermy.shanghai != MAXLEVEL) {
puts("攻击力");
jibie = &enermy.shanghai;
}
break;
case 3:
if(enermy.fang != MAXLEVEL) {
puts("防御力");
jibie = &enermy.fang;
}
break;
}
} while(!jibie);
++*jibie;
getch();
}
}
}
/* 进攻(施暴者指针, 被动者指针):对指针指向对象造成伤害,返回是否扣光对方血量 */
int gongji(struct character *zhudong, struct character *beidong)
{
int win = 0, duan, shangh, total = 0, level;
int nopofang;
duan = (zhudong->duoduan > 1) ? rand() % zhudong->duoduan + 1 : 1; /* 多段伤害 */
if(duan > 1) {
puts("爆发多段伤害");
getch();
}
printf("造成伤害:");
while(!win && duan-- > 0) { /* 造成伤害 */
level = (shangh = zhudong->shanghai - beidong->fang) > 0 ? shangh : 1;
shangh = rand() % (level * 10) + level * 10 / 2;
nopofang = 1;
if(zhudong->shanghai == MAXLEVEL) /* 最高攻击破防。1/5概率,0为破 */
nopofang = rand() % 5;
if(shangh > beidong->health) { /* 扣光对方血量 */
shangh = beidong->health;
win = 1;
}
beidong->health -= shangh;
printf("%d", shangh);
if(!nopofang && beidong->fang > MINFANG) { /* 破防 */
printf("(破防)");
beidong->fang--;
}
total += shangh;
printf(" ");
}
printf("(总伤害:%d)", total);
printf("\n");
getch();
return win;
}
/* 我方动作():进行自己的动作 */
void myaction(void)
{
int xuanze, *ptr = NULL, win = 0, huihe;
puts("轮到你了");
getch();
srand(time(NULL));
huihe = (me.huihe > 1) ? rand() % me.huihe + 1 : 1; /* 多回合模式确定 */
if(huihe > 1) {
puts("爆发多回合模式");
getch();
printf("你霸占了%d回合\n", huihe);
getch();
}
while(huihe--) {
ptr = NULL;
showheal(enermy.health, me.health);
printf("敌我等级对照表:\n");
getch();
showlevel(&enermy);
showlevel(&me);
do {
printf("进攻请叩1,加属性请叩2:");
scanf("%d", &xuanze);
if(xuanze == 2 && ismax(&me)) {
puts("属性已全满");
getch();
}
} while((xuanze != 1 && xuanze != 2) || (xuanze == 2 && ismax(&me)));
if(xuanze - 1) { /* 加属性 */
do {
do {
printf("加攻击力请叩1,加多回合请叩2,加多段伤请叩3,加防御力请叩4:");
scanf("%d", &xuanze);
} while(!(xuanze < LEVELS + 1 && xuanze > 0));
switch (xuanze) { /* 选择性加属性 */
case 1:
if(me.shanghai != MAXLEVEL)
ptr = &me.shanghai;
break;
case 2:
if(me.huihe != MAXLEVEL)
ptr = &me.huihe;
break;
case 3:
if(me.duoduan != MAXLEVEL)
ptr = &me.duoduan;
break;
case 4:
if(me.fang != MAXLEVEL)
ptr = &me.fang;
break;
}
if(!ptr) {
puts("该属性已满级");
getch();
}
} while(!ptr);
++*ptr;
} else {
win = gongji(&me, &enermy);
if(win)
break;
}
}
}
/* 显示双方血量(敌方血量, 我方血量):显示双方血量 */
void showheal(int enheal, int myheal)
{
int heal, i = 0;
puts("敌我血量对比");
getch();
heal = 20 * enheal / ENERMYHEAL; /* 敌方血量 */
while(i++ < heal)
printf("血");
while(i++ < 21)
printf(" ");
printf("(%d)\n", enheal);
getch();
i = 0;
heal = 20 * myheal / MYHEAL; /* 我方血量 */
while(i++ < heal)
printf("血");
while(i++ < 21)
printf(" ");
printf("(%d)\n", myheal);
getch();
}
/* 是否完美(结构指针):某角色是否属性最高 */
int ismax(struct character *ptr)
{
int level = 0;
level += ptr->duoduan;
level += ptr->huihe;
level += ptr->shanghai;
level += ptr->fang;
return (level < MAXLEVEL * LEVELS) ? 0 : 1;
}
/* 显示登记(人物指针):显示该人物的等级 */
void showlevel(struct character *ptr)
{
printf("属性等级(1-%d)\n", MAXLEVEL);
getch();
printf("攻击力:%d,多回合:%d,多段伤:%d,防御力:%d\n", ptr->shanghai, ptr->huihe, ptr->duoduan, ptr->fang);
getch();
}