首页 > 解决方案 > C 程序编译并运行,但在执行时随机跳过 if-else 评估

问题描述

编辑:填写完整的代码,对不起。

我在编译和运行的简单程序时遇到问题,但有时在执行时它不会正确评估 if-else 语句并运行到无限循环中。

我认为这是非常简单、直接的代码,但由于某种原因它有问题。

它是两个 Character 类型的结构,分别评估攻击值并依次从它们的每个健康属性中删除该数量。它应该在调整健康值后评估特定角色的健康是否等于或低于0并相应地退出循环。

当我只用一个结构编写它时它工作得很好,但现在我在函数中有两个结构,它似乎被击中或错过。

否则一切正常,随机攻击值计算,健康属性的调整,将结构信息打印到控制台,但循环并不总是中断,所以它并不总是像它应该的那样评估为“真”。

对此有何想法可能会使此 if-else 评估变得敏感?

在 Pi 3 B+ 上运行 Raspbian

用 gcc 编译

在 Visual Studio Code 中编写代码

也在运行 Ubuntu 的 ThinkPad 上编译,但问题相同

确切的代码

// main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define WEAPON_INV_SIZE     5

/* Weapon, attack types */
enum Hand_type
{
    ONE_H,
    TWO_H,
    RANGED,
    MAGIC
};

struct Weapon
{
    char            name[64];
    int             level;
    int             minDamage;
    int             maxDamage;
    unsigned char   type;
};

/* Holds player current and maximum health amount */
struct Health
{
    unsigned int    currentHealth;
    unsigned int    maxHealth;
};

/* Holds player attributes */
struct Character
{
    char            name[64];
    unsigned int    expPoints;
    struct Health   health;

    float           attackBonus[4];

    struct Weapon * weaponInventory[WEAPON_INV_SIZE];
    struct Weapon * currentWeapon;
};

/* Holds weapon attributes */
struct Weapon * GetWeapon (char * name, int minDamage, int maxDamage, unsigned char type)
{
    struct Weapon * w = (struct Weapon *)malloc (sizeof(struct Weapon));
    strcpy (w->name, name);
    w->level = 1;
    w->minDamage = minDamage;
    w->maxDamage = maxDamage;
    w->type = type;
    return w;
}

/* Get random int from range between two ints */
int Random_Range (int lower, int greater)
{
    return (rand () % (greater - lower)) + lower;
}

/* Adjust character health */
void AdjustHealth (struct Character * character, int minAmount, int maxAmount)
{
    character->health.currentHealth += minAmount;
    character->health.maxHealth += maxAmount;
}


/* Two character battle sequence */
void Battle_2 (struct Character * char1, struct Character * char2)
{
    int min1 = (int)((float)char1->currentWeapon->minDamage * char1->attackBonus[char1->currentWeapon->type]);
    int max1 = (int)((float)char1->currentWeapon->maxDamage * char1->attackBonus[char1->currentWeapon->type]);
    int diff1 = max1 - min1;

    int min2 = (int)((float)char2->currentWeapon->minDamage * char2->attackBonus[char2->currentWeapon->type]);
    int max2 = (int)((float)char2->currentWeapon->maxDamage * char2->attackBonus[char2->currentWeapon->type]);
    int diff2 = max2 - min2;

    while (1)
    {
        AdjustHealth (char2, -(min1 + rand () % diff1), 0);

        if (char2->health.currentHealth <= 0)
        {
            printf ("Character 2 is dead!!!\n");
            break;
        }
        else
        {
            printf ("%3d/%3d : %3d/%3d\n",  char1->health.currentHealth, 
                                            char1->health.maxHealth,
                                            char2->health.currentHealth, 
                                            char2->health.maxHealth);
        }

        AdjustHealth (char1, -(min2 + rand () % diff2), 0);
        
        if (char1->health.currentHealth <= 0)
        {
            printf ("Character 1 is dead!!!\n");
            break;
        }
        else
        {
            printf ("%3d/%3d : %3d/%3d\n",  char1->health.currentHealth, 
                                            char1->health.maxHealth,
                                            char2->health.currentHealth, 
                                            char2->health.maxHealth);
        }
    }
    
}

int main ()
{
    srand (time (0));

    struct Character myChar01 = 
    {
        "My name",
        0,
        {100, 100},
        {1, 1.5, 1, .75},
        {},
        0
    };

    struct Character myChar02 = 
    {
        "Not my name",
        0,
        {100, 100},
        {1, 1.5, 1, .75},
        {},
        0
    };  

    /* Populate weapon inventory with new weapons */
    myChar01.weaponInventory[0] = GetWeapon ("Dagger", 1, 5, ONE_H);
    myChar01.weaponInventory[1] = GetWeapon ("Sword", 4, 8, TWO_H);
    myChar01.weaponInventory[2] = GetWeapon ("Wood Bow", 1, 6, RANGED);
    myChar01.weaponInventory[3] = GetWeapon ("Root Wand", 3, 7, MAGIC);

    /* Assign current weapons */
    myChar01.currentWeapon = myChar01.weaponInventory[0];
    myChar02.currentWeapon = myChar01.weaponInventory[2];

    /* Enter Battle sequence */
    Battle_2 (&myChar01, &myChar02);
    
    return 0;
    
}

标签: cif-statement

解决方案


    AdjustHealth(char2, -(min1 + rand() % diff1), 0);

这会一定程度上降低角色的生命值char2->health.currentHealth

    if (char2->health.currentHealth <= 0) 

这应该在生命值下降到或低于 0 时结束战斗。

但是由于部分中的“无符号”永远不会成立,并且条件仅在 时计算为真。unsigned int currentHealth;< 0ifchar2->health.currentHealth == 0

所以游戏会一直持续到任何一个角色的生命值恰好降为零,如果两个currentHealth数字都围绕0在该UINT_MAX范围内的值,这可能需要很长时间。


推荐阅读