Advertisement
Korotkodul

bulls_and_cows

Sep 17th, 2024 (edited)
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. // A little guessing game called
  2. //    (for some obscure reason) <<Bulls and Cows>>.
  3.  
  4. #include <std_lib_facilities.h>
  5.  
  6. int count (const vector<char>& digits, char d)
  7. {
  8.   int k{};
  9.  
  10.   for (int i = 0; i < digits.size(); ++i)
  11.     if (digits[i] == d)
  12.       ++k;
  13.  
  14.   return k;
  15. }
  16.  
  17. bool validate (const vector<char>& number)
  18. {
  19.   for (int i = 0; i < number.size(); ++i)
  20.   {
  21.     if (number[i] < '0' || '9' < number[i]){
  22.       //error("the number contains not a digit");
  23.       cout << "the number contains not a digit" << "\n";
  24.       return 0;
  25.     }
  26.  
  27.     if (count(number, number[i]) != 1){
  28.       //error("digits of the number are not unique");
  29.       cout << "digits of the number are not unique" << "\n";
  30.       return 0;
  31.     }
  32.      
  33.   }
  34.   return 1;
  35. }
  36.  
  37. vector<char> user_guess ()
  38. {
  39.   vector<char> number(4);
  40.   bool ok = 0;
  41.   while (!ok) {
  42.     cout << "guess the number: ";
  43.     for (int i = 0; i < number.size(); ++i)
  44.     cin >> number[i];
  45.     ok = validate(number);
  46.   }
  47.  
  48.  
  49.   if (!cin)
  50.     error("invalid input");
  51.  
  52.  
  53.  
  54.   return number;
  55. }
  56.  
  57. #include <cstdlib>
  58. #include <ctime>
  59.  
  60. int main () {
  61.   try
  62.   {
  63.     cout << "<<Bulls and Cows>>\n"
  64.         << "Computer sets a number of 4 unique digits.\n"
  65.         << "Try to guess it.\n"
  66.         << "<Bull> means right digit in the right position.\n"
  67.         << "<Cow> means right digit in the wrong position.\n"
  68.         << "\n"
  69.         << "game is on" << endl;
  70.  
  71.     srand((unsigned)time(0));
  72.     vector<char> number{char(rand() % 10), char(rand() % 10), char(rand() % 10), char(rand() % 10)};
  73.  
  74.     int bulls{};
  75.  
  76.     do
  77.     {
  78.       bulls = 0;
  79.       int cows{};
  80.  
  81.       vector<char> uguess = user_guess();
  82.  
  83.       for (int i = 0; i < uguess.size(); ++i)
  84.       {
  85.         if (uguess[i] == number[i])
  86.           ++bulls;
  87.         else if (count(number, uguess[i]) == 1)
  88.           ++cows;
  89.       }
  90.  
  91.       cout << bulls << " bull(s) and " << cows << " cow(s)" << endl;
  92.     }
  93.     while (bulls != 4);
  94.  
  95.     cout << "game is over" << endl;
  96.   }
  97.   catch (exception& e)
  98.   {
  99.     cerr << e.what() << endl;
  100.     return 1;
  101.   }
  102.   catch (...)
  103.   {
  104.     cerr << "Oops, something went wrong..." << endl;
  105.     return 2;
  106.   }
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement