Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name: Nickelis Jarvis
- * Date: 3.7.15
- * Program Name: CS Stat Tracker
- * Description: Calculate total and average kills and deaths per match.
- */
- #include <iomanip>
- #include <iostream>
- #include <string>
- //Our max rounds per match, this could change with future updates.
- #define MAXROUNDS 30
- //Array indexes for k / d
- typedef enum SortType {ROUND, KILLS, DEATHS, KDR};
- //Function to display menu
- int showMenu() {
- int selection;
- do {
- std::cout
- << "1) Record Match Stats\n"
- << "2) Sort By Kills (Descending)\n"
- << "3) Sort By Deaths (Descending)\n"
- << "4) Sort By K/D (Descending)\n"
- << "5) Look Up Stats by Round\n"
- << "6) Display Match Stats\n"
- << "\n0) Exit Program\n" << std::endl
- << "What would you like to do? ";
- //This will return only if the selection is of
- //valid type and in range, otherwise continue looping.
- if (std::cin >> selection)
- if (selection >= 0 && selection < 7)
- return selection;
- std::cout << "\nError: Invalid selection, please enter a number between 0 and 6.\n" << std::endl;
- std::cin.clear();
- std::cin.ignore(10000, '\n');
- } while (true);
- }
- //Function to pull stat from standard i/o, will be obsolete in future versions.
- int getStat(SortType StatType, int& rnd_) {
- int rVal;
- std::string sVar = { "kills//deaths" };
- if (StatType == KILLS)
- sVar = sVar.substr(0, sVar.find("//"));
- else
- sVar = sVar.substr(sVar.find("//") + 2); //Increment index 2 chars to scan over "//"
- while (true) {
- std::cout << "Enter number of " << sVar << " for round " << rnd_ + 1 << ": ";
- if (std::cin >> rVal) {
- return rVal;
- }
- else {
- std::cout << "\nError: Please enter a number.\n" << std::endl;
- std::cin.clear();
- std::cin.ignore(10000, '\n');
- }
- }
- }
- //Function to calculate an average value
- double calcAvg(double* arrName, size_t& arrSize) {
- double rVal = 0.0;
- for (size_t i = 0; i < arrSize; i++) {
- rVal += *(arrName + i) / arrSize;
- }
- return rVal;
- }
- //Function to calculate ratio of kills to deaths
- double getKDR(int& kills, int& deaths) {
- return ((double)kills / (double)deaths);
- }
- //Sort arrays by kills, deaths, or kdr, into a pointer array.
- void ptrSort(int Src[][3], int* Dest[], const SortType& SortBy) {
- bool swap;
- for (size_t i = 0; i < MAXROUNDS; i++)
- Dest[i] = Src[i];
- if (SortBy == KDR){
- do {
- swap = false;
- for (size_t i = 0; i < (MAXROUNDS - 1); i++) {
- if ((getKDR(Dest[i + 1][KILLS], Dest[i + 1][DEATHS])) > (getKDR(Dest[i][KILLS], Dest[i][DEATHS]))) {
- int* temp = Dest[i];
- Dest[i] = Dest[i + 1];
- Dest[i + 1] = temp;
- swap = true;
- }
- }
- } while (swap);
- }
- else {
- do {
- swap = false;
- for (size_t i = 0; i < (MAXROUNDS - 1); i++) {
- if (Dest[i + 1][SortBy] > Dest[i][SortBy]) {
- int* temp = Dest[i];
- Dest[i] = Dest[i + 1];
- Dest[i + 1] = temp;
- swap = true;
- }
- }
- } while (swap);
- }
- return;
- }
- //Main function
- int main() {
- int kd_arr[MAXROUNDS][3] = { 0 }; //Our actual array, initialized to 0 (empty)
- int* p_kd_arr[MAXROUNDS]; //Our pointer array (for sorting)
- //This time i skipped the sentinel and just set 0 to return from main.
- while (true) {
- int choice = showMenu();
- switch (choice) {
- case 0:
- system("pause");
- return 0;
- case 1: //Record Match Stats
- for (int i = 0; i < MAXROUNDS; i++) {
- kd_arr[i][ROUND] = i + 1; //Assign round number
- kd_arr[i][KILLS] = getStat(KILLS, i); //Assign kills for round
- kd_arr[i][DEATHS] = getStat(DEATHS, i); //Assign deaths for round
- }
- break;
- case 2: //Sort by kills (Descending)
- //Fall-Through to avoid duplicate code. Only difference is SortBy passed to ptrSort
- case 3: //Sort by deaths (Descending)
- //Fall-Through to avoid duplicate code.
- case 4: //Sort by K/D ratio (Descending)
- if (**kd_arr) {
- //This handles which menu choice they made and uses the appropriate sort call.
- if (choice == 2) {
- ptrSort(kd_arr, p_kd_arr, KILLS);
- std::cout << "5 rounds with the most kills: \n" << std::endl;
- }
- else if (choice == 3) {
- ptrSort(kd_arr, p_kd_arr, DEATHS);
- std::cout << "5 rounds with the most deaths: \n" << std::endl;
- }
- else {
- ptrSort(kd_arr, p_kd_arr, KDR);
- std::cout << "5 rounds with highest Kill / Death: \n" << std::endl;
- }
- //Print top 5 for whatever sort we used.
- for (int i = 0; i < 5; i++) {
- std::cout << std::fixed << std::setprecision(3)
- << "ROUND " << p_kd_arr[i][ROUND] << ": \n"
- << std::setw(5) << p_kd_arr[i][KILLS] << " Kills"
- << std::setw(5) << p_kd_arr[i][DEATHS] << " Deaths"
- << std::setw(8) << "K/D: " << getKDR(p_kd_arr[i][KILLS], p_kd_arr[i][DEATHS]) << '\n' << std::endl;
- }
- }
- else
- std::cout << "\nError: No data to sort\n" << std::endl;
- break;
- case 5: //Look up stats by round
- int round;
- if (**kd_arr) {
- do {
- std::cout << "Which round would you like to look up?" << std::endl;
- //Check for valid data.
- if (std::cin >> round) {
- //If round is over constant MAXROUNDS, throw an error. Otherwise continue.
- if (round <= MAXROUNDS) {
- //Loop through to find our round we searched for.
- for (int i = 0; i < MAXROUNDS; i++) {
- //FOUND IT! Go ahead and print the results (no need to use sorting here)
- if (round == kd_arr[i][ROUND]) {
- std::cout << std::fixed << std::setprecision(3)
- << "\nROUND " << kd_arr[i][ROUND] << ": \n"
- << std::setw(5) << kd_arr[i][KILLS] << " Kills"
- << std::setw(5) << kd_arr[i][DEATHS] << " Deaths"
- << std::setw(8) << "K/D: " << getKDR(kd_arr[i][KILLS], kd_arr[i][DEATHS]) << '\n' << std::endl;
- break;
- }
- //Aww, nothing to be found :(
- else if (i + 1 >= MAXROUNDS)
- std::cout << "Data for round " << round << " could not be found.\n" << std::endl;
- }
- //Break the for loop and return to the menu.
- break;
- }
- //Display an error for too high a value.
- else
- std::cout << "Sorry, there are only " << MAXROUNDS << " rounds in a match, please try again.\n" << std::endl;
- }
- //Display an error for invalid data type.
- else
- std::cout << "Error: Invalid data type, please enter a number.\n" << std::endl;
- std::cin.clear();
- std::cin.ignore(10000, '\n');
- } while (true);
- }
- //This will display if our first element has no data (empty array)
- else
- std::cout << "Error: No data to look up.\n" << std::endl;
- break;
- case 6: //Display match stats
- if (**kd_arr) {
- for (int i = 0; i < MAXROUNDS; i++) {
- std::cout << std::fixed << std::setprecision(3)
- << "\nROUND " << kd_arr[i][ROUND] << ": \n"
- << std::setw(5) << kd_arr[i][KILLS] << " Kills"
- << std::setw(5) << kd_arr[i][DEATHS] << " Deaths"
- << std::setw(8) << "K/D: " << getKDR(kd_arr[i][KILLS], kd_arr[i][DEATHS]) << '\n' << std::endl;
- }
- }
- else
- std::cout << "Error: No data to display.\n" << std::endl;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement