Advertisement
Hydreigon_Lord

Panel Panic

May 14th, 2020
1,520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.83 KB | None | 0 0
  1. //Hydreigon_Lord
  2. //Panel Panic
  3. /*
  4.  IF YOU ARE GETTING AN ERROR:
  5.  Exit Code 16: Make sure the contestant file exists and is accessible.
  6.  Exit Code 17: Make sure the contestant file contains at least one contestant name.
  7.  Exit Code 32 or 33: Make sure you are not entering the name of a read-only file when asked for an output filename.
  8.  */
  9.  
  10. #include <iostream>
  11. #include <fstream>
  12. #include <string>
  13. #include <ctime>
  14. #include <cctype>
  15. using namespace std;
  16.  
  17. const string FILENAME = "contestants.txt";
  18.  
  19. class Contestant
  20. {
  21. private:
  22.     static int num;
  23.     string name;
  24.     int panel, rounds, totalRounds, wins;
  25.     bool fallen;
  26. public:
  27.     Contestant() {num++; name = ""; panel = num; rounds = 0; totalRounds = 0; wins = 0; fallen = false;};
  28.     void setName(string n) {name = n;}
  29.     void fall() {fallen = true;}
  30.     void round() {rounds++; totalRounds++;};
  31.     void win() {wins++;};
  32.     void reset() {fallen = false; rounds = 0;};
  33.     string getName() const {return name;};
  34.     int getPanel() const {return panel;};
  35.     int getRounds() const {return rounds;};
  36.     int getTotalRounds() const {return totalRounds;};
  37.     int getWins() const {return wins;};
  38.     bool hasFallen() const {return fallen;};
  39.     operator string() const {return name + " (panel " + to_string(panel) + (fallen ? ", fallen" : "") + ")";};
  40. };
  41. int Contestant::num = 0;
  42.  
  43. //Canonical implementation of overloaded stream insertion from cppreference.com
  44. std::ostream& operator<<(std::ostream& os, const Contestant& obj)
  45. {
  46.     os << (string)obj;
  47.     return os;
  48. }
  49.  
  50. int main()
  51. {
  52.     //Read the contestant list from the file
  53.     //The file should contain one contestant per line (NO BLANK LINE AT THE END)
  54.     fstream fin(FILENAME, ios::in);
  55.     if (!fin)
  56.     {
  57.         cout << "Error reading file " << FILENAME << endl;
  58.         exit(1 << 4);
  59.     }
  60.     string name, nameList = "";
  61.     int numConts = 0;
  62.     while (!fin.eof())
  63.     {
  64.         getline(fin, name);
  65.         nameList += name + "\n"; //delimiter between contestant names
  66.         numConts++;
  67.     }
  68.     fin.close();
  69.     if (numConts == 0)
  70.     {
  71.         cout << "No contestants found in " << FILENAME << endl;
  72.         exit((1 << 4) + 1);
  73.     }
  74.     cout << "Successfully read " << numConts << " contestants.\n";
  75.    
  76.     //Construct the contestant array
  77.     Contestant* conts = new Contestant[numConts];
  78.     int c = 0;
  79.     name = "";
  80.     for (int i = 0; i < nameList.length(); i++) //read the name list one character at a time
  81.     {
  82.         if (nameList[i] == '\n') //delimiter
  83.         {
  84.             conts[c].setName(name);
  85.             name = "";
  86.             c++;
  87.         }
  88.         else
  89.             name += nameList[i];
  90.     }
  91.    
  92.     //Get the output filenane from the user
  93.     string outFileName;
  94.     cout << "Enter the name of the output files (no extension): ";
  95.     getline(cin, outFileName);
  96.     string reportFile = outFileName + ".txt";
  97.     string csvFile = outFileName + ".csv";
  98.     fstream reportOut(reportFile, ios::out);
  99.     reportOut << "*** Panel Panic Report ***\n\n\n";
  100.     if (!reportOut)
  101.     {
  102.         cout << "Error writing to file " << reportFile << endl;
  103.         exit(2 << 4);
  104.     }
  105.     fstream csvOut(csvFile, ios::out);
  106.     csvOut << "Game #,";
  107.     if (!csvOut)
  108.     {
  109.         cout << "Error writing to file " << csvFile << endl;
  110.         exit((2 << 4) + 1);
  111.     }
  112.     cout << "Successfully opened files for writing.\n";
  113.     for (int i = 0; i < numConts; i++)
  114.         csvOut << conts[i].getName() << (i == numConts - 1 ? "\n" : ",");
  115.    
  116.     //Get the number of games to play
  117.     string inStr;
  118.     int numGames = 0;
  119.     bool validIn;
  120.     do
  121.     {
  122.         cout << "Enter the number of games to play: ";
  123.         getline(cin, inStr);
  124.         validIn = true;
  125.         for (int i = 0; i < inStr.length(); i++)
  126.             if (!isdigit(inStr[i]))
  127.                 validIn = false;
  128.         if (validIn)
  129.         {
  130.             numGames = stoi(inStr);
  131.             validIn = numGames > 0;
  132.         }
  133.         if (!validIn)
  134.             cout << "Please enter a positive integer value.\n";
  135.     }
  136.     while (!validIn);
  137.     cout << "Playing " << numGames << " game(s).\n";
  138.    
  139.     //Play the games
  140.     srand(time(0));
  141.     int* rolls = new int[numConts];
  142.     for (int g = 1; g <= numGames; g++)
  143.     {
  144.         reportOut << "=== Game " << g << " ===\n\n";
  145.         csvOut << g << ",";
  146.         for (int i = 0; i < numConts; i++)
  147.             conts[i].reset();
  148.         int contsLeft = numConts;
  149.         int round = 0;
  150.         while (contsLeft > 1)
  151.         {
  152.             round++;
  153.             if (round > 1) //clear the previous round's rolls
  154.                 for (int i = 0; i < numConts; i++)
  155.                     rolls[i] = -1;
  156.             reportOut << "--- Round " << round << " ---\n";
  157.             //Everyone who has not yet fallen rolls a die
  158.             for (int i = 0; i < numConts; i++)
  159.                 if (!conts[i].hasFallen())
  160.                     rolls[i] = rand() % contsLeft; //this value is an index (not the actual roll)
  161.             //Convert the roll indices to actual rolls
  162.             for (int i = 0; i < numConts; i++)
  163.             {
  164.                 if (!conts[i].hasFallen())
  165.                 {
  166.                     int nextIndex = 0;
  167.                     for (int j = 0; j < numConts; j++)
  168.                     {
  169.                         if (rolls[j] >= 0)
  170.                         {
  171.                             if (rolls[i] == nextIndex)
  172.                             {
  173.                                 rolls[i] = conts[j].getPanel();
  174.                                 break;
  175.                             }
  176.                             else
  177.                                 nextIndex++;
  178.                         }
  179.                     }
  180.                     reportOut << conts[i] << " rolls a " << rolls[i] << ".\n";
  181.                 }
  182.             }
  183.             //Make contestants fall according to the rolls
  184.             reportOut << "---\n";
  185.             for (int i = 0; i < numConts; i++)
  186.             {
  187.                 for (int j = 0; j < numConts; j++)
  188.                 {
  189.                     if (conts[i].getPanel() == rolls[j])
  190.                     {
  191.                         conts[i].fall();
  192.                         reportOut << conts[i].getName() << " has fallen.\n";
  193.                         contsLeft--;
  194.                         break;
  195.                     }
  196.                 }
  197.                 if (!conts[i].hasFallen())
  198.                     conts[i].round();
  199.             }
  200.             reportOut << endl;
  201.         }
  202.         //Print the game results
  203.         reportOut << "--- Game Results ---\n";
  204.         if (contsLeft == 0)
  205.             reportOut << "The game has ended in a draw.\n";
  206.         else
  207.         {
  208.             for (int i = 0; i < numConts; i++)
  209.             {
  210.                 if (!conts[i].hasFallen())
  211.                 {
  212.                     conts[i].win();
  213.                     reportOut << conts[i].getName() << " has won!\n";
  214.                 }
  215.             }
  216.         }
  217.         reportOut << endl;
  218.         for (int i = 0; i < numConts; i++)
  219.             csvOut << conts[i].getRounds() << (i == numConts - 1 ? "\n" : ",");
  220.         cout << "Completed game " << g << " of " << numGames << ".\n";
  221.     }
  222.    
  223.     //Print the final statistics
  224.     reportOut << "=== Final Statistics ===\n";
  225.     for (int i = 0; i < numConts; i++)
  226.         reportOut << conts[i].getName() << " won " << conts[i].getWins() << " game(s) and survived " << conts[i].getTotalRounds() << " round(s).\n";
  227.     csvOut << "Total,";
  228.     for (int i = 0; i < numConts; i++)
  229.         csvOut << conts[i].getTotalRounds() << (i == numConts - 1 ? "" : ",");
  230.    
  231.     //Cleanup
  232.     reportOut.close();
  233.     csvOut.close();
  234.     cout << "You can find the game report in " << reportFile << " and the table of individual rounds survived in " << csvFile << ".\n";
  235.     delete[] conts;
  236.     delete[] rolls;
  237.     return 0;
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement