Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <cstddef>
- #include <cstdlib>
- #include <ctime>
- #include <iostream>
- #include <istream>
- #include <ostream>
- #include <sstream>
- #include <string>
- #include <vector>
- const std::vector<int>::size_type SIZE(5);
- std::ostream & operator << (std::ostream &os, const std::vector<int> &vi)
- {
- for (std::vector<int>::const_iterator ci = vi.begin(); ci != vi.end(); ++ci)
- os << *ci << ' ';
- return os;
- }
- std::istream & operator >> (std::istream &is, std::vector<int> &vi)
- {
- while (true)
- {
- vi.clear();
- std::string ts; // Temporary String
- std::getline(std::cin, ts);
- std::stringstream tss(ts); // Temporary String Stream
- int ti; // Temporary Integer
- bool skipped(false);
- while (tss >> ti)
- if (ti < 1 || ti > 9)
- {
- std::cerr << "Only numbers between 1 and 9 are allowed!\n";
- skipped = true;
- break;
- }
- else
- vi.push_back(ti);
- if (vi.size() != SIZE)
- {
- if (!skipped)
- std::cerr << "You didn't give exactly " << SIZE << " numbers. It matters.\n";
- }
- else
- break;
- std::cerr << "Let's try this again.\n\n";
- }
- return is;
- }
- struct RandomOneToNine
- {
- int operator () () const
- {
- return std::rand() % 9 + 1;
- }
- };
- std::vector<int> create_secret()
- {
- RandomOneToNine rotn;
- std::vector<int> r(SIZE);
- std::generate(r.begin(), r.end(), rotn);
- return r;
- }
- std::string compare_similarity(const std::vector<int> &guessed, const std::vector<int> &actual)
- {
- std::string r;
- for (std::vector<int>::size_type i=0; i < guessed.size(); ++i)
- if (guessed.at(i) == actual.at(i))
- r += "A ";
- else
- if (std::find(actual.begin(), actual.end(), guessed.at(i)) != actual.end())
- r += "C ";
- else
- r += "- ";
- return r;
- }
- int main()
- {
- std::srand(std::time(NULL));
- const std::vector<int> actual(create_secret());
- std::vector<int> guessed;
- std::cout << "Please input " << SIZE << " numbers between 1 and 9, separated by space:\n";
- while (std::cin >> guessed)
- {
- std::cout << compare_similarity(guessed, actual) << '\n';
- if (guessed == actual)
- {
- std::cout << "Congratulations, you have won!" << std::endl;
- break;
- }
- else
- std::cout << "Try again!\n\n";
- std::cout.flush();
- }
- std::system("PAUSE");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement