Skip to content

Commit 3b1a8d0

Browse files
authored
♻️ Made printUnitsFighting function (#75)
* ♻️ Made printUnitsFighting function * 🔨 Fixed a typo * ✏️ Changed comment
1 parent d767b1c commit 3b1a8d0

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

Game.cpp

+17-14
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,21 @@ void Game::printUnitMaintenanceList() const
297297
std::cout << "]" << std::endl;
298298
}
299299

300+
void Game::printUnitsFighting()
301+
{
302+
Unit* currentAttacker = nullptr;
303+
std::string currentFoughtUnits, currentAction;
304+
305+
while (attackers.dequeue(currentAttacker) && attackActions.dequeue(currentAction) && foughtUnits.dequeue(currentFoughtUnits))
306+
{
307+
currentAttacker->printUnit();
308+
std::cout << " " << currentAction << " [" << currentFoughtUnits << "]" << std::endl;
309+
310+
// Nullify the pointer
311+
currentAttacker = nullptr;
312+
}
313+
}
314+
300315
void Game::printAll()
301316
{
302317
std::cout << std::endl;
@@ -311,20 +326,8 @@ void Game::printAll()
311326
std::cout << std::endl << "============== Earth Allied Army Alive Units ===================" << std::endl;
312327
earthAlliedArmy.printArmy();
313328

314-
if (!attackers.isEmpty())
315-
{
316-
std::cout << std::endl << "============== Units fighting at current step =================" << std::endl;
317-
Unit* currentAttacker = nullptr;
318-
std::string currentFoughtUnits, currentAction;
319-
320-
while (attackers.dequeue(currentAttacker) && attackActions.dequeue(currentAction) && foughtUnits.dequeue(currentFoughtUnits))
321-
{
322-
currentAttacker->printUnit();
323-
std::cout << " " << currentAction << " [" << currentFoughtUnits << "]" << std::endl;
324-
}
325-
}
326-
else
327-
std::cout << std::endl << "============== No units fighting at current step ==============" << std::endl;
329+
std::cout << std::endl << (attackers.isEmpty() ? "============== No units fighting at current step ==============" : "============== Units fighting at current step =================") << std::endl;
330+
printUnitsFighting();
328331

329332
std::cout << std::endl << "============== Maintenance List Units =========================" << std::endl;
330333
printUnitMaintenanceList();

Game.h

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Game
3838

3939
void printKilledList() const; // Prints the killed list with the console formats
4040
void printUnitMaintenanceList() const; // Print the units at the maintence list
41+
void printUnitsFighting(); // Print the units fighting in current timestep
4142
void printAll(); // Prints all the armies and the killed list and units fighting at the current timestep
4243

4344
std::string battleResult() const; // Returns the result of the battle

0 commit comments

Comments
 (0)