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? :(