From 911633171be6bc39723ed7226c8591c44bd99784 Mon Sep 17 00:00:00 2001 From: AhmedSobhy01 Date: Fri, 17 May 2024 00:31:22 +0300 Subject: [PATCH] :recycle: Made printUnitsFighting function --- Game.cpp | 31 +++++++++++++++++-------------- Game.h | 1 + 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/Game.cpp b/Game.cpp index ea01332..51b4491 100644 --- a/Game.cpp +++ b/Game.cpp @@ -297,6 +297,21 @@ void Game::printUnitMaintenanceList() const std::cout << "]" << std::endl; } +void Game::printUnitsFighting() +{ + 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; + + // Nullify the pointers + currentAction = nullptr; + } +} + void Game::printAll() { std::cout << std::endl; @@ -311,20 +326,8 @@ void Game::printAll() std::cout << std::endl << "============== Earth Allied Army Alive Units ===================" << std::endl; earthAlliedArmy.printArmy(); - if (!attackers.isEmpty()) - { - std::cout << std::endl << "============== Units fighting at current step =================" << std::endl; - 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; + std::cout << std::endl << (attackers.isEmpty() ? "============== No units fighting at current step ==============" : "============== Units fighting at current step =================") << std::endl; + printUnitsFighting(); std::cout << std::endl << "============== Maintenance List Units =========================" << std::endl; printUnitMaintenanceList(); diff --git a/Game.h b/Game.h index 7228a83..1741a7e 100644 --- a/Game.h +++ b/Game.h @@ -38,6 +38,7 @@ class Game void printKilledList() const; // Prints the killed list with the console formats void printUnitMaintenanceList() const; // Print the units at the maintence list + void printUnitsFighting(); // Print the units fighting in current timestep void printAll(); // Prints all the armies and the killed list and units fighting at the current timestep std::string battleResult() const; // Returns the result of the battle