Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <string>
#include <map>
using namespace std;
bool Probability(float prob);
// Precondition: prob is between 0.0 and 1.0 inclusive.
// Returns: generates random probability between 0.0 and 1.0 and returns true if randomProb
// is <= prob, else false. Each invocation of this function has a (prob*100.0) percent chance
// of returning true.
string KeyOfLargest(map<string, float>& shooters, string activeShooter);
// Precondition: shooters.size() can 0 - arbitrarily large. activeShooter
// is the current shooter.
// Returns: if shooters.size() == 0, returns "", else returns
// key associated with the largest value in shooters, excluding activeShooter.
string NextShooter(map<string, float>& shooters, string previousShooter);
// Precondition: previousShooter is a key in shooters, else program terminates.
// previousShooter is also the previous duelist to shoot. "" if first shooter.
// Returns: if previousShooter is "" or last key in map, return key of value
// at front of map, else return key immediately following previousShooter.
int main()
{
srand(time(NULL));
map<string, float> shooters;
map<string, int> wins;
wins["Aaron"] = 0;
wins["Bob"] = 0;
wins["Charlie"] = 0;
string prevShooter = "";
const int ROUNDS = 10000;
for (int i = 0; i < 10000; ++i)
{
shooters.clear();
shooters["Aaron"] = (1 / 3.0f);
shooters["Bob"] = (1 / 2.0f);
shooters["Charlie"] = 1.0f;
while (shooters.size() > 1)
{
string activeShooter = NextShooter(shooters, prevShooter);
string target = KeyOfLargest(shooters, activeShooter);
bool targetHit = Probability(shooters.find(activeShooter)->second);
if (targetHit)
shooters.erase(target);
prevShooter = activeShooter;
}
wins.find(shooters.begin()->first)->second += 1;
}
cout << "Aaron: " << (wins.find("Aaron")->second / static_cast<float>(ROUNDS)) * 100 << "%" << endl;
cout << "Bob: " << (wins.find("Bob")->second / static_cast<float>(ROUNDS)) * 100 << "%" << endl;
cout << "Charlie: " << (wins.find("Charlie")->second / static_cast<float>(ROUNDS)) * 100 << "%" << endl;
return 0;
}
bool Probability(float prob)
{
float randomProb = (RAND_MAX - rand()) / static_cast<float>(RAND_MAX);
return (randomProb <= prob) ? true : false;
}
string KeyOfLargest(map<string, float>& shooters, string activeShooter)
{
string mostAccName = "";
float mostAccVal = 0.0;
for (map<string, float>::iterator it = shooters.begin(); it != shooters.end(); ++it)
{
if (it->second > mostAccVal && it != shooters.find(activeShooter))
{
mostAccName = it->first;
mostAccVal = it->second;
}
}
return mostAccName;
}
string NextShooter(map<string, float>& shooters, string previousShooter)
{
if (previousShooter == "" || shooters.find(previousShooter) == prev(shooters.end())) return shooters.begin()->first;
if (shooters.find(previousShooter) == shooters.end())
{
cout << "NextShooter: Value not in map. Program exiting.\n\n";
exit(1);
}
return next(shooters.find(previousShooter))->first;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.