Reputation: 73
Here's where my confusion starts, so a null pointer is declared first. Then selection is executed running whatever input was given then the function randomEncounter comes.
cout << "1) Move, 2) Rest, 3) View Stats, 4) Quit: ";
cin >> selection;
Monster* monster = 0;
switch( selection )
{
case 1:
gameMap.movePlayer();
// Check for a random encounter. This function
// returns a null pointer if no monsters are
// encountered.
monster = gameMap.checkRandomEncounter();
// 'monster' not null, run combat simulation.
if( monster != 0 )
So next when the function goes through, is
//refering to the once null pointer and now modifies something real?
monster = new Monster
Is
0 = gameMap.checkRandomEncounter();
Or is it pointing to nothing instead of the actually value of zero, and waits patiently to have something to point to? So
0 = null;
Until the function is ran: Here's the function
Monster* Map::checkRandomEncounter()
{
int roll = Random(0, 20);
Monster* monster = 0;
if( roll <= 5 )
{
// No encounter, return a null pointer.
return 0;
}
else if(roll >= 6 && roll <= 10)
{
monster = new Monster("Orc", 10, 8, 200, 1,
"Short Sword", 2, 7);
cout << "You encountered an Orc!" << endl;
cout << "Prepare for battle!" << endl;
cout << endl;
}
So does when there is an encounter:
monster = new Monster();
Does this modify the once null pointer to a now real object, or does this make a completely new object? I'm so confused how all this links together. Also in the function there is a second
Monster* monster = 0;
Where is this one used, and how does it get back to the calling function that there was an encounter. I realize its somehow going back through the pointer but definitely need some clarity
Upvotes: 0
Views: 65
Reputation: 66194
The comments in the posted code are nothing if not self explanatory.
The caller side simply starts with a Monster
pointer that is initially NULL(0) (deliberately pointing to no object), and assigns it the return value from checkRandomEncounter()
if selection
is 1
. If that function return a non-null address, it means a Monster
has been returned and a specific course of action in response shall be undertaken.
// Check for a random encounter. This function
// returns a null pointer if no monsters are
// encountered.
monster = gameMap.checkRandomEncounter();
// 'monster' not null, run combat simulation.
if( monster != 0 ) ...
Regarding checkRandomEncounter()
if( roll <= 5 )
{
// No encounter, return a null pointer.
return 0;
}
So the function returns a null pointer if the roll is below 5, otherwise if it is between 6 and 10, then allocate a new Monster
and return a pointer to that.
else if(roll >= 6 && roll <= 10)
{
monster = new Monster("Orc", 10, 8, 200, 1,
"Short Sword", 2, 7);
cout << "You encountered an Orc!" << endl;
cout << "Prepare for battle!" << endl;
cout << endl;
}
And though not shown, there had better be some code to handle what happens when the roll is between 11 and 20, I'm guessing it is likely different monster types being generated, each with different random ranges. (just a shot in the dark there).
Regardless, if it were possible or the author to be more specific in what is going on, I'm pressed to see how. If it still isn't clear, you need to spend time learning basic pointer nuances in C++ prior to dissecting this further.
Finally, if the variable names are confusing you, does it help at all to know the monster
pointer variable in the main code has nothing to do with the one in checkRandomEncounter()
? Apart from the latter being used for calculating a return value that will eventually be assigned to the former due to the code structure, they are entirely unrelated, different pointers.
Upvotes: 1
Reputation: 156
you need to return the pointer for the other side of the if (or you haven't included the last two lines which i would assume to be
return monster;
}
from the gameMap.checkRandomEncounter method.
Also you shouldn't be printing stuff out in that method - just focus on checking if the encounter is triggered and then this affect the flow of the game.
Upvotes: 0