另一种基于文字的冒险游戏而循环战斗系统

时间:2014-03-14 14:07:50

标签: c while-loop

感谢所有试图帮助我的人!

所以这里应该发生的是,如果你运行这个,并选择1,2或3级选择,那么当你进入战斗时你的攻击是不同的等等。

我正在制作它以便你必须赢得石头剪刀才能攻击,所以如果计算机获胜就会攻击你。

对于班级选择1,这是有效的,但是对于其他两个则没有,我为什么会迷失。

我很陌生,如果我错过了一些明显的东西,我很抱歉!

例如,如果你选择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");
        }
    }
}

1 个答案:

答案 0 :(得分:1)

以下是您计划中的主要错误:

要使用:system("pause");您应该include <windows.h>

要使用:time(NULL)您应该include <time.h>

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()。您可以在程序开始时或每个游戏开始时使用它(如果您打算在不重新启动的情况下重新播放它)。

游戏的一些问题:

你正在为对手的攻击添加角色防守!即我的防守越多,我的对手就越强大。你应该减去。

对战士和守护者的防御是如此之多,以至于即使被引诱者袭击他们的健康也不会减少。