当玩家选择攻击时,我遇到了这个问题。它最初不会打印任何东西,但只会在玩家击中骑士时打印。如果玩家在性别中选择了无效的选择,则还会出现其他问题,第一个错误将打印一次,但如果错误再次发生,则会打印两次。我会感激任何帮助,只是做我的小事。项目
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
int main()
{
char enter = 0;
char pNAME[30];
char pGENDER;
char pWEAPON[20];
char pARMOR [10];
int pWEAPONID;
int choice1,choice2 = 0,choice3;
int pAGE;
int pATK = 0;
int pHP = 30;
int pATKS = 10;
int pATKC = 0;
int knightHP = 25;
int knightATKS = 20;
int knightATKC;
int kingHP = 30;
int kingATKC;
srand((unsigned)time(NULL));
printf("While you are enjoying your breakfast at your house, suddenly you get teleported to a different dimension and no one to be seen except an old man.\n");
printf("\nOld Man: What is your name warrior?\n");
printf("\nInsert name:");
scanf("%s",pNAME);
fpurge( stdin );
printf("\nInsert gender (m/f):");
scanf("%c",&pGENDER);
fpurge( stdin );
while ( pGENDER != 'f' &&pGENDER != 'm' )
{
printf("\nInvalid entry, please try again.\n");
printf("\nInsert gender (m/f):");
scanf("%c",&pGENDER);
}
printf("\nOld Man: Now how old are you warrior?\n");
printf("\nInsert Age:");
scanf("%d",&pAGE);
fpurge( stdin );
if ( pAGE < 18)
{
printf("\nOy mate! No stalker!\n");
printf("\nTeleporting back to reality...\n");
return 0;
}
if ( pAGE > 80)
{
printf("\nNo oldies allowed!\n");
printf("\nTeleporting back to reality...\n");
return 0;
}
printf("\nOld Man: Welcome warrior I fear the knights has taken over our kingdom, and you, %s, are the only one who can save us.\n",pNAME);
printf("\n%s: How am I suppose to do that?[Enter]\n",pNAME);
while (enter != '\r' && enter != '\n') { enter = getchar(); }
printf("Old Man: Pick one of these three items\n");
printf("\n[1] Axe : Damage = 7\t Accuracy = 70%% Critical = 7%%\n"); //
printf("\n[2] Sword : Damage = 5\t Accuracy = 77%% Critical = 8%%\n");
printf("\n[3] Knife : Damage = 3\t Accuracy = 83%% Critical = 9%%\n");
printf("\nYou choose: ");
scanf("%d",&pWEAPONID);
if (pWEAPONID == 1)
{
pATK = 8;
pATKS = 14; // 10/14 chance
strcpy(pWEAPON, "Axe");
}
if (pWEAPONID == 2)
{
pATK = 6;
pATKS = 14; // 10/13 chance
strcpy(pWEAPON, "Sword");
}
if (pWEAPONID == 3)
{
pATK = 4;
pATKS = 12; // 10/12 chance
strcpy(pWEAPON, "Knife");
}
if (pWEAPONID > 3)
{
printf("\nYou are gonna be fighting with your bare fist");
pATK = 2;
pATKS = 11; // 10/11 chance
strcpy(pWEAPON, "Fist");
}
printf("\nOld Man: Good choice, you chose the %s, now continue foward to destroy The Evil King\n",pWEAPON);
printf("\nWhile you are continuing your journey to destroy The Evil King, you come face to face with a knight\n");
printf("\nWhat do you want to do?\n");
printf("\n[1] Run [2] Battle:");
scanf("%d",&choice1);
if (choice1 == 1) //Dead
{
printf("\nYou attempted to run, but the knight chases you with his horse and a spear.\n");
printf("\nThe knight stabs you right through your body.\n");
printf("\nYou are dead, thanks for playing.\n");
return 0;
}
while (pHP != 0 && knightHP != 0) // Combat starts
{
knightATKC = rand()%knightATKS; // 15/20 chance
sleep(1);
// Knight's turn
if ( knightATKC == 1 || knightATKC == 2 || knightATKC == 3 || knightATKC == 4 || knightATKC == 5 || knightATKC == 6 || knightATKC == 7 || knightATKC == 8 || knightATKC == 9 || knightATKC == 10 || knightATKC == 11 || knightATKC == 0 )
{
printf("\nThe knight swings his spear at %s\n",pNAME);
printf("The knight hits %s for 4 HP\n",pNAME);
pHP = pHP - 4;
printf("%s has %d/30 HP\n",pNAME,pHP);
if (pHP == 0 || pHP <0)
{
printf("\nYou fell to the ground heavily damages, while the knight lungs at your for the final hit\n");
printf("\nYou are dead, thanks for playing.\n");
return 0;
}
printf("\nWhat do you want to do?\n");
printf("[1] Attack [2] Charge your attack [3] Heal:"); // Charge and Heal not activated yet
scanf("%d",&choice1);
}
else if (knightATKC == 12 || knightATKC == 13 )
{
printf("\nThe knight slashes his spear at %s\n",pNAME);
printf("The knight critically hit %s for 6 HP\n",pNAME);
pHP = pHP - 6;
printf("%s has %d/30 HP\n",pNAME,pHP);
if (pHP == 0 || pHP <0)
{
printf("\nYou fell to the ground heavily damages, while the knight lungs at your for the final hit\n");
printf("\nYou are dead, thanks for playing.\n");
return 0;
}
printf("\nWhat do you want to do?\n");
printf("[1] Attack [2] Charge your attack [3] Heal:"); // Charge and Heal not activated yet
scanf("%d",&choice2);
}
else if (knightATKC == 14)
{
printf("\nThe knight's horse kicks %s\n",pNAME);
printf("The knight's horse hit %s for 8 HP\n",pNAME);
pHP = pHP - 8;
printf("%s has %d/30 HP\n",pNAME,pHP);
if (pHP == 0 || pHP <0)
{
printf("\nYou fell to the ground heavily damages, while the knight lungs at your for the final hit\n");
printf("\nYou are dead, thanks for playing.\n");
return 0;
}
printf("\nWhat do you want to do?\n");
printf("[1] Attack [2] Charge your attack [3] Heal:");
scanf("%d",&choice2);
}
else
{
printf("\nThe knight swings his spear at %s\n",pNAME);
printf("You blocked his spear with your %s\n",pWEAPON);
printf("%s has %d/30 HP\n",pNAME,pHP);
printf("\nWhat do you want to do?\n");
printf("[1] Attack [2] Charge your attack [3] Heal:"); // Charge and Heal not activated yet
scanf("%d",&choice2);
}
// Player's turns
if (choice2 == 1)
{
pATKC = rand()%pATKS;
if (pATKC == 0 || pATKC == 1 || pATKC == 2 || pATKC == 3 || pATKC == 4 || pATKC == 5 || pATKC == 6 || pATKC == 7 || pATKC == 8 || pATKC == 9)
{
printf("\nYou swing your %s at the knight\n",pWEAPON);
printf("You hit the knight with your %d\n",pATK);
knightHP = knightHP - pATK;
printf("The knight has %d/25\n",knightHP);
if (knightHP == 0 || knightHP <0)
{
printf("\nYou fell to the ground heavily damages, while the knight lungs at your for the final hit\n");
printf("\nYou are dead, thanks for playing.\n");
}
printf("\nWhat do you want to do?\n");
printf("[1] Run [2] Attempt to block\n");
scanf("%d",&choice3);
}
else if (pATKC == 10)
{
printf("\nYou swing your %s at the knight\n",pWEAPON);
printf("You critically hit the knight for %f\n",pATK * 1.5);
knightHP = knightHP - (pATK * 1.5);
printf("The knight has %d/25\n",knightHP);
printf("\nWhat do you want to do?\n");
printf("[1] Run [2] Attempt to block\n");
scanf("%d",&choice3);
}
else
{
printf("\nYou swing your %s at the knight\n",pWEAPON);
printf("The knight intercept your %s with his spear\n",pWEAPON);
printf("The knight has %d/25\n",knightHP);
printf("\nWhat do you want to do?\n");
printf("[1] Run [2] Attempt to block\n"); // Not activated yet
scanf("%d",&choice3);
}
}
}
答案 0 :(得分:0)
发布的代码无法编译,因此肯定无法运行。
以下是代码
中列出的众多问题中的一小部分编译时始终启用所有警告。
对于gcc,至少使用:-Wall -Wextra -pedantic
line:column: message
35:5: warning implicit declaration of function `fpurge`
224:5: error: expected declaration or statement at end of input
27:9: warning: unused variable `kingATKC`
26:9: warning: unused variable `kingHP`
15:10: warning: unused variable `pARMOR`
BTW:fpurge
不是stdio.h头文件中支持的函数
建议,而不是致电fpurge()
,使用:
while( '\n' != getchar() );
调用scanf()时,总是检查返回的值,而不是参数值,以确保操作成功
输入“性别”时,将tolower
应用于该值,以便用户可以输入大写或小写
这段代码:围绕第232行杀死英雄应该杀死骑士
if (knightHP == 0 || knightHP <0)
{
printf("\nYou fell to the ground heavily damages, while the knight lungs at your for the final hit\n");
printf("\nYou are dead, thanks for playing.\n");
}
任何死亡,所产生的HP可能小于0但是这一行:
while (pHP != 0 && knightHP != 0) // Combat starts
只有在HP == 0时它才会终止循环:
while (pHP > 0 && knightHP > 0) // Combat starts
建议,对于一个战斗序列,只有一个地方英雄可以输入他们下一步想要做什么的决定。
建议,使每个战斗活动成为一个单独的子功能。
以上两个建议将大大增加每个函数的简单性,使代码更容易调试。