输入对战双方名字后即可开始精(无)彩(聊)刺(透)激的对战。由于命中判定使用了随机数,使得每次对战的结果不可预测
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
int main() {
char p1[20],p2[20];
int p1hp,p2hp;
printf("======= 姓名对战v0.1 =======\n");
printf("玩家1姓名:");
scanf("%s",p1);
printf("玩家2姓名:");
scanf("%s",p2);
printf("\n========= 对战开始 =========\n");
p1hp = p2hp = 100 ;
printf("体力100 %s 大战 %s 体力100\n",p1,p2);
getch();
srand(time(NULL)); // 用当前系统时间time(NULL)作为随机种子
// 第1回合 --------------------------------------------
printf("\n第1回合\n");
getch();
printf("%s出招!",p1);
if (rand()%2==0) {
getch();
p2hp -= 50 ;
printf("%s中招!体力减至%d!\n",p2,p2hp);
getch();
} else {
getch();
printf("%s躲开了攻击!体力%d!\n",p2,p2hp);
getch();
}
printf("%s出招!",p2);
if (rand()%2==0) {
getch();
p1hp -= 50 ;
printf("%s中招!体力减至%d!\n",p1,p1hp);
getch();
} else {
getch();
printf("%s躲开了攻击!体力%d!\n",p1,p1hp);
getch();
}
// 第2回合 --------------------------------------------
printf("\n第2回合\n");
getch();
printf("%s出招!",p1);
if (rand()%2==0) {
getch();
p2hp -= 50 ;
printf("%s中招!体力减至%d!\n",p2,p2hp);
if (p2hp = = 0) {
getch();
printf("\n%s胜利!游戏结束!\n\n",p1);
exit(0);
}
getch();
} else {
getch();
printf("%s躲开了攻击!体力%d!\n",p2,p2hp);
getch();
}
printf("%s出招!",p2);
if (rand()%2==0) {
getch();
p1hp -= 50 ;
printf("%s中招!体力减至%d!\n",p1,p1hp);
if (p1hp = = 0) {
getch();
printf("\n%s胜利!游戏结束!\n\n",p2);
exit(0);
}
getch();
} else {
getch();
printf("%s躲开了攻击!体力%d!\n",p1,p1hp);
getch();
}
// 第3回合 --------------------------------------------
printf("\n第3回合\n");
getch();
printf("%s出招!",p1);
if (rand()%2==0) {
getch();
p2hp -= 50 ;
printf("%s中招!体力减至%d!\n",p2,p2hp);
if (p2hp = = 0) {
getch();
printf("\n%s胜利!游戏结束!\n\n",p1);
exit(0);
}
getch();
} else {
getch();
printf("%s躲开了攻击!体力%d!\n",p2,p2hp);
getch();
}
printf("%s出招!",p2);
if (rand()%2= =0) {
getch();
p1hp -= 50 ;
printf("%s中招!体力减至%d!\n",p1,p1hp);
if (p1hp == 0) {
getch();
printf("\n%s胜利!游戏结束!\n\n",p2);
exit(0);
}
getch();
} else {
getch();
printf("%s躲开了攻击!体力%d!\n",p1,p1hp);
getch();
}
// 游戏结束 胜负判定 ------------------------------------
printf("\n3回合结束!\n");
printf("体力%d %s 大战 %s 体力%d\n",p1hp,p1,p2,p2hp);
if ( p1hp>p2hp )
printf("%s胜利!",p1);
else if ( p1hp<p2hp )
printf("%s胜利!",p2);
else
printf("平局!");
printf("游戏结束!\n\n");
getch();
return 0;
}
接下来升级为0.2版,现在需要使用循环结构while语句对源程序进行改进:
加入以下特性:
-
血战到底:直到一方倒下为止,没有平局。
-
伤害随机:每次攻击伤害在0-100之间随机变化。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
int main() {
char p1[20],p2[20];
int p1hp,p2hp,t;
t=1;
printf("======= 姓名对战v0.1 =======\n");
printf("玩家1姓名:");
scanf("%s",p1);
printf("玩家2姓名:");
scanf("%s",p2);
printf("\n========= 对战开始 =========\n");
p1hp = p2hp = 100 ;
printf("体力100 %s 大战 %s 体力100\n",p1,p2);
getch();
srand(time(NULL));
while(p1hp>0&&p2hp>0){
// 第t回合 --------------------------------------------
printf("\n第%d回合\n",t);
getch();
printf("%s出招!",p1);
if (rand()%2==0) {
getch();
p2hp -= rand() % 100+1 ;
if(p2hp<0) {
p2hp=0;
getch();
printf("%s中招!体力减至%d!\n",p2,p2hp);
printf("\n%s胜利!游戏结束!\n\n",p1);
exit(0);
}
printf("%s中招!体力减至%d!\n",p2,p2hp);
getch();
} else {
getch();
printf("%s躲开了攻击!体力%d!\n",p2,p2hp);
getch();
}
printf("%s出招!",p2);
if (rand()%2==0) {
getch();
p1hp -= rand() % 100+1 ;
if(p1hp<0) {
p1hp=0;
getch();
printf("%s中招!体力减至%d!\n",p1,p1hp);
printf("\n%s胜利!游戏结束!\n\n",p2);
exit(0);
}
printf("%s中招!体力减至%d!\n",p1,p1hp);
getch();
} else {
getch();
printf("%s躲开了攻击!体力%d!\n",p1,p1hp);
getch();
}
t++;
}
// 游戏结束 胜负判定 ------------------------------------
printf("\n%d回合结束!\n",t-1);
printf("体力%d %s 大战 %s 体力%d\n",p1hp,p1,p2,p2hp);
if ( p1hp>p2hp )
printf("%s胜利!",p1);
else if ( p1hp<p2hp )
printf("%s胜利!",p2);
else
printf("平局!");
printf("游戏结束!\n\n");
getch();
return 0;
}
由此可发现while语句在程序的编写当中发挥了重要作用,更加方便了我们对代码的编写,相对于if来说有了更加深层次的逻辑
注: 从老师那学来的~~~~