- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在监督一个技术营,其中一名营员为基于文本的视频游戏创建了一些代码,但他在显示结果时遇到了问题。虽然程序编译并正确运行,但选择“治愈”时,它不会增加玩家的健康,当用户选择“攻击”时,我们也会获得零。我在编程方面的知识有限,我正在尽我所能帮助他,这样他在这里的经历就会愉快而充实。如果您能提供任何帮助或建议,我们将不胜感激。这是代码:
// Test for hard stuff.cpp : Defines the entry point for the console application.
//
// Bigger proj
// Constructors will make characters with rolling statistics
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
// declaring function for hit power
//int power( int str, int def);
int command;
class character
{
public:
character();
//~character();
string name;
float str;
float def;
float health; // hit points
float regen; // health regen amount
float roll; // for random value
float ouch; // amount of attack damage
float getAttack(void);
float getHeal(void);
void setRegen(float reg);
//void setHeal(float healAmt);
private:
};
character::character()
{
srand(time_t(NULL));
str = rand() % 30 + 5;
def = rand() % 30 + 5;
health = 100;
//Output to check the constructor is running properly
cout<< "Character has been created.\n";
}
void character::setRegen( float reg )
{
regen = reg;
}
float character::getAttack()
{
//defines the magnitude/power of attack
//function shows how much damage is inflicted
// ouch is how much damage is done
roll = rand() % 20 + 1; // range between 1 &20
if (roll <= 11)
{
ouch = str - (def /2);
}
else if ((roll <= 17) && (roll >= 12))
{
ouch = (str * 2) - (def / 2);
}
else if ((roll <= 20) && (roll >= 18))
{
ouch = (str * 3) - (def / 2);
//cout << "CRITICAL HIT!!";
}
return ouch;
}
float character::getHeal()
{
//this is what happens when you chose to heal
regen = rand() % 20 + 3;
cout << "regen value= " << regen<< ".\n";
return regen;
}
/*character::~character()
{
str = 0;
def = 0;
health = 0;
// Output to check the destructor is running properly
cout << "Character has been destroyed\n";
} */
int _tmain(int argc, _TCHAR* argv[])
{
//Class objects
character user, computer;
//Hard code in a name for the computer's player
computer.name = "ZOID\n";
float attackDamage;
float healthAdded;
user.setRegen(void);
//Recieve data for the user's player
cout<< "Please enter a name for your character:\n";
cin>> user.name;
//Output name and stats to the user
cout<< "\nYour name is: " << user.name << endl;
cout << "here are your statistics: \n"
<< "strength: " << user.str << endl
<< "Defense: " << user.def << endl
<< "Health: " << user.health << endl;
cout<< "oh no an oppenent appeared!!!\n";
cout<< "you will have to fight him!" << endl<< endl;
cout << "opponent's health: 100" << endl
<< "what would you like to do: heal (1), attack(2), or run(3).\n";
cin>> command;
switch(command)
{
case 1 :
healthAdded = user.getHeal();
cout<< ""<<user.name <<" has regenerated " << healthAdded << " health.\n";
break;
case 2 :
attackDamage = user.getAttack();
cout << "" <<user.name <<" did " << attackDamage << " damage to the opponent!\n";
break;
case 3:
cout<< ""<<user.name<<" got away!\n";
break;
default:
cout<< "Please enter a valid choice!";
} //end switch
return 0;
}
最佳答案
我会尽我所能一次提供帮助。我的行号可能与您的略有不同,因此请随意浏览一下。
在:
115 user.setRegen(void);
setRegen
声明为采用float
:
20 class character
21 {
22 public:
.
.
.
34 void setRegen(float reg);
所以你不能传递void
。顺便说一句,在 C++ 中,通常在调用不带参数的函数时不传递任何内容,而不是传递显式 void
。但是,显式 void
是可以的。
getHeal()
函数计算一个随机数来治疗角色,但它实际上并不增加 health
成员变量。您可以通过这种方式实现修复,请参见第 92 行:
87 float character::getHeal()
88 {
89 //this is what happens when you chose to heal
90 regen = rand() % 20 + 3;
91 cout << "regen value= " << regen<< ".\n";
92 health += regen;
93 return regen;
94 } Z
你在攻击时也不会降低对手的生命值。一种方法是将对手的引用传递给 getAttack()
并在那里修改它:
58 float character::getAttack(character& opponent)
59 {
60 //defines the magnitude/power of attack
61 //function shows how much damage is inflicted
62
63
64 // ouch is how much damage is done
65 roll = rand() % 20 + 1; // range between 1 &20
66
67 if (roll <= 11)
68 {
69 ouch = str - (def /2);
70 }
71
72 else if ((roll <= 17) && (roll >= 12))
73 {
74 ouch = (str * 2) - (def / 2);
75 }
76
77 else if ((roll <= 20) && (roll >= 18))
78 {
79 ouch = (str * 3) - (def / 2);
80 //cout << "CRITICAL HIT!!";
81 }
82
83 opponent.health -= ouch;
84
85 return ouch;
86
87 }
您还需要更改 getAttack()
的声明(原型(prototype)):
20 class character
21 {
22 public:
.
.
.
32 float getAttack(character& opponent);
...以及它在 main()
中的调用方式:
152 case 2 :
153
154 attackDamage = user.getAttack(computer);
155
156 cout << "" <<user.name <<" did " << attackDamage << " damage to the opponent!\n";
157
158 break;
我还注意到程序根本没有循环。它只接受一个 Action ,执行它,然后终止。如果游戏循环直到其中一名玩家死亡,游戏可能会更有趣。
最后一件事,当使用随机数时,您通常在程序运行开始时恰好调用 srand
。每次创建 character
时都会调用它。
Here是我之前关于使用 rand
的回答之一的无耻插件。
我为您做了一些修改。这是一个link to ideone使用与下面相同的代码:
// Test for hard stuff.cpp : Defines the entry point for the console application.
//
// Bigger proj
// Constructors will make characters with rolling statistics
//#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
// declaring function for hit power
//int power( int str, int def);
int command;
class character
{
public:
character();
//~character();
string name;
float str;
float def;
float health; // hit points
float regen; // health regen amount
float roll; // for random value
float ouch; // amount of attack damage
float getAttack(character& opponent);
float getHeal(void);
void setRegen(float reg);
bool IsAlive() const;
//void setHeal(float healAmt);
private:
};
character::character()
{
str = rand() % 30 + 5;
def = rand() % 30 + 5;
health = 100;
//Output to check the constructor is running properly
cout<< "Character has been created.\n";
}
bool character::IsAlive() const
{
return health > 0.0f;
}
void character::setRegen( float reg )
{
regen = reg;
}
float character::getAttack(character& opponent)
{
//defines the magnitude/power of attack
//function shows how much damage is inflicted
// ouch is how much damage is done
roll = rand() % 20 + 1; // range between 1 &20
if (roll <= 11)
{
ouch = str - (def /2);
}
else if ((roll <= 17) && (roll >= 12))
{
ouch = (str * 2) - (def / 2);
}
else if ((roll <= 20) && (roll >= 18))
{
ouch = (str * 3) - (def / 2);
//cout << "CRITICAL HIT!!";
}
opponent.health -= ouch;
return ouch;
}
float character::getHeal()
{
//this is what happens when you chose to heal
regen = rand() % 20 + 3;
cout << "regen value= " << regen<< ".\n";
health += regen;
return regen;
}
/*character::~character()
{
str = 0;
def = 0;
health = 0;
// Output to check the destructor is running properly
cout << "Character has been destroyed\n";
} */
int main()
{
srand(time_t(NULL));
//Class objects
character user, computer;
//Hard code in a name for the computer's player
computer.name = "ZOID\n";
float attackDamage;
float healthAdded;
user.setRegen(42.0);
//Recieve data for the user's player
cout<< "Please enter a name for your character:\n";
cin>> user.name;
//Output name and stats to the user
cout<< "\nYour name is: " << user.name << endl;
cout << "here are your statistics: \n"
<< "strength: " << user.str << endl
<< "Defense: " << user.def << endl
<< "Health: " << user.health << endl;
cout<< "oh no an oppenent appeared!!!\n";
cout<< "you will have to fight him!" << endl<< endl;
cout << "opponent's health: 100" << endl;
while (user.IsAlive() && computer.IsAlive())
{
cout << "Str: " << user.str << "\t"
<< "Def: " << user.def << "\t"
<< "Health: " << user.health << "\t"
<< "\n";
cout << "what would you like to do: heal (1), attack(2), or run(3).\n";
cin>> command;
switch(command)
{
case 1 :
healthAdded = user.getHeal();
cout<< ""<<user.name <<" has regenerated " << healthAdded << " health.\n";
break;
case 2 :
attackDamage = user.getAttack(computer);
cout << "" <<user.name <<" did " << attackDamage << " damage to the opponent!\n";
break;
case 3:
cout<< ""<<user.name<<" got away!\n";
break;
default:
cout<< "Please enter a valid choice!";
} //end switch
}
return 0;
}
关于c++ - 基于文本的冒险游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17578857/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!