Video Coming Soon...

Created by Zed A. Shaw Updated 2026-06-19 17:00:40

47: Advanced Containers

This exercise is pending. Quick notes about this exercise:

The Code

View Source file ex47.cpp Only

#include <fmt/core.h>
#include <string>
#include <random>
#include <memory>
#include <algorithm>
#include <unordered_map>
#include <vector>

using std::shared_ptr, std::make_shared, std::string;

std::random_device RNG;
std::mt19937 GENERATOR(RNG());

struct Combatant {
  string name;
  int damage{0};
  int hp{0};
  bool dead{false};
};

using ArenaID = int;
using Arena = std::unordered_map<ArenaID, shared_ptr<Combatant>>;
using CombatQueue = std::vector<size_t>;

int random_number(int from, int to) {
  std::uniform_int_distribution rand(from, to);
  return rand(GENERATOR);
}

void fight(shared_ptr<Combatant> attacker, shared_ptr<Combatant> defender) {
  int damage = random_number(0, attacker->damage);

  if(damage > 0) {
    defender->hp -= damage;
    fmt::println("{} hit {} for {}. {} now has {} hp",
        attacker->name, defender->name, damage,
        defender->name, defender->hp);
  } else {
    fmt::println("{} missed {}!",
        attacker->name, defender->name);
  }

  if(defender->hp <= 0) {
    fmt::println("{} died. {} wins! {} has {} HP",
        defender->name, attacker->name,
        attacker->name, attacker->hp);
  }
}

void fill_arena(Arena& arena, int combatants) {
  for(int arena_id = 0; arena_id < combatants; arena_id++) {
    string name = fmt::format("Giant Rat #{}", arena_id);
    int hp = random_number(5,20);
    int damage = random_number(2,10);
    arena.try_emplace(arena_id, make_shared<Combatant>(name, damage, hp, 1.4f));
  }
}

bool combat_active(Arena& arena, shared_ptr<Combatant> player) {
  return !arena.empty() && player->hp > 0;
}

CombatQueue queue_combat(Arena& arena) {
  CombatQueue queue;

  for(auto [arena_id, who]: arena) {
    queue.push_back(arena_id);
  }

  std::shuffle(queue.begin(), queue.end(), GENERATOR);

  return queue;
}

void run_combat(Arena& arena, shared_ptr<Combatant> player) {
  bool player_turn = false;

  // loop through the arena indexes, popping them off as we go
  for(auto enemy_id : queue_combat(arena)) {
    auto who = arena.at(enemy_id);

    if(player_turn) {
      fight(who, player);
    } else {
      fight(player, who);
    }

    // now how to remove when dead
    if(who->hp <= 0) {
      who->dead = true;
      fmt::println("-- 1 Down, {} To Go!", arena.size());
    }

    player_turn = !player_turn;
  }
}

void cull_the_dead(Arena& arena) {
  CombatQueue dead;

  // confirm this is bad or good
  for(auto [enemy_id, who] : arena) {
    if(who->dead) {
      dead.push_back(enemy_id);
    }
  }

  for(auto enemy_id : dead) {
    arena.erase(enemy_id);
  }
}

int main(int argc, char* argv[]) {
  Arena arena;
  auto player = make_shared<Combatant>("Player", 20, 50, 1.4f);

  fill_arena(arena, 10);

  // now with multiple combatants we have to keep going until the player dies
  while(combat_active(arena, player)) {
    run_combat(arena, player);
    cull_the_dead(arena);
  }

  if(arena.empty()) {
    fmt::println("The Player Wins! You defeated all of the rats.");
  } else {
    fmt::println("You Died! The Rats Win.");
  }
}

The Breakdown

line of code
Description.

The Discussion

Blah blah.

Further Study

Previous Lesson Next Lesson

Register for Learn C++ the Hard Way

Register to gain access to additional videos which demonstrate each exercise. Videos are priced to cover the cost of hosting.