c++ - C ++中基于文本的战斗系统无法正常工作,不知道为什么
问题描述
所以我尝试制作一个基于文本的小战斗系统,但它有点坏了......我想知道是否有人可以帮助我找出原因?我尝试使用变量和简单的数学算法使其尽可能易于运行,但它只是在玩家 2 轮到后不断重置。它还播放“你无法治愈!” (玩家 2 的回合)消息,即使玩家没有治愈。我检查了几次括号,以及我的调试器,但是它说一切都很好。我不确定问题是什么,所以如果有人能指出它会很棒。提前致谢!
V以下的代码
using namespace std
int varset()
{
string ChoiceTeamA;
string ChoiceTeamB;
bool Atk1 = false;
bool Atk2 = false;
int health = health;
int health1 = health1;
}
int main() {
cout << "player 1 turn - choose what to do" << endl;
cout << "Player 1's health is " << health << " and Player 2's health is " << health1 << endl;
cout << "A. Attack" << endl;
cout << "B. Heal Magic" << endl;
getline(cin, ChoiceTeamA);
if (ChoiceTeamA == "A")
{
string ChoiceTeamA = "c";
health1--;
bool Atk1 = false;
cout << "Player 1 attacked! Player 2's health is now " << health1 << endl;
}
if (ChoiceTeamA == "B")
{
string ChoiceTeamA = "0";
if (health < 5)
{
health++;
cout << "Player 1 healed! Player 1's health is now " << health;
}
else
{
cout << "You can't heal!" << endl;
}
}
cout << "." << endl;
cout << "player 2 turn - choose what to do" << endl;
cout << "Player 1's health is " << health << " and Player 2's health is " << health1 << endl;
cout << "A. Attack" << endl;
cout << "B. Heal Magic" << endl;
getline(cin, ChoiceTeamB);
if (ChoiceTeamB == "A")
string ChoiceTeamB = "c";
{
string ChoiceTeamA = "c";
health--;
bool Atk2 = false;
cout << "Player 2 attacked! Player 1's health is now " << health << endl;
}
if (ChoiceTeamB == "B")
{
string ChoiceTeamA = "c";
if (health < 5)
{
health1++;
cout << "Player 2 healed! Player 2's health is now " << health1;
}
if (health = 5)
{
cout << "You can't heal!" << endl;
cout << "." << endl;
}
}
if (health = 0)
{
cout << "Player 2 wins!!!";
return 0;
}
if (health1 = 0)
{
cout << "Player 1 wins!!!";
return 0;
}
return main();
}
解决方案
I fixed your code, but remember a few things next time:
- Remember to use good formatting
main()
cannot be called in your code==
is for comparisons and=
is for assignments.
Note the use of reference parameters in the turn function.
Don't stop learning. :)
#include <iostream>
using namespace std;
void turn(int playerNum, string choice, int& healthATK, int& healthDEF) {
int othPlayerNum = 0;
if(playerNum == 1)
othPlayerNum = 2;
else
othPlayerNum = 1;
cout << "Player " << playerNum << " turn - choose what to do" << endl;
cout << "Player " << playerNum << "'s health is " << healthATK << " and Player " << othPlayerNum <<"'s health is " << healthDEF << endl;
cout << "A. Attack" << endl;
cout << "B. Heal Magic" << endl;
getline(cin, choice);
if (choice == "A") {
healthDEF--;
bool Atk1 = false;
cout << "Player " << playerNum << " attacked! Player " << othPlayerNum << "'s health is now " << healthDEF << endl;
}
if (choice == "B") {
if (healthATK < 5) {
healthATK++;
cout << "Player " << playerNum << " healed! Player " << playerNum << "'s health is now " << healthATK << endl;
}
else
cout << "You can't heal!" << endl;
}
}
void play() {
string ChoiceTeamA;
string ChoiceTeamB;
bool Atk1 = false;
bool Atk2 = false;
int health = 10;
int health1 = 10;
while(true) {
turn(1, ChoiceTeamA, health, health1);
turn(2, ChoiceTeamB, health1, health);
if (health <= 0) {
cout << "Player 2 wins!!!";
return;
}
if (health1 <= 0) {
cout << "Player 1 wins!!!";
return;
}
}
}
int main() {
play();
return 0;
}
推荐阅读
- computer-vision - 如何修剪 Detectron2 模型?
- r - 当我添加任何东西时,ggplot映射aes错误
- javascript - Nuxt 和 cloudinary - 将图像从客户端直接上传到 cloudinary
- javascript - 下载的文件与服务器上的文件不同
- r - 内连接 - 在 R 中分配向量
- c++ - Eigen 中立方根的性能改进
- informatica - 如何连接多个表并将某个列(在具有不同值的所有表中重复)放入具有一列的单个目标表中
- python - Pytorch:掩码膨胀/扩展
- php - 尝试将 Laravel 从版本 6 升级到 7 时出现问题
- asp.net-core - 激活服务工作者时重新发送请求