I am trying to build a dungeon crawler type game. I have figured out movement, and damage from attacks and traps. I have a variable 'health' that I initialize to 12. When you are attacked or step on a trap 1 point is decreased. This is working. I tried to return the health variable to main but it keeps being initialized to 12. How can I decrease health until it reaches 0?
class movement
{
public:
char wasd()
{
char mv;
char up = 'w';
char left = 'a';
char right = 'd';
char down = 's';
cout << "\nMove using w for (up), a for (left), d for (right), and s for (down).\n\n";
mv = _getch();
if (mv == up)
{
cout << "You have moved up 1 space.\n";
}
elseif (mv == left)
{
cout << "You have moved left 1 space.\n";
}
elseif (mv == right)
{
cout << "You have moved right 1 space.\n";
}
elseif (mv == down)
{
cout << "You have moved down 1 space.\n";
}
else
{
cout << "it didn't work.";
}
return 0;
}
};
class random_enc
{ public:
int treasure;
int health = 12;
public:
int encounter(int)
{
int randnumber = rand() % 5 + 1;
treasure = 0;
if (randnumber == 1)
{
cout << "You have been attacked!!! You lose 1 hitpoint." << endl;
health = --health;
cout << "Hitpoints remaining: " << health << endl;
return health;
}
elseif (randnumber == 2)
{
cout << "You found treasure!" << endl;
treasure = ++treasure;
cout << "Treasure collected: " << treasure << endl;;
return random_enc::treasure;
}
elseif (randnumber == 3)
{
return health;
}
elseif (randnumber == 4)
{
cout << "You step on a trap and take damage!! You lose 1 hit point.\n" << endl;
health = --health;
cout << "Hitpoints remaining: " << health << endl;
}
return health;
}
};
int main()
{
string name;
cout << "Welcome to the dungeon, enter your name:\n";
cin >> name;
cout << "\nGood luck in the dungeon " << name << "\n";
int health = 12;
while (health != 0)
{
movement mvmt;
random_enc random;
mvmt.wasd();
random.encounter(health);
}
cout << "You have been killed. Goodbye.\n";
return 0;
}
Second, the problem is that you're not using the return value correctly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int encounter() //Don't take an int. You're not using it for anything.
{
//...
return health;
}
//...
while (health != 0)
{
movement mvmt;
random_enc random;
mvmt.wasd();
health = random.encounter();
}
Thanks helios. I made those changes and that made my code cleaner. I made a change to the declaration of health in the class random_enc. I removed the initialization so it it just int health; This allows the return and decreases the number but the number starts at a large negative number:
1 2 3 4 5
Move using w for (up), a for (left), d for (right), and s for (down).
You have moved down 1 space.
You have been attacked!!! You lose 1 hitpoint.
Hitpoints remaining: -858993461
No one but every time I go through the class 'random' It gets reinitialized at 'main'. When I removed it the number would decrease but did not start at 12.
It looks like you want to remove the local variable "health" from encounter and instead pass in main's "health" by reference.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
usingnamespace std;
void encounter(int& health) // note the ampersand
{
--health; // this will change health in main
}
int main()
{
int health = 12;
encounter(health);
cout << health << '\n'; // prints 11
}
Thanks Dutch that did the trick. Can you explain why it worked? I suspect that the function call just passed the pointer so that the value was not carried over. Then the value was determined dynamically in the function. What does it matter where the & is placed? Is 'int& health' the same as 'int &health'?
void encounter(int health) // note the ampersand
{
--health;
}
In this code, the value health is copied into the function. The local copy of health (in the function) is decremented, but the original value (where the function was called) is not changed. Only the copy of the value is changed.
1 2 3 4
void encounter(int& health) // note the ampersand
{
--health; // this will change health in main
}
In this code, health is a reference variable (denoted by the &), which means that it refers to the actual variable that was passed into the function. Any changes to health are made to the variable where the function was called so that changes within the function are visible outside the function.
'int& health' and 'int &health' are the same. There are religious wars about which is better style / correct.