Skip to content

Commit

Permalink
🚀 Adjusted units interactions console printing (#72)
Browse files Browse the repository at this point in the history
* ✨ adjusted unit self printing

* 🚀 created a function to monitor attacks in game

* 🗑️ removed unnecessary data members and functions inside class Unit

* ⚡ adjusted units to print their fightings to the new system

* 📝 added comments

* 🔨 added a missing function call

* ✨ adjust ESs to print infected unique IDs

* 🎨 Auto formatted files

* 🔨 Made string parameter by const reference

* 🔥 Removed unused functions

---------

Co-authored-by: AhmedSobhy01 <[email protected]>
  • Loading branch information
habibayman and AhmedSobhy01 authored May 16, 2024
1 parent 701db5b commit 0dbb712
Show file tree
Hide file tree
Showing 22 changed files with 121 additions and 124 deletions.
15 changes: 1 addition & 14 deletions ArmyClasses/Army.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,4 @@
#include "../Game.h"

Army::Army(Game* gamePtr): gamePtr(gamePtr)
{}

void Army::printFightingUnits()
{
Unit* unit = nullptr;

while (currentAttackers.dequeue(unit))
unit->printFought();
}

int Army::getFightingUnitsCount() const
{
return currentAttackers.getCount();
}
{}
2 changes: 0 additions & 2 deletions ArmyClasses/Army.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ class Army
virtual bool isDead() const = 0; // Check if the army is dead

virtual void printArmy() const = 0; // Print the army units
void printFightingUnits(); // Print the fighting units of the army (current attackers)

// Getters
virtual int getUnitsCount(UnitType) const = 0; // Get the count of a specific unit type
int getFightingUnitsCount() const; // Get the count of fighting units
};

#endif
25 changes: 16 additions & 9 deletions Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,6 @@ bool Game::battleOver(bool didArmiesAttack) const
return currentTimestep >= 40 && (anArmyDied || noAttackTie);
}

bool Game::areUnitsFighting() const
{
return earthArmy.getFightingUnitsCount() + alienArmy.getFightingUnitsCount();
}

void Game::killSaverUnits()
{
Unit* saverToKill = earthAlliedArmy.removeUnit(UnitType::SU); // Remove a saver from its list
Expand Down Expand Up @@ -245,6 +240,13 @@ LinkedQueue<Unit*> Game::getEnemyList(ArmyType armyType, UnitType unitType, int
return enemyUnits;
}

void Game::registerAttack(Unit* currentAttacker, const std::string& currentAction, const std::string& currentFoughtUnits)
{
attackers.enqueue(currentAttacker); // Store the current attacker
attackActions.enqueue(currentAction); // Store the action it made
foughtUnits.enqueue(currentFoughtUnits); // Store the list of units the action happened on
}

void Game::addToKilledList(Unit* unit)
{
// Add the unit to the killed list
Expand Down Expand Up @@ -309,12 +311,17 @@ void Game::printAll()
std::cout << std::endl << "============== Earth Allied Army Alive Units ===================" << std::endl;
earthAlliedArmy.printArmy();

if (areUnitsFighting())
if (!attackers.isEmpty())
{
std::cout << std::endl << "============== Units fighting at current step =================" << std::endl;
earthArmy.printFightingUnits();
alienArmy.printFightingUnits();
earthAlliedArmy.printFightingUnits();
Unit* currentAttacker = nullptr;
std::string currentFoughtUnits, currentAction;

while (attackers.dequeue(currentAttacker) && attackActions.dequeue(currentAction) && foughtUnits.dequeue(currentFoughtUnits))
{
currentAttacker->printUnit();
std::cout << " " << currentAction << " [" << currentFoughtUnits << "]" << std::endl;
}
}
else
std::cout << std::endl << "============== No units fighting at current step ==============" << std::endl;
Expand Down
6 changes: 5 additions & 1 deletion Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ class Game
EarthAlliedArmy earthAlliedArmy;
RandomGenerator randomGenerator;

LinkedQueue<Unit*> attackers; // Attackers of the current time step
LinkedQueue<std::string> foughtUnits; // Units attacked by each attacker in the current timestep
LinkedQueue<std::string> attackActions; // Action done on the unit of the opposite army

LinkedQueue<Unit*> killedList;
PriorityQueue<HealableUnit*> unitMaintenanceList;

private:
bool startAttack(); // Makes the two armies attack each other
void setGameMode(GameMode); // Change the game mode
bool battleOver(bool) const; // Check if the battle is over
bool areUnitsFighting() const; // Check if there are units fighting
void killSaverUnits(); // Savers need to be killed once all infected units are healed

void printKilledList() const; // Prints the killed list with the console formats
Expand All @@ -57,6 +60,7 @@ class Game
void addUnit(Unit*); // Add a unit to the appropriate army and list
Unit* removeUnit(ArmyType, UnitType); // Remove a unit from the appropriate army and list
LinkedQueue<Unit*> getEnemyList(ArmyType, UnitType, int); // Get the enemy list for the given army type, unit type and attack capacity
void registerAttack(Unit*, const std::string&, const std::string&); // Store the current attacker along with its fought units

void addToKilledList(Unit*); // Add a unit to the killed list

Expand Down
20 changes: 10 additions & 10 deletions UnitClasses/AlienDrone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@ AlienDrone::AlienDrone(Game* gamePtr, double health, int power, int attackCapaci
: Unit(gamePtr, UnitType::AD, health, power, attackCapacity)
{}

void AlienDrone::printFought()
void AlienDrone::printUnit()
{
if (!foughtUnits.isEmpty())
{
std::cout << "AD " << getId() << " shots [";
foughtUnits.printList();
std::cout << "]" << std::endl;

clearFoughtUnits(); // Clear the list after printing
}
std::cout << "AD " << id;
}

bool AlienDrone::attack()
Expand All @@ -27,6 +20,8 @@ bool AlienDrone::attack()
LinkedQueue<Unit*> ETlist = gamePtr->getEnemyList(ArmyType::EARTH, UnitType::ET, ETnumber);
LinkedQueue<Unit*> EGlist = gamePtr->getEnemyList(ArmyType::EARTH, UnitType::EG, EGnumber);

std::string foughtUnits = "";

// Check for a successful attack
bool attackCheck = false;

Expand All @@ -47,7 +42,9 @@ bool AlienDrone::attack()
gamePtr->addUnit(enemyUnit);

// Store the IDs of the fought units to be printed later
foughtUnits.enqueue(enemyUnit->getId());
if (foughtUnits != "")
foughtUnits += ", ";
foughtUnits += std::to_string(enemyUnit->getId());

// Nullify the pointer
enemyUnit = nullptr;
Expand All @@ -56,5 +53,8 @@ bool AlienDrone::attack()
attackCheck = true;
}

if (foughtUnits != "")
gamePtr->registerAttack(this, "shots", foughtUnits);

return attackCheck;
}
2 changes: 1 addition & 1 deletion UnitClasses/AlienDrone.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class AlienDrone: public Unit
public:
AlienDrone(Game*, double, int, int);

void printFought(); // Print the units that the drone has fought
void printUnit(); // Print the unit's type along with its ID
bool attack(); // Attack the enemy units
};

Expand Down
28 changes: 18 additions & 10 deletions UnitClasses/AlienMonster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,9 @@ void AlienMonster::setInfectingProbability(int probability)
infectingProbability = probability;
}

void AlienMonster::printFought()
void AlienMonster::printUnit()
{
if (!foughtUnits.isEmpty())
{
std::cout << "AM " << getId() << " shots [";
foughtUnits.printList();
std::cout << "]" << std::endl;

clearFoughtUnits(); // Clear the list after printing
}
std::cout << "AM " << id;
}

bool AlienMonster::attack()
Expand All @@ -36,6 +29,8 @@ bool AlienMonster::attack()
LinkedQueue<Unit*> tanksList = gamePtr->getEnemyList(ArmyType::EARTH, UnitType::ET, tanksCapacity);
LinkedQueue<Unit*> saversList = gamePtr->getEnemyList(ArmyType::EARTH_ALLIED, UnitType::SU, saversCapacity);

std::string foughtUnits = "", infectedSoldiers = "";

// Check for a successful attack
bool attackCheck = false;

Expand All @@ -51,6 +46,12 @@ bool AlienMonster::attack()
{
dynamic_cast<EarthSoldier*>(enemyUnit)->getInfection(); // The soldier will get infected if not infected already and not immune
gamePtr->addUnit(enemyUnit); // The soldier will be re-enqueued to the list & infected soldiers counter will be incremented

// Store the IDs of the fought units to be printed later
if (infectedSoldiers != "")
infectedSoldiers += ", ";
infectedSoldiers += std::to_string(enemyUnit->getId());

continue;
}

Expand All @@ -66,7 +67,9 @@ bool AlienMonster::attack()
gamePtr->addUnit(enemyUnit);

// Store the IDs of the fought units to be printed later
foughtUnits.enqueue(enemyUnit->getId());
if (foughtUnits != "")
foughtUnits += ", ";
foughtUnits += std::to_string(enemyUnit->getId());

// Nullify the pointer
enemyUnit = nullptr;
Expand All @@ -75,5 +78,10 @@ bool AlienMonster::attack()
attackCheck = true;
}

if (foughtUnits != "")
gamePtr->registerAttack(this, "shots", foughtUnits);
if (infectedSoldiers != "")
gamePtr->registerAttack(this, "infects", infectedSoldiers);

return attackCheck;
}
2 changes: 1 addition & 1 deletion UnitClasses/AlienMonster.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AlienMonster: public Unit
// Static functions
static void setInfectingProbability(int); // Set the infecting probability

void printFought(); // Print the units that the monster has fought
void printUnit(); // Print the unit's type along with its ID
bool attack(); // Attack the enemy units
};

Expand Down
20 changes: 10 additions & 10 deletions UnitClasses/AlienSoldier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@ AlienSoldier::AlienSoldier(Game* gamePtr, double health, int power, int attackCa
: Unit(gamePtr, UnitType::AS, health, power, attackCapacity)
{}

void AlienSoldier::printFought()
void AlienSoldier::printUnit()
{
if (!foughtUnits.isEmpty())
{
std::cout << "AS " << getId() << " shots [";
foughtUnits.printList();
std::cout << "]" << std::endl;

clearFoughtUnits(); // Clear the list after printing
}
std::cout << "AS " << id;
}

bool AlienSoldier::attack()
Expand All @@ -27,6 +20,8 @@ bool AlienSoldier::attack()
LinkedQueue<Unit*> soldiersList = gamePtr->getEnemyList(ArmyType::EARTH, UnitType::ES, soldiersCapacity);
LinkedQueue<Unit*> saversList = gamePtr->getEnemyList(ArmyType::EARTH_ALLIED, UnitType::SU, saversCapacity);

std::string foughtUnits = "";

// Check for a successful attack
bool attackCheck = false;

Expand All @@ -47,7 +42,9 @@ bool AlienSoldier::attack()
gamePtr->addUnit(enemyUnit);

// Store the IDs of the fought units to be printed later
foughtUnits.enqueue(enemyUnit->getId());
if (foughtUnits != "")
foughtUnits += ", ";
foughtUnits += std::to_string(enemyUnit->getId());

// Nullify the pointer
enemyUnit = nullptr;
Expand All @@ -56,5 +53,8 @@ bool AlienSoldier::attack()
attackCheck = true;
}

if (foughtUnits != "")
gamePtr->registerAttack(this, "shots", foughtUnits);

return attackCheck;
}
2 changes: 1 addition & 1 deletion UnitClasses/AlienSoldier.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class AlienSoldier: public Unit
public:
AlienSoldier(Game*, double, int, int);

void printFought(); // Print the units that the soldier has fought
void printUnit(); // Print the unit's type along with its ID
bool attack(); // Attack the enemy units
};

Expand Down
21 changes: 11 additions & 10 deletions UnitClasses/EarthGunnery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@ EarthGunnery::EarthGunnery(Game* gamePtr, double health, int power, int attackCa
: Unit(gamePtr, UnitType::EG, health, power, attackCapacity)
{}

void EarthGunnery::printFought()
void EarthGunnery::printUnit()
{
if (!foughtUnits.isEmpty())
{
std::cout << "EG " << getId() << " shots [";
foughtUnits.printList();
std::cout << "]" << std::endl;

clearFoughtUnits(); // Clear the list after printing
}
std::cout << "EG " << id;
}

bool EarthGunnery::attack()
Expand All @@ -29,6 +22,8 @@ bool EarthGunnery::attack()
LinkedQueue<Unit*> dronesList = gamePtr->getEnemyList(ArmyType::ALIEN, UnitType::AD, dronesCapacity);
LinkedQueue<Unit*> monstersList = gamePtr->getEnemyList(ArmyType::ALIEN, UnitType::AM, monstersCapacity);

std::string foughtUnits = "";

// Check for a successful attack
bool attackCheck = false;

Expand All @@ -47,14 +42,20 @@ bool EarthGunnery::attack()
gamePtr->addUnit(enemyUnit);

// Store the IDs of the fought units to be printed later
foughtUnits.enqueue(enemyUnit->getId());
if (foughtUnits != "")
foughtUnits += ", ";
foughtUnits += std::to_string(enemyUnit->getId());

// Nullify the pointer
enemyUnit = nullptr;

// If this line is reached, at least one unit was attacked
attackCheck = true;
}

if (foughtUnits != "")
gamePtr->registerAttack(this, "shots", foughtUnits);

return attackCheck;
}

Expand Down
2 changes: 1 addition & 1 deletion UnitClasses/EarthGunnery.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class EarthGunnery: public Unit
public:
EarthGunnery(Game*, double, int, int);

void printFought(); // Print the units that the gunnery has fought
void printUnit(); // Print the unit's type along with its ID
bool attack(); // Attack the enemy units

// Getters
Expand Down
Loading

0 comments on commit 0dbb712

Please sign in to comment.