Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Hydreigon_Lord
- //Panel Panic
- /*
- IF YOU ARE GETTING AN ERROR:
- Exit Code 16: Make sure the contestant file exists and is accessible.
- Exit Code 17: Make sure the contestant file contains at least one contestant name.
- Exit Code 32 or 33: Make sure you are not entering the name of a read-only file when asked for an output filename.
- */
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <ctime>
- #include <cctype>
- using namespace std;
- const string FILENAME = "contestants.txt";
- class Contestant
- {
- private:
- static int num;
- string name;
- int panel, rounds, totalRounds, wins;
- bool fallen;
- public:
- Contestant() {num++; name = ""; panel = num; rounds = 0; totalRounds = 0; wins = 0; fallen = false;};
- void setName(string n) {name = n;}
- void fall() {fallen = true;}
- void round() {rounds++; totalRounds++;};
- void win() {wins++;};
- void reset() {fallen = false; rounds = 0;};
- string getName() const {return name;};
- int getPanel() const {return panel;};
- int getRounds() const {return rounds;};
- int getTotalRounds() const {return totalRounds;};
- int getWins() const {return wins;};
- bool hasFallen() const {return fallen;};
- operator string() const {return name + " (panel " + to_string(panel) + (fallen ? ", fallen" : "") + ")";};
- };
- int Contestant::num = 0;
- //Canonical implementation of overloaded stream insertion from cppreference.com
- std::ostream& operator<<(std::ostream& os, const Contestant& obj)
- {
- os << (string)obj;
- return os;
- }
- int main()
- {
- //Read the contestant list from the file
- //The file should contain one contestant per line (NO BLANK LINE AT THE END)
- fstream fin(FILENAME, ios::in);
- if (!fin)
- {
- cout << "Error reading file " << FILENAME << endl;
- exit(1 << 4);
- }
- string name, nameList = "";
- int numConts = 0;
- while (!fin.eof())
- {
- getline(fin, name);
- nameList += name + "\n"; //delimiter between contestant names
- numConts++;
- }
- fin.close();
- if (numConts == 0)
- {
- cout << "No contestants found in " << FILENAME << endl;
- exit((1 << 4) + 1);
- }
- cout << "Successfully read " << numConts << " contestants.\n";
- //Construct the contestant array
- Contestant* conts = new Contestant[numConts];
- int c = 0;
- name = "";
- for (int i = 0; i < nameList.length(); i++) //read the name list one character at a time
- {
- if (nameList[i] == '\n') //delimiter
- {
- conts[c].setName(name);
- name = "";
- c++;
- }
- else
- name += nameList[i];
- }
- //Get the output filenane from the user
- string outFileName;
- cout << "Enter the name of the output files (no extension): ";
- getline(cin, outFileName);
- string reportFile = outFileName + ".txt";
- string csvFile = outFileName + ".csv";
- fstream reportOut(reportFile, ios::out);
- reportOut << "*** Panel Panic Report ***\n\n\n";
- if (!reportOut)
- {
- cout << "Error writing to file " << reportFile << endl;
- exit(2 << 4);
- }
- fstream csvOut(csvFile, ios::out);
- csvOut << "Game #,";
- if (!csvOut)
- {
- cout << "Error writing to file " << csvFile << endl;
- exit((2 << 4) + 1);
- }
- cout << "Successfully opened files for writing.\n";
- for (int i = 0; i < numConts; i++)
- csvOut << conts[i].getName() << (i == numConts - 1 ? "\n" : ",");
- //Get the number of games to play
- string inStr;
- int numGames = 0;
- bool validIn;
- do
- {
- cout << "Enter the number of games to play: ";
- getline(cin, inStr);
- validIn = true;
- for (int i = 0; i < inStr.length(); i++)
- if (!isdigit(inStr[i]))
- validIn = false;
- if (validIn)
- {
- numGames = stoi(inStr);
- validIn = numGames > 0;
- }
- if (!validIn)
- cout << "Please enter a positive integer value.\n";
- }
- while (!validIn);
- cout << "Playing " << numGames << " game(s).\n";
- //Play the games
- srand(time(0));
- int* rolls = new int[numConts];
- for (int g = 1; g <= numGames; g++)
- {
- reportOut << "=== Game " << g << " ===\n\n";
- csvOut << g << ",";
- for (int i = 0; i < numConts; i++)
- conts[i].reset();
- int contsLeft = numConts;
- int round = 0;
- while (contsLeft > 1)
- {
- round++;
- if (round > 1) //clear the previous round's rolls
- for (int i = 0; i < numConts; i++)
- rolls[i] = -1;
- reportOut << "--- Round " << round << " ---\n";
- //Everyone who has not yet fallen rolls a die
- for (int i = 0; i < numConts; i++)
- if (!conts[i].hasFallen())
- rolls[i] = rand() % contsLeft; //this value is an index (not the actual roll)
- //Convert the roll indices to actual rolls
- for (int i = 0; i < numConts; i++)
- {
- if (!conts[i].hasFallen())
- {
- int nextIndex = 0;
- for (int j = 0; j < numConts; j++)
- {
- if (rolls[j] >= 0)
- {
- if (rolls[i] == nextIndex)
- {
- rolls[i] = conts[j].getPanel();
- break;
- }
- else
- nextIndex++;
- }
- }
- reportOut << conts[i] << " rolls a " << rolls[i] << ".\n";
- }
- }
- //Make contestants fall according to the rolls
- reportOut << "---\n";
- for (int i = 0; i < numConts; i++)
- {
- for (int j = 0; j < numConts; j++)
- {
- if (conts[i].getPanel() == rolls[j])
- {
- conts[i].fall();
- reportOut << conts[i].getName() << " has fallen.\n";
- contsLeft--;
- break;
- }
- }
- if (!conts[i].hasFallen())
- conts[i].round();
- }
- reportOut << endl;
- }
- //Print the game results
- reportOut << "--- Game Results ---\n";
- if (contsLeft == 0)
- reportOut << "The game has ended in a draw.\n";
- else
- {
- for (int i = 0; i < numConts; i++)
- {
- if (!conts[i].hasFallen())
- {
- conts[i].win();
- reportOut << conts[i].getName() << " has won!\n";
- }
- }
- }
- reportOut << endl;
- for (int i = 0; i < numConts; i++)
- csvOut << conts[i].getRounds() << (i == numConts - 1 ? "\n" : ",");
- cout << "Completed game " << g << " of " << numGames << ".\n";
- }
- //Print the final statistics
- reportOut << "=== Final Statistics ===\n";
- for (int i = 0; i < numConts; i++)
- reportOut << conts[i].getName() << " won " << conts[i].getWins() << " game(s) and survived " << conts[i].getTotalRounds() << " round(s).\n";
- csvOut << "Total,";
- for (int i = 0; i < numConts; i++)
- csvOut << conts[i].getTotalRounds() << (i == numConts - 1 ? "" : ",");
- //Cleanup
- reportOut.close();
- csvOut.close();
- cout << "You can find the game report in " << reportFile << " and the table of individual rounds survived in " << csvFile << ".\n";
- delete[] conts;
- delete[] rolls;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement