This assignment is about a space ship fighting game. It's turn-based between the player and the computer. I'm suppose to create this function that will transfer the ship upon death to the one who destroyed the ship.
For example, lets say the player manages to destroy one of the enemy's ship. The enemy's ship will then be transferred to the player with full health.
The ships are stored inside two vectors. One for the player, one for the computer. However, the codes I implemented did not delete the element in the enemy's vector (if player destroy ship). The ship did get transferred but the ship still exists in the enemy's vector with full health.
Here's the code that I've done:

if(enemy->isAlive(enemy->getVector(), enemyOption) == false)
		{
			player->addShip(enemy->getVector()[enemyOption]);
			enemy->enemyResetHP(enemy->getVector(), enemyOption, player->getVector());
		}

		if(player->isAlive(player->getVector(), playerOption) == false)
		{
			enemy->addShip(player->getVector()[playerOption]);
			player->playerResetHP(player->getVector(), playerOption, enemy->getVector());
		}

This is the isAlive function:

bool Fleet::isAlive(vector<StarShip *> list, int num)
{
	if(list[num]->getHP() <= 0)
	{
		return false;
	}
	return true;
}

And this is the PlayerReset function:

void Fleet::playerResetHP(vector<StarShip *> &playerList, int num, vector<StarShip *> &enemyList)
{
	if(Fleet::isAlive(playerList, num) == false)
	{
		if(typeid(*playerList[num]) == typeid(Craft))
		{
			enemyList[enemyList.size()-1]->resetHP(150);
			//playerList.erase((playerList.begin() + num), (playerList.end() - (playerList.size() - num)));
		}
		else if(typeid(*playerList[num]) == typeid(Frigate))
		{
			enemyList[enemyList.size()-1]->resetHP(300);
			//playerList.erase((playerList.begin() + num), (playerList.end() - (playerList.size() - num)));
		}
		else
		{
			enemyList[enemyList.size()-1]->resetHP(700);
			//playerList.erase((playerList.begin() + num), (playerList.end() - (playerList.size() - num)));
		}
		playerList.erase((playerList.begin() + num), (playerList.begin() + (num + 1 )));
	}
}

And this is the enemyReset function:

void Fleet::enemyResetHP(vector<StarShip *> &enemyList, int num, vector<StarShip *> &playerList)
{
	if(Fleet::isAlive(enemyList, num) == false)
	{
		if(typeid(*enemyList[num]) == typeid(Craft))
		{
			playerList[playerList.size()-1]->resetHP(150);
			//enemyList.erase((enemyList.begin() + num), (enemyList.end() - (enemyList.size() - num)));
		}
		else if(typeid(*enemyList[num]) == typeid(Frigate))
		{
			playerList[playerList.size()-1]->resetHP(300);
			//enemyList.erase((enemyList.begin() + num), (enemyList.end() - (enemyList.size() - num)));
		}
		else
		{
			playerList[playerList.size()-1]->resetHP(700);
			//enemyList.erase((enemyList.begin() + num), (enemyList.end() - (enemyList.size() - num)));
		}
		enemyList.erase((enemyList.begin() + num), (enemyList.begin() + (num + 1)));
	}
}

Can anyone help me? :(

Fbody commented: Excellent first post, hopefully you continue to post as well. +1

Kudos for an excellent first post :cool:

Are the "isAlive" functions static? I think you may be referencing them incorrectly when you call them in your conditionals. It seems like the compiler would have thrown an error though...

Also, is there a reason for all 6 *List.erase() lines being commented? If they are commented, they won't do anything...

Because I placed the erase function right at the end of the if-else loops so it should have gone through the erase function. If it didn't, at least it will go through the erase function in the if-else loop. But the problem is that the erase function did not erase the object that is suppose to be erased.
I didn't get a CTE (Compile Time Error) but I did get a RTE (Run Time Error) if I put the erase function in the first segment of codes instead of the erase function being in the reset functions that I placed in the first post.

Because I placed the erase function right at the end of the if-else loops so it should have gone through the erase function. If it didn't, at least it will go through the erase function in the if-else loop. But the problem is that the erase function did not erase the object that is suppose to be erased.
I didn't get a CTE (Compile Time Error) but I did get a RTE (Run Time Error) if I put the erase function in the first segment of codes instead of the erase function being in the reset functions that I placed in the first post.

OK, I see that now. Is there a reason the new ones don't match the old ones? It looks like you have the right idea, but you may not be referencing the right elements.

Have you tried using the arrow operator instead of the dot operator when you call erase? Since it seems to be coming in as a pointer, you would need to de-reference it in order to have access to the methods. But again, I think this would have been flagged at compile time.

Nope, it's a dot. Tried arrow but it only gives me my member functions. Yeah, after testing out a bit, I know which areas will give me RTE while the rest don't do anything.

Nope, it's a dot. Tried arrow but it only gives me my member functions. Yeah, after testing out a bit, I know which areas will give me RTE while the rest don't do anything.

Please make sure you read posts thoroughly. When there is more than one question, you tend to skip earlier questions and only answer the last one. I have another question, but I'm going to wait until you answer this one.

Is there a reason the new ones don't match the old ones? It looks like you have the right idea, but you may not be referencing the right elements.

Okay...so what would the referencing be like?

I think you have the right format, but let's figure that out. First, num seems to trace back to enemyOption and playerOption depending on when the *ResetHP() methods are called. Where do those values (enemyOption, playerOption) come from?

the enemyOption is from the RNG (Random Number Generator) in the statePlayerTurn and stateFleetTurn functions. The playerOption is the ship that the player has chosen.

the enemyOption is from the RNG (Random Number Generator) in the statePlayerTurn and stateFleetTurn functions. The playerOption is the ship that the player has chosen.

Based on what I'm seeing in the rest of your code I'm making this assumption. Is this correct?

These are the ID#s of the ships that each side has chosen from their own fleet for their own use in some sort of 1-on-1 battle phase.

Assuming this is correct, I would suggest you use the positional version of erase, rather than the range version. Follow this link for a description.

Tried it, it's still not working. It's late here so I'll come back to check the topic again some other time =x
Thanks for the help

You're still not reading posts carefully. You ignored the first part of my post AGAIN. When someone posts, READ ALL OF THE POST VERY CAREFULLY. They may have asked more than 1 question or provided more than 1 suggestion.

@MODS:
I know this is a double post, I apologize, but I thought it necessary...

@OP:
You have two identical functions, playerResetHP() and enemyResetHP(), which really isn't a very good design. It's harder to maintain the code because it requires more editing time and increases the potential for errors. I think you should combine these functions into one function to simplify the design. Perhaps something like:

void Fleet::resetHP(vector<StarShip *> &winnerList, int winnerNum, vector<StarShip *> &loserList, int loserNum)
{
	// remove the killed ship from the loser's inventory/fleet
	// using position notation
	loserList.erase(loserList.begin() + loserNum);

	// reset the winner's ship's HP
}

A double post is when the same message gets posted twice. What you did is post more information -- perfectly acceptable. There is no problem making 2 or more posts in a row as long as there is a reason for it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.