Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Hydreigon_Lord
- //Pair Game
- /*
- 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 NUM_CONTESTANTS contestants (one contestant per line).
- Exit Code 32: Make sure the card file exists and is accessible.
- Exit Code 33: Make sure the card file contains at least NUM_CARD_TYPES cards (one card per line).
- Exit Code 48: 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>
- using namespace std;
- const int NUM_CONTESTANTS = 26;
- const int NUM_CARD_TYPES = 13;
- const string CONTESTANT_FILENAME = "contestants.txt";
- const string CARD_FILENAME = "cards.txt";
- class Contestant
- {
- private:
- string name, card1, card2;
- public:
- Contestant() {name = ""; card1 = ""; card2 = "";};
- void setName(string n) {name = n;};
- void setCard1(string c1) {card1 = c1;};
- void setCard2(string c2) {card2 = c2;};
- string getName() const {return name;};
- string getCard1() const {return card1;};
- string getCard2() const {return card2;};
- bool hasPair() const {return card1 == card2;};
- operator string() const {return name + ": " + card1 + ", " + card2 + (hasPair() ? " (pair)" : "");}
- };
- //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()
- {
- static_assert(NUM_CONTESTANTS > 0 && NUM_CARD_TYPES > 0 && NUM_CONTESTANTS % NUM_CARD_TYPES == 0, "Invalid game parameters; check values of NUM_CONTESTANTS and NUM_CARD_TYPES");
- int cardCopies = NUM_CONTESTANTS / NUM_CARD_TYPES * 2;
- //Read the contestant and card lists from the respective files
- //The files should contain one contestant or card per line
- fstream contin(CONTESTANT_FILENAME, ios::in), cardin(CARD_FILENAME, ios::in);
- if (!contin)
- {
- cout << "Error reading file " << CONTESTANT_FILENAME << endl;
- exit(1 << 4);
- }
- if (!cardin)
- {
- cout << "Error reading file " << CARD_FILENAME << endl;
- exit(2 << 4);
- }
- Contestant* conts = new Contestant[NUM_CONTESTANTS];
- string* cards = new string[NUM_CONTESTANTS * 2];
- for (int i = 0; i < NUM_CONTESTANTS; i++)
- {
- string name;
- if (contin.eof())
- {
- cout << "Unexpected EOF reading file " << CONTESTANT_FILENAME << endl;
- exit((1 << 4) + 1);
- }
- else
- {
- getline(contin, name);
- conts[i].setName(name);
- }
- }
- for (int i = 0; i < NUM_CARD_TYPES; i++)
- {
- string card;
- if (cardin.eof())
- {
- cout << "Unexpected EOF reading file " << CARD_FILENAME << endl;
- exit((2 << 4) + 1);
- }
- else
- {
- getline(cardin, card);
- for (int j = 0; j < cardCopies; j++)
- cards[i * cardCopies + j] = card;
- }
- }
- cout << "Successfully read files.\n";
- contin.close();
- cardin.close();
- //Ask the user for a file to output the game report to
- string outFileName;
- cout << "Enter the output filename: ";
- cin >> outFileName;
- fstream fout(outFileName, ios::out);
- fout << "*** Pair Game Report ***\n\n\n";
- if (!fout)
- {
- cout << "Error writing to file " << outFileName << endl;
- exit(3 << 4);
- }
- cout << "Successfully opened file for writing.\n";
- //Declare other game-related variables
- bool* finished = new bool[NUM_CONTESTANTS];
- for (int i = 0; i < NUM_CONTESTANTS; i++)
- finished[i] = false;
- int* finishRounds = new int[NUM_CONTESTANTS];
- int numFinished = 0;
- int round = 0;
- //Shuffle the cards
- srand(time(0));
- for (int i = 0; i < NUM_CONTESTANTS * 2; i++)
- {
- int j = rand() % (NUM_CONTESTANTS * 2);
- string temp = cards[i];
- cards[i] = cards[j];
- cards[j] = temp;
- }
- //Deal the cards
- for (int i = 0; i < NUM_CONTESTANTS; i++)
- {
- conts[i].setCard1(cards[2 * i]);
- conts[i].setCard2(cards[2 * i + 1]);
- }
- //Play the game
- do
- {
- round++;
- fout << "=== Round " << round << " ===\n\n";
- //Output each contestant's hand and detect pairs
- fout << "--- Contestant Hands ---\n";
- for (int i = 0; i < NUM_CONTESTANTS; i++)
- {
- if (!finished[i])
- {
- fout << conts[i] << endl;
- if (conts[i].hasPair())
- {
- finished[i] = true;
- finishRounds[i] = round;
- numFinished++;
- }
- }
- }
- fout << endl;
- if (numFinished == NUM_CONTESTANTS)
- {
- cout << "Game complete.\n";
- break;
- }
- //Have each contestant who has not finished pass a card
- fout << "--- Passing ---\n";
- int firstCont = -1, prevCont = -1;
- string prevCard = "", currCard = "";
- bool firstPassCard1 = true, passCard1 = true;
- for (int i = 0; i < NUM_CONTESTANTS; i++)
- {
- if (!finished[i])
- {
- passCard1 = rand() % 2;
- if (firstCont == -1)
- {
- firstCont = i;
- if (passCard1)
- prevCard = conts[i].getCard1();
- else
- prevCard = conts[i].getCard2();
- firstPassCard1 = passCard1;
- }
- else
- {
- if (passCard1)
- {
- currCard = conts[i].getCard1();
- conts[i].setCard1(prevCard);
- }
- else
- {
- currCard = conts[i].getCard2();
- conts[i].setCard2(prevCard);
- }
- fout << conts[prevCont].getName() << " passes " << prevCard << " to " << conts[i].getName() << endl;
- prevCard = currCard;
- }
- prevCont = i;
- }
- }
- if (firstPassCard1)
- conts[firstCont].setCard1(prevCard);
- else
- conts[firstCont].setCard2(prevCard);
- fout << conts[prevCont].getName() << " passes " << prevCard << " to " << conts[firstCont].getName() << "\n\n";
- cout << "Wrote round " << round << "; " << (NUM_CONTESTANTS - numFinished) << " contestants remain.\n";
- }
- while (numFinished < NUM_CONTESTANTS);
- //Report how many rounds it took each contestant to finish
- fout << "=== Final Results ===\n\n";
- for (int i = 0; i < NUM_CONTESTANTS; i++)
- fout << conts[i].getName() << " finished on round " << finishRounds[i] << endl;
- //Cleanup
- fout.close();
- delete[] conts;
- delete[] cards;
- delete[] finished;
- delete[] finishRounds;
- return 0;
- }
Add Comment
Please, Sign In to add comment