库函数
- 主要使用 随机函数sand() 、srand()
- 时间函数time()
主要代码
# include<stdio.h>
# include<stdlib.h> //提供srand()
# include<time.h> //提供time()
int main()
{
//使用时间作为种子,产生不一样的随机数字
srand( time(NULL) );
// rand(); //每个程序是一致的
// printf("%d",rand());
/***************************
*拳皇规则
*每人HP:100
*攻击力:5—15
*hp1先攻击
*****************************/
int hp1=100,hp2=100; //1p和2p的攻击力
int att1,att2; //1p和2p的攻击力
int i=1; //显示第几轮
int *Att1,*Att2,*Hp1,*Hp2; //仅仅为练习指针的使用
Att1=&att1;Att2=&att2;
Hp1=&hp1; Hp2=&hp2;
while(hp1>0&&hp2>0)//当两个玩家都活着,进行对战
{
//默认1p先攻击
att1 = rand()%11+5; //5-15攻击力
att2 = rand()%11+5;
hp2 -= att1; //玩家1->2
hp1 -= att2; //玩家2->1
if(hp1<1) //hp2死亡
{
printf("hp1--death\n");
// 是为了显示最后一次攻击的结果,验证是否正确
// printf("玩家1 扣血:-%d,玩家1剩余 Hp:%d\n",*Att2,*Hp1);
// printf("玩家2 扣血:-%d,玩家2剩余 Hp:%d\n",*Att1,*Hp2);
break;
}
if(hp2<1) //hp1死亡
{
printf("hp2--death\n");
// 同上
// printf("玩家1 扣血:-%d,玩家1剩余 Hp:%d\n",*Att2,*Hp1);
// printf("玩家2 扣血:-%d,玩家2剩余 Hp:%d\n",*Att1,*Hp2);
break;
}
printf("第%d轮\n",i++);
printf("*************************\n");
printf("玩家1 扣血:-%d,玩家1剩余 Hp:%d\n",att2,hp1);
printf("玩家2 扣血:-%d,玩家2剩余 Hp:%d\n",att1,hp2);
printf("*************************\n");
}
return 0;
}