Advertisement
Guest User

Codebreaker game

a guest
Apr 17th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cstddef>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <iostream>
  6. #include <istream>
  7. #include <ostream>
  8. #include <sstream>
  9. #include <string>
  10. #include <vector>
  11.  
  12. const std::vector<int>::size_type SIZE(5);
  13.  
  14. std::ostream & operator << (std::ostream &os, const std::vector<int> &vi)
  15. {
  16.     for (std::vector<int>::const_iterator ci = vi.begin(); ci != vi.end(); ++ci)
  17.         os << *ci << ' ';
  18.  
  19.     return os;
  20. }
  21.  
  22. std::istream & operator >> (std::istream &is, std::vector<int> &vi)
  23. {
  24.     while (true)
  25.     {
  26.         vi.clear();
  27.  
  28.         std::string ts; // Temporary String
  29.  
  30.         std::getline(std::cin, ts);
  31.  
  32.         std::stringstream tss(ts); // Temporary String Stream
  33.         int ti; // Temporary Integer
  34.         bool skipped(false);
  35.  
  36.         while (tss >> ti)
  37.             if (ti < 1 || ti > 9)
  38.             {
  39.                 std::cerr << "Only numbers between 1 and 9 are allowed!\n";
  40.                 skipped = true;
  41.                 break;
  42.             }
  43.             else
  44.                 vi.push_back(ti);
  45.  
  46.         if (vi.size() != SIZE)
  47.         {
  48.             if (!skipped)
  49.                 std::cerr << "You didn't give exactly " << SIZE << " numbers. It matters.\n";
  50.         }
  51.         else
  52.             break;
  53.  
  54.         std::cerr << "Let's try this again.\n\n";
  55.     }
  56.  
  57.     return is;
  58. }
  59.  
  60. struct RandomOneToNine
  61. {
  62.     int operator () () const
  63.     {
  64.         return std::rand() % 9 + 1;
  65.     }
  66. };
  67.  
  68. std::vector<int> create_secret()
  69. {
  70.     RandomOneToNine rotn;
  71.     std::vector<int> r(SIZE);
  72.  
  73.     std::generate(r.begin(), r.end(), rotn);
  74.     return r;
  75. }
  76.  
  77. std::string compare_similarity(const std::vector<int> &guessed, const std::vector<int> &actual)
  78. {
  79.     std::string r;
  80.  
  81.     for (std::vector<int>::size_type i=0; i < guessed.size(); ++i)
  82.         if (guessed.at(i) == actual.at(i))
  83.             r += "A ";
  84.         else
  85.         if (std::find(actual.begin(), actual.end(), guessed.at(i)) != actual.end())
  86.             r += "C ";
  87.         else
  88.             r += "- ";
  89.  
  90.     return r;
  91. }
  92.  
  93. int main()
  94. {
  95.     std::srand(std::time(NULL));
  96.  
  97.     const std::vector<int> actual(create_secret());
  98.     std::vector<int> guessed;
  99.  
  100.     std::cout << "Please input " << SIZE << " numbers between 1 and 9, separated by space:\n";
  101.  
  102.     while (std::cin >> guessed)
  103.     {
  104.         std::cout << compare_similarity(guessed, actual) << '\n';
  105.  
  106.         if (guessed == actual)
  107.         {
  108.             std::cout << "Congratulations, you have won!" << std::endl;
  109.             break;
  110.         }
  111.         else
  112.             std::cout << "Try again!\n\n";
  113.  
  114.         std::cout.flush();
  115.     }
  116.  
  117.     std::system("PAUSE");
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement