【问题标题】:Another text based adventure game; while loop battle system另一个基于文本的冒险游戏; while循环战斗系统
【发布时间】: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


【解决方案1】:

以下是您程序中的主要错误:

要使用:system("pause"); 你应该include &lt;windows.h&gt;

要使用:time(NULL) 你应该include &lt;time.h&gt;

string.h 在您的程序中并不是真正需要的。

您尚未初始化 currenthealth,这可能会导致不可预知的结果。

您已经创建了 healthHoodMan_HealthCurrent 之类的变量,但没有使用它们。

rockpaperscissors() 的调用应该在while 块内。 把它放在外面会使循环无限,块中的代码永远不会被执行!

如果你想使用像HoodManDef*0.25这样的浮点运算,你应该将它声明为float,否则它会被四舍五入。

条件else if ( playerschoice != 1||2||3) 是错误的(始终为真)并且是多余的(以上三个条件意味着玩家选择不会是 1,2 或 3 )。 一个简单的else 就足够了。

rockpaperscissors() 是一个空函数。你不能return(0);。请改用简单的return;

您不必每次调用该函数时都使用srand()。您可以简单地在程序开始时或每次游戏开始时使用它(如果您打算使其无需重新启动即可重新播放)。

游戏的一些问题:

您正在将角色的防御添加到对手的攻击中!即我拥有的防御越多,我的对手就会变得越强大。你应该减去。

战士和守护者的防御力如此之强,即使受到兜帽人的攻击,他们的健康也不会减少。

【讨论】:

  • 您好,感谢您的回答,对您有很大帮助!我有两个问题,首先我如何初始化 currenthealth?我对c很陌生。其次,如果玩家获胜,他们会攻击,如果计算机获胜,则攻击?好吧,当我将“else if”更改为“else”时,它可以工作,但它适用于计算机获胜和平局,无论如何我可以只为计算机获胜吗?
  • int currenthealth = 45; //初始化
  • @user3263564 我看不出为什么要替换 else if(playerschoice != 1||2||3) { printf("that was not a choice");返回(0); } with else { printf("那不是一个选择");返回; } 将对计算机获胜或平局产生任何影响。
  • @meryln 再次感谢!不抱歉,也许我没有说清楚。在while循环内。基本上它运行石头剪刀布,如果我赢了我攻击,如果计算机赢了,计算机应该攻击但它不起作用,我想知道这是否与我的 while 循环中的“else if”有关?跨度>
猜你喜欢
  • 1970-01-01
  • 2016-08-25
  • 2013-07-08
  • 1970-01-01
  • 1970-01-01
  • 2018-07-28
  • 1970-01-01
  • 1970-01-01
  • 2021-05-04
相关资源
最近更新 更多