【发布时间】:2014-03-14 14:07:49
【问题描述】:
感谢任何试图帮助我的人!
所以这里应该发生的是,如果你运行它,并且选择职业选择 1,2 或 3,那么当你进入战斗时,你的攻击就会不同等等。
我这样做是为了让你必须赢得剪刀石头布才能进行攻击,所以如果计算机获胜,它就会攻击你。
对于班级选择 1,这是有效的,但对于其他两个则无效,我不知道为什么。
我对 c 很陌生,如果我遗漏了一些明显的东西,我很抱歉!
例如,如果你选择职业选择 3,守卫,并且你输赢了剪刀石头布游戏,什么都不会发生,应该让你攻击或他攻击
#include<stdio.h>
#include<string.h>
int i;
int playerschoice, compchoice;
main()
{
int i;
int choice1,choice2;
int class_choice,warrior,rogue,guardian;
int HoodMan_Health = 30;
int HoodMan_HealthCurrent;
int HoodManAtk = 25;
int HoodManDef = 15;
int RogueAtk = 100;
int RogueDef = 10;
int WarriorAtk = 50;
int WarriorDef = 50;
int GuardianAtk = 10;
int GuardianDef = 100;
int health = 100;
int currenthealth;
int difficulty;
int level;
printf("\n1.Rogue [100atck 10def]\n\n2.Warrior [50atck 50def]\n\n3.Guardian [10atck 100def]\n");
printf("\nYour choice?\t");
scanf("%i",&class_choice);
if (class_choice == 1 || class_choice == 2 ||class_choice == 3)
{
printf("\nLets play...\n\n");
system ("PAUSE");
}
else
{
printf("\nThat was not a choice\n");
return(0);
}
while ( (currenthealth>0)&&(HoodMan_Health>0) ) // while both healths are above zero do this battle
rockpaperscissors();
{
if (((playerschoice == 1)&&(compchoice == 3)) || ((playerschoice == 2)&&(compchoice == 1)) || ((playerschoice == 3)&&(compchoice == 1)))
{
printf("You attack the hooded man\n");
if (class_choice == 1)
{
HoodMan_Health=HoodMan_Health-(RogueAtk*0.5+HoodManDef*0.25);
printf("The Hooded Man's health is now %i\n\n",HoodMan_Health);
}
else if (class_choice == 2)
{
HoodMan_Health=HoodMan_Health-(WarriorAtk*0.5+HoodManDef*0.25);
printf("The Hooded Man's health is now %i\n\n",HoodMan_Health);
}
else if (class_choice == 3)
{
HoodMan_Health=HoodMan_Health-(GuardianAtk*0.5+HoodManDef*0.25);
printf("The Hooded Man's health is now %i\n\n",HoodMan_Health);
}
}
else if (((playerschoice == 3)&&(compchoice == 1)) || ((playerschoice == 1)&&(compchoice == 2)) || ((playerschoice == 1)&&(compchoice == 3)))
{
printf("The Hooded Man attacks you\n");
if (class_choice == 1)
{
currenthealth=currenthealth-(HoodManAtk+RogueDef*0.5);
printf("Your health is now %i\n\n\n",currenthealth);
}
else if (class_choice == 2)
{
currenthealth=currenthealth-(HoodManAtk+WarriorDef*0.5);
printf("Your health is now %i\n\n\n",currenthealth);
}
else if (class_choice == 3)
{
currenthealth=currenthealth-(HoodManAtk+GuardianDef*0.5);
printf("Your health is now %i\n\n\n",currenthealth);
}
}
}
if (currenthealth<0)
{
printf("You died\n");
return (0);
}
else
{
printf("You killed the hooded man\n");
}
}
void rockpaperscissors()
{
printf("Enter 1 for Rock, 2 for Paper and 3 for Scissors\n");
scanf("%i",&playerschoice);
if ( playerschoice == 1 )
{
printf("You are going with: Rock...\n");
}
else if ( playerschoice == 2 )
{
printf("You are going with: Paper...\n");
}
else if ( playerschoice == 3 )
{
printf("You are going with: Scissors...\n");
}
else if ( playerschoice != 1||2||3)
{
printf("that was not a choice");
return(0);
}
// initialize random seed: //
srand (time(NULL));
// set compchoice to random number from 1 to 3 //
compchoice=rand() %3+1;
if (compchoice == 1)
{
printf("\nThe computer is going with: Rock...\n\n");
}
else if (compchoice == 2)
{
printf("\nThe computer is going with: Paper...\n\n");
}
else if (compchoice == 3)
{
printf("\nThe computer is going with: Scissors...\n\n");
}
{
if (((playerschoice == 1)&&(compchoice == 3)) || ((playerschoice == 2)&&(compchoice == 1)) || ((playerschoice == 3)&&(compchoice == 1)))
{
printf("you win\n");
}
else if (((playerschoice == 3)&&(compchoice == 1)) || ((playerschoice == 1)&&(compchoice == 2)) || ((playerschoice == 1)&&(compchoice == 3)))
{
printf("you lose\n");
}
else if (((playerschoice == 1)&&(compchoice == 1)) || ((playerschoice == 2)&&(compchoice == 2)) || ((playerschoice == 3)&&(compchoice == 3)))
{
printf("it's a draw\n");
}
}
}
【问题讨论】:
-
仅供参考,'else if (playerschoice != 1||2||3)' 将始终评估为 true。
-
请注意 1.
while-loop 的主体完全仅由rockpaperscissors();组成,之后您有一个复合语句(只执行一次)。并且 2. 你在rockpaperscissors的末尾有无用的复合语句,你可能打算制作if/else语句的主体。提示:正确的格式和缩进确实有助于尽早发现这些东西。 -
另请注意,您正在混合整数和浮点数学。这将导致截断。例如,'HoodManDef*.25' = 3.75(当 HoodManDef= 15 时)但当转换回 int 时,结果将为 3。
-
这个问题没有问题,只是一堆事实,然后是一大块代码。 你有什么问题?
标签: c while-loop