Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

language: c++ I am not able to figure out the last part of the following directi

ID: 3882630 • Letter: L

Question

language: c++

I am not able to figure out the last part of the following directions, which is finding the average remaining health of each player when they won.

Directions: The program should run the results of the duel as many times as the user specified, and display for each participant the percentage of simulations where they won, and what their average remaining health was when they won.

please adjust my code because that is how i would learn.

code:

#include <iostream>

#include <iomanip>

#include <vector>

#include <string>

#include <map>

#include <cstdlib>

#include <ctime>

using namespace std;

struct Duelist {

string name;

int accuracy;

int health;

};

Duelist& find_threat(vector<Duelist> const& participants, string seeker)

{

for (auto check = participants.rbegin(); check < participants.rend(); ++check)

{

if (check->name == seeker || check->health <= 0) continue;

return const_cast<Duelist&>(*check);

}

throw std::runtime_error("No acceptable targets");

}

void play_round(vector<Duelist>& participants)

{

for (Duelist& active : participants)

{

if (active.health <= 0) continue;

Duelist& target = find_threat(participants, active.name);

auto rnd = rand();

if (rnd % active.accuracy == 0) target.health -= 1;

}

}

int living(vector<Duelist> participants)

{

int count = 0;

for (Duelist participant : participants)

{

if (participant.health > 0) ++count;

}

return count;

}

Duelist play_duel(vector<Duelist> participants)

{

while (living(participants) > 1)

{

play_round(participants);

}

for (auto participant : participants)

{

if (participant.health > 0) return participant;

}

return{ "", 0, 0 };

}

int main(int argc, char* argv[])

{

srand(static_cast<unsigned>(time(nullptr)));

int health1, health2, health3;

cout << "How much health do you want Melissa to have? ";

cin >> health1;

cout << "How much health do you want Nina to have? ";

cin >> health2;

cout << "How much health do you want Ophelia to have? ";

cin >> health3;

vector<Duelist> participants = {

{ "Melissa", 3, health1 },

{ "Nina", 2, health2 },

{ "Ophelia", 1, health3 }

};

int repetitions;

cout << "How many times do you want the duel to run? ";

cin >> repetitions;

map<string, int> results;

for (int i = 0; i < repetitions; i += 1)

{

string winner = play_duel(participants).name;

results[winner] += 1;

}

cout << "Results:" << endl;

for (auto result : results)

{

cout << left << setw(20) << result.first << right << setw(5) << result.second << endl;

}

double WinPercentage = results.find("Melissa")->second;

cout << "Melissa's winning percentage is " << WinPercentage / repetitions * 100 << "." << endl;

double WinPercentage1 = results.find("Nina")->second;

cout << "Nina's winning percentage is " << WinPercentage1 / repetitions * 100 << "." << endl;

double WinPercentage2 = results.find("Ophelia")->second;

cout << "Ophelia's winning percentage is " << WinPercentage2 / repetitions * 100 << "." << endl;

if (health1 == 1) {

cout << "Melissa's average health when winning is 1" << endl;

}

if (health2 == 1) {

cout << "Nina's average health when winning is 1" << endl;

}

if (health3 == 1) {

cout << "Ophelia's average health when winning is 1" << endl;

}

system("pause");

return 0;

}

Explanation / Answer

#include <iostream>

#include <iomanip>

#include <vector>

#include <string>

#include <map>

#include <cstdlib>

#include <ctime>

using namespace std;

struct Duelist {

string name;

int accuracy;

int health;

};

Duelist& find_threat(vector<Duelist> const& participants, string seeker)

{

for (auto check = participants.rbegin(); check < participants.rend(); ++check)

{

if (check->name == seeker || check->health <= 0) continue;

return const_cast<Duelist&>(*check);

}

throw std::runtime_error("No acceptable targets");

}

void play_round(vector<Duelist>& participants)

{

for (Duelist& active : participants)

{

if (active.health <= 0) continue;

Duelist& target = find_threat(participants, active.name);

auto rnd = rand();

if (rnd % active.accuracy == 0) target.health -= 1;

}

}

int living(vector<Duelist> participants)

{

int count = 0;

for (Duelist participant : participants)

{

if (participant.health > 0) ++count;

}

return count;

}

Duelist play_duel(vector<Duelist> participants)

{

while (living(participants) > 1)

{

play_round(participants);

}

for (auto participant : participants)

{

if (participant.health > 0) return participant;

}

return{ "", 0, 0 };

}

int main(int argc, char* argv[])

{

srand(static_cast<unsigned>(time(nullptr)));

int health1, health2, health3;

cout << "How much health do you want Melissa to have? ";

cin >> health1;

cout << "How much health do you want Nina to have? ";

cin >> health2;

cout << "How much health do you want Ophelia to have? ";

cin >> health3;

vector<Duelist> participants = {

{ "Melissa", 3, health1 },

{ "Nina", 2, health2 },

{ "Ophelia", 1, health3 }

};

int repetitions;

cout << "How many times do you want the duel to run? ";

cin >> repetitions;

map<string, int> results;

for (int i = 0; i < repetitions; i += 1)

{

string winner = play_duel(participants).name;

results[winner] += 1;

}

cout << "Results:" << endl;

for (auto result : results)

{

cout << left << setw(20) << result.first << right << setw(5) << result.second << endl;

}

double WinPercentage = results.find("Melissa")->second;

cout << "Melissa's winning percentage is " << WinPercentage / repetitions * 100 << "." << endl;

double WinPercentage1 = results.find("Nina")->second;

cout << "Nina's winning percentage is " << WinPercentage1 / repetitions * 100 << "." << endl;

double WinPercentage2 = results.find("Ophelia")->second;

cout << "Ophelia's winning percentage is " << WinPercentage2 / repetitions * 100 << "." << endl;

if (health1 == 1) {

cout << "Melissa's average health when winning is 1" << endl;

}

if (health2 == 1) {

cout << "Nina's average health when winning is 1" << endl;

}

if (health3 == 1) {

cout << "Ophelia's average health when winning is 1" << endl;

}

system("pause");

return 0;

}