-
Function doesnt call
This is driving me mad. I have been trying several things to sort this out, but my "gameOver()" function is never read, dispite doing everything right that I can see.
The whole code is over 650 lines so Il only post the parts relevant.
I have also commented on the code what is going wrong.
Here is the player struct:
Code:
typedef struct
{
std::string m_PlayerName;
unsigned long m_PlayerScore;
unsigned short m_Lives; // This is assigned a value of 3 in a function
double m_Gold;
} Player;
This is the second room of the game, where the problem is, but I have grealtly reduced it to show the problem
Code:
// function with details of room two
int roomTwo ( Room *rm, Player *client )
{
std::cout << "Lives: " << client->m_Lives << std::endl;
rm->m_DoorIslocked = true;
std::cout << rm->mp_RoomDiscrip[ 1 ] << " ";
const char *pQuestions[ 5 ] =
{
"\nHow many legs on a spider?\n\n",
"\nHow many colours in the rainbow?\n\n",
"\nWhat is the opposite to black?\n\n",
"\n5 * 50 = ?\n\n",
"\nCrystal is a?\n\n"
};
bool answer = false, haveKey = false, isAlive = true;
int choice = rand() % 4 + 0;
while ( rm->m_DoorIslocked == true )
{
if ( rm->m_DoorIslocked == false )
{
break;
}
else if ( client->m_Lives <= 0 )
{
isAlive = false; // Player is defeated token
break; // get out of the loop
}
while ( answer == false )
{
std::cout << "Lives: " << client->m_Lives << "\n\n";
int theAnswer = 0;
std::string theReply = "";
std::cout << pQuestions[ choice ] << " ";
std::cout << "> ";
switch ( choice )
{
case 0: // for minimal code only showing one
std::cin >> theAnswer;
if ( theAnswer == 8 )
{
answer = true;
haveKey = true;
if ( haveKey == true )
{
rm->m_DoorIslocked = false;
}
}
else
{
if ( client->m_Lives <= 0 )
{
answer = true;
haveKey = true;
if ( haveKey == true )
{
rm->m_DoorIslocked = false;
}
}
client->m_Lives--; // lose a life
}
break;
default:
break;
}
}
}
if ( isAlive == false ) // This is the function that is not being called
{ // dispite the isAlive being equal to false
gameOver();
}
roomThree ( rm, client ); // instead, it skips to here and the new room
return 0;
}
I am sure it is somthing very small I am doing wrong, as if the answer is entered correctly, the program works fine and calls the third room no problems. It just after 3 incorrect answers, the game over function is not called dispite the loop breaking as it should do.
Any help greatly appriciated.
-
At a glance, you have quite alot of redundant code that could be removed to simplify your problem. For example, consider:
Code:
while ( rm->m_DoorIslocked == true )
{
if ( rm->m_DoorIslocked == false )
{
break;
}
else if ( client->m_Lives <= 0 )
{
isAlive = false; // Player is defeated token
break; // get out of the loop
}
You could discard the isAlive variable and just write:
Code:
while ( rm->m_DoorIslocked && client->m_Lives > 0 )
{
I suspect that this is the very problem as well. At some point, rm->m_DoorIslocked becomes true, but isAlive remains true. You need to check the status of client->m_Lives in the inner loop, or something like that.
-
Thanks for the advice laserlight, Il see what I can do, il change the code as you suggested too, I think the readabity could be better I agree.
-
Code:
if ( client->m_Lives <= 0 )
{
answer = true;
haveKey = true;
if ( haveKey == true )
{
rm->m_DoorIslocked = false;
}
}
m_DoorIslocked is set to false, causing the outer loop to terminate before isAlive can be set to false.