编辑:填写完整的代码,对不起。
我在编译和运行一个简单程序时遇到了麻烦,但是有时在执行该程序时,它无法正确评估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;
}
AdjustHealth(char2, -(min1 + rand() % diff1), 0);
这德折痕人物的健康char2->health.currentHealth
一定量。
if (char2->health.currentHealth <= 0)
当生命值降至0或低于0时,本应结束战斗。
但是由于该部分中的“无符号”永远无法保持,并且条件仅在true时计算为true。unsigned int currentHealth;
< 0
if
char2->health.currentHealth == 0
因此,游戏将继续进行,直到任一角色的生命值完全降低到零为止,如果两个currentHealth
数字都包裹0
在该UINT_MAX
范围内的值上,则可能需要很长时间。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句