我正在为一个大学项目制作一个基于文本的RPG游戏,我遇到了一个逻辑错误 - 我认为这是一个逻辑错误,因为它不会产生我期待的结果。
我正在设计一个基于回合制的系统,我想要一个自动功能,将敌人的等级与玩家的等级相匹配,并使其高出2.因此,无论玩家的等级如何,此功能都将匹配敌人的等级。等级,高于2级。
此代码如下所示:
#ifndef ADD_H
#define ADD_H
#include <string>
#include <locale>
#include <ctype.h>
#include <iostream>
#include <cstdlib>
#include <unistd.h>
unsigned int sleep(unsigned int seconds);
using namespace std;
class Character
{
private:
/*
* The UNDERSCORE is mainly so you know it's private!
*/
string _name;
int _level;
int _health;
int _hpThres;
int _exp;
int _expThres;
int _bonusDEF; // UNUSED
int _bonusATK; // UNUSED
int _totalATK; // UNUSED
int _attack;
int _damageMin;
int _damageMax;
int _defense;
public:
Character();
Character(const string name);
virtual ~Character();
//Functions
void init (const string name);
string getAsString();
// THIS THE FUNCTION I'M CURRENTLY HAVING TROUBLES WITH
void levelMatch(int lvl)
{
this->_level = lvl + _level;
}
//The functions below have the same format and they work, but the one above simply doesn't
void setBonus(int n, int m)
{
this->_attack = _attack + n;
this->_defense = _defense + m;
}
void levelUp(int exp)
{
while (this->_exp >= this->_expThres)
{
this->_exp -= this->_expThres;
this->_level++;
this->_expThres = (_level*100) - 50;
}
}
void damage(int dmg)
{
this->_health = _health -= dmg;
}
//Accessors
inline const string& getName() const {return this->_name;}
inline const int& getLevel() const {return this->_level;}
inline const int& getHealth() const {return this->_health;}
inline const int& getHPThres() const {return this->_hpThres;}
inline const int& getEXP() const {return this->_exp;}
inline const int& getEXPThres() const {return this->_expThres;}
inline const int& getAttack() const {return this->_attack;}
inline const int& getDamageMin() const {return this->_damageMin;}
inline const int& getDamageMax() const {return this->_damageMax;}
inline const int& getDefense() const {return this->_defense;}
//Modifiers
};
#endif
这是我在主文件中使用该功能的地方:下面是上半部分很好,这是我遇到问题的下半部分。
#include "add.h"
#include <climits>
///TOP HALF OF MY CODE
void setName()
{
// In case you forget, which you will, to print something from a class
// Make an object class, using <CLASS NAME> OBJECT (INITIALIZE VARIBLE);
// Now, since this is not yet optimized, other stats need to be initialized independently,
// So, <CLASS OBJECT> SET FUNCTION (THE VARIABLE) will give that object that value,
// To print it use: <CLASS OBJECT>.GET FUNCTION();
// IMPORTANT NOTE: The GET FUNCTION is the value you need here. If you need to calculate something,
// use the the GET FUNCTION by calling the Class Object (dot) GET FUNCTION.
string name;
cout << "Enter the name of your character: ";
cin >> name;
Character user (name);
}
void pressAnyKey(void)
{
cout << "\nPress ENTER to continue...";
cin.ignore();
}
//Above you can find amazing functions that you needed for future reference
int main()
{
string YN; // Sigh...
cout << " \n";
cout << "*****************************************" << endl;
cout << "* *" << endl;
cout << "* SECOND BRAVES: THE WAR BELL TOLLS *" << endl;
cout << "* *" << endl;
cout << "*****************************************\n";
pressAnyKey();
string name;
cout << "Enter the name of your character: ";
cin >> name;
Character user (name);
//cout << print the story
sleep(1);
//Right so the section below may look intimidating, but let me explain
//The Do-While Switch Loop is to increment two loops,
//The main loop is the first you see, while has the option to choose the weapons,
//The second is morer about whether you want said-weapon.
//The second loop will break when you respond Yes or No.
//Saying Yes will break both loops as I have instructed,
//Saying No will sent the user back to the main loop (with the "goto" command).
//The Cin.fail doesn't seem to work as I wanted to, but it does the important bit,
//which is to prevent the loop from going beserk if the user is stupid
do
{
again:
int weapon;
cout << "\nChoose your weapon: " << endl;
cout << "************" << endl;
cout << "* *" << endl;
cout << "* 1. Sword *" << endl;
cout << "* 2. Axe *" << endl;
cout << "* 3. Lance *" << endl;
cout << "* *" << endl;
cout << "************" << endl;
cin >> weapon;
while (cin.fail()) //Meant to catch the wrong type and ignore it
{
cin.clear();
cin.ignore(INT_MAX, 1);
cin.get();
}
switch(weapon)
{
case 1:
{
cout << "This is the Hero Sword," << endl;
cout << "Very high on Attack, but low on Defense!\n";
cout << "Do you want to choose this weapon? (Y/N)" << endl;
cin >> YN;
if (YN == "Y")
{
int A = 3;
int D = 1;
user.setBonus(A,D);
cout << "You chose the Hero Sword!" << endl;
break;
}
else
{
goto again;
}
}
case 2:
{
cout << "This is the Hero Axe," << endl;
cout << "It has balanced Attack and Defense!\n";
cout << "Do you want to choose this weapon? (Y/N)" << endl;
cin >> YN;
if (YN == "Y")
{
int A = 2;
int D = 2;
user.setBonus(A,D);
cout << "You chose the Hero Axe!" << endl;
break;
}
else
{
goto again;
}
}
break;
case 3:
{
cout << "This is the Hero Lance," << endl;
cout << "Very high Defense, but low on Attack!\n";
cout << "Do you want to choose this weapon? (Y/N)" << endl;
cin >> YN;
if (YN == "Y")
{
int A = 2;
int D = 2;
user.setBonus(A,D);
cout << "You chose the Hero Lance!" << endl;
break;
}
else
{
goto again;
}
}
default:
{
cout << "\nChoose a weapon, BASED ON THE NUMBERS AVAILABLE!" << endl;
goto again;
}
}
} while (YN != "Y");
///我的问题所在代码的BOTTOM HALF
string N1;
N1 = "Shadow Armour";
Character Enemy1 (N1);
Enemy1.levelMatch(user.getLevel() + 2); // Doesn't work.
sleep(1);
do
{
back:
int choice;
cout << "A wild " << Enemy1.getName() << " is challenging you to battle!" << endl;
sleep(1);
cout << "What do you want to do?" << endl;
cout << "* 1. Attack *" << endl;
cout << "* 2. Guard *" << endl;
cout << "* 3. Brave *" << endl;
cout << "* 4. Run *" << endl;
cout << "***********************" << endl;
cout << "* " << user.getName() << ": " << user.getHealth() << " Hit Points left!" << endl;
cout << "***********************" << endl;
cout << "* " << Enemy1.getName() << ": " << Enemy1.getHealth() << " Hit Points left!" << endl;
cout << "***********************" << endl;
cin >> choice;
switch (choice)
{
case 1:
{
int BattleDamage;
int RandomDamage = rand() % (user.getDamageMin() - user.getDamageMax() + 1) + user.getDamageMin();
BattleDamage = Enemy1.getHealth() + Enemy1.getDefense() - RandomDamage;
Enemy1.damage(BattleDamage);
cout << "You attacked the monster! You did " << BattleDamage << " of damage!" << endl;
break;
}
case 2:
cout << "ye" << endl;
break;
case 3:
cout << "ye" << endl;
break;
case 4:
cout << "ye" << endl;
break;
default:
{
cout << "\nChoose an option, BASED ON THE NUMBERS AVAILABLE!" << endl;
goto back;
}
}
} while (user.getHealth() > 0);
}
add.cpp文件:
#include "add.h"
#include <string>
#include <math.h>
using namespace std;
Character::Character()
{
}
Character::Character(const string name)
{
this->_name = name;
this->_level = 1;
this->_health = this->_level+16;
this->_hpThres = this->_level+16;
this->_exp = (this->_level*100)-50;
this->_expThres = (this->_level*100)-50;
this->_attack = (this->_level+3);
this->_defense = round((_level+2)*1.5);
this->_damageMin = this->_attack-2;
this->_damageMax = this->_attack+2;
}
Character::~Character()
{
}
//Functions
//void Character::init(const string name){ }
string Character::getAsString()
{
}
我知道它不起作用,因为计算应该使敌人命中点高出2,但是当它被编译并运行时,敌人的生命值与玩家相同,这意味着该功能从未在第一时间起作用
运行代码的输出:
A wild Shadow Armour is challenging you to battle!
What do you want to do?
* 1. Attack *
* 2. Guard *
* 3. Brave *
* 4. Run *
***********************
* a: 17 Hit Points left!
***********************
* Shadow Armour: 17 Hit Points left! <-- This should have been 19, not 17.
17表示该功能被忽略,从未奏效。
我是C ++的新手,所以我理解我的代码是否很糟糕。 提前谢谢!