#include #include #include #include #include #include #include #include //for the algorthms sort and for_each #include using namespace std; const string nameBank[] { "pa63", "kab33", "fdb4", "mb341", "jc208", "scortes9", "mrd22", "lee2", "rfrancolini1", "eh141", "mh182", "ljl8", "jmanu4", "jmm56", "aco8", "is49", "els13", "jt88", "mjt6", "ju13", "lov", "kwesner1", "rz54", "mmeretzky" }; const size_t n {size(nameBank)}; struct stats { string name; int lvl; int hp; int xp; }; stats a[n]; unsigned seed = chrono::system_clock::now().time_since_epoch().count(); default_random_engine engine {seed}; uniform_int_distribution distribution {0, 999}; auto r {bind(distribution, engine)}; //Declarations for the functions that perfrom the tasks. //Each function has no arguments and no return value. void tourney(); void train(); void battle(); void list(); void defeated(size_t victor, size_t vanquished, size_t e); void xpCheck(size_t i); int main() { for (int i {0}; i < n; ++i){ a[i].name = nameBank[i]; a[i].lvl = 1; a[i].hp = r() % 50 + 100 + a[i].lvl * 25; a[i].xp = 0; }; list(); struct task { string name; void (*f)(); //a pointer to a function with no arguments //or return value }; //Write the name of each function in the second column. //The name of a function, all by itself, //means the address of the function. const task tasks[] { {"Start a tournament", tourney}, {"Train a player", train}, {"Random battle", battle}, {"Leaderboard", list} }; for (;;) { cout << "Would you like to\n"; //List the four tasks. int i {0}; for_each(begin(tasks), end(tasks), [&i](const task& t) { cout << ++i << ". " << t.name << "\n"; } ); int t {0}; cin >> t; if (!cin) { cerr << "Sorry, something went wrong.\n"; return EXIT_FAILURE; } if (t <= 0 || t > size(tasks)) { cerr << "Sorry, Please select a number in the range 1 " << "to " << size(tasks) << "inclusive and " << "press enter.\n"; cin.clear(); const streamsize n {numeric_limits::max()}; cin.ignore(n, '\n'); cout << "Try again: "; continue; } tasks[t-1].f(); //Call one of the four possible task functions. } return EXIT_SUCCESS; } void list() { //In order of decreasing lvl. sort(begin(a), end(a), [](const stats& a, const stats& b) {return a.lvl > b.lvl;}); for_each(begin(a), end(a), [](const stats& s) { cout << "Player: " << setw(12) << left << s.name << right << ". " << "Level: " << setw(6) << s.lvl << ". " << "HP: " << setw(6) << s.hp << ". " << "XP: " << setw(6) << s.xp << ".\n"; }); } void tourney() { cout << "Under Construction\n"; cout << "\n==== BEGIN TOURNAMENT ====\n"; cout << "There are " << n << " participants who will compete through rounds.\n"; for_each(begin(a), end(a), [](const stats& s) {}); } void train() { cout << "Which player would you like to train?\n"; string trainer; cin >> trainer; cout << "Pending\n"; for_each(begin(a), end(a), [](const stats& s) {}); } void battle() { //Select two different random numbers in the range 0 to n-1 inclusive. const size_t y {r() % n}; size_t z {r() % n}; while (y == z) { z = r() % n; } int tempHPy {a[y].hp}; int tempHPz {a[z].hp}; int rounds {1}; //Should this be 0? const size_t e {500 + (r() % 250)}; for (; tempHPy > 0 && tempHPz > 0; ++rounds) { tempHPz -= a[y].lvl * 15 + (r() % (a[y].lvl * 15)); tempHPy -= a[z].lvl * 15 + (r() % (a[z].lvl * 15)); } cout << "After " << rounds << " rounds of combat, "; if (tempHPy <= 0 && tempHPz <= 0){ cout << "it's a tie. " << e << " XP points will be divided evenly between " << a[y].name << " and " << a[z].name << ".\n"; a[y].xp += e/2; a[z].xp += e/2; } else if (tempHPy <= 0) { defeated(z, y, e); //z defeated y. } else { defeated(y, z, e); //y defeated z. } xpCheck(y); xpCheck(z); } void defeated(size_t victor, size_t vanquished, size_t e) { cout << a[victor].name << " has won!\n" << a[victor].name << " has defeated " << a[vanquished].name << " and gained " << e << " xp.\n"; a[victor].xp += e; } void xpCheck(size_t i) { if (a[i].xp > (a[i].lvl * 1.25 * 1000)) { a[i].lvl += 1; a[i].hp += (r() % 50 + 25); cout << a[i].name << " has leveled up!\n"; } }