C程序编译并运行,但是在执行时随机跳过if-else评估

安德鲁·吉尔伯特

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

我在编译和运行一个简单程序时遇到了麻烦,但是有时在执行该程序时,它无法正确评估if-else语句,并且会陷入无限循环。

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

它是“字符”类型的两个结构,用于评估攻击值并依次从其每个健康属性中删除该值。应该在调整健康值之后评估特定角色的健康状况是否为0或低于0,并相应地退出循环。

当我只用一个结构体编写它时,它就可以很好地工作,但是现在我在函数中有两个结构体,看起来似乎很成功。

否则,一切正常,随机攻击值计算,运行状况属性的调整,将结构信息打印到控制台,但循环并不会总是中断,因此它并不一定总是将其评估为“ true”。

关于如何使此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;
    
}
dxiv
    AdjustHealth(char2, -(min1 + rand() % diff1), 0);

折痕人物的健康char2->health.currentHealth一定量。

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

当生命值降至0或低于0时,本应结束战斗。

但是由于部分中的“无符号永远无法保持,并且条件仅在true时计算为trueunsigned int currentHealth;< 0ifchar2->health.currentHealth == 0

因此,游戏将继续进行,直到任一角色的生命值完全降低到为止,如果两个currentHealth数字都包裹0在该UINT_MAX范围内的值上,则可能需要很长时间

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

基于用户输入的特定条件的函数评估和运行 if else 循环中的错误

来自分类Dev

编译/运行时的Java枚举评估

来自分类Dev

C程序编译,但是在执行后立即终止

来自分类Dev

如何评估程序的运行时?

来自分类Dev

if-else条件:AND评估之谜

来自分类Dev

if-else条件:AND评估之谜

来自分类Dev

概括编译时和运行时评估

来自分类Dev

IF运算符与If,then,else之间的评估差异

来自分类Dev

克隆功能速度:if / else与短路评估

来自分类Dev

如何客观评估C ++代码段的执行时间?

来自分类Dev

如何客观评估C ++代码段的执行时间?

来自分类Dev

首次执行时无法运行已编译的C ++程序

来自分类Dev

是否在运行时对没有变量的C函数调用进行了预编译或评估?

来自分类Dev

C ++代码编译没有错误,但是在运行时给出了“分段错误”

来自分类Dev

C ++循环的编译时评估

来自分类Dev

Groovy String评估运行时

来自分类Dev

在运行时动态评估代码

来自分类Dev

防止运行时评估

来自分类Dev

Groovy String评估运行时

来自分类Dev

应用程序。评估与Activesheet。评估

来自分类Dev

应用程序。评估与Activesheet。评估

来自分类Dev

运行(C#)算法评估

来自分类Dev

“else”语句在执行时未在“didSelectRowAt”函数中运行

来自分类Dev

MongoDB“评估”执行顺序

来自分类Dev

惰性评估的执行(OCaml)

来自分类Dev

casperjs评估未执行

来自分类Dev

构造函数中表达式的编译时或运行时评估

来自分类Dev

左和右的对象进行评估/运行时或编译时期间解决了吗?

来自分类Dev

如何在允许运行时调用的同时保证算法的编译时评估

Related 相关文章

热门标签

归档