Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <iterator>
  5. #include <chrono>
  6. #include <set>
  7. #include <sstream>
  8. #include <cstdlib>
  9. #include <algorithm>
  10. #include <random>
  11.  
  12. const int defSize = 4;
  13. const int minSize = 3;
  14. const int maxSize = 6;
  15.  
  16. using namespace std;
  17.  
  18. int parseCmdLineArgs(int argc, char* argv[]);
  19. vector<string> fillNumbers(int n);
  20. void showHelp(int n);
  21. string getUserNumber(const vector<string>& numbers);
  22. void analyze(const string& compNumber, const string& userNumber, int& dig, int& pos);
  23.  
  24. int main(int argc, char* argv[]) {
  25. int n = parseCmdLineArgs(argc, argv);
  26. vector<string> numbers = fillNumbers(n);
  27. mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
  28. shuffle(begin(numbers), end(numbers), rnd);
  29. string compNumber = numbers.front();
  30.  
  31. showHelp(n);
  32.  
  33. int moves = 0;
  34. int dig = 0;
  35. int pos = 0;
  36.  
  37. while (dig != n || pos != n) {
  38. string userNumber = getUserNumber(numbers);
  39. analyze(compNumber, userNumber, dig, pos);
  40. cout << "digits: " << dig << endl;
  41. cout << "positions: " << pos << endl;
  42. ++moves;
  43. }
  44. cout << "You are winner!!!" << endl;
  45. cout << "Your result is " << moves << (moves == 1 ? "move." : " moves.") << endl;
  46. }
  47.  
  48. int parseCmdLineArgs(int argc, char* argv[]) {
  49. int res = defSize;
  50. if (argc == 2) {
  51. int t;
  52. istringstream sinp(argv[1]);
  53. if (sinp >> t && sinp.eof()) {
  54. t = abs(t);
  55. res = minSize <= t && t <= maxSize ? t : defSize;
  56. }
  57. }
  58. return res;
  59. }
  60.  
  61. vector<string> fillNumbers(int n) {
  62. vector<int> lowBounds = {102, 1023, 10234, 102345};
  63. vector<int> uppBounds = {987, 9876, 98765, 987654};
  64. vector<string> res;
  65. for (int i = lowBounds[n - minSize]; i <= uppBounds[n - minSize]; i++) {
  66. string t = to_string(i);
  67. set<char> s(t.begin(), t.end());
  68. if (s.size() == n) {
  69. res.push_back(t);
  70. }
  71. }
  72. return res;
  73. }
  74.  
  75. void showHelp(int n) {
  76. cout << "------------------------BULLS AND COWS-----------------------------------\n"
  77. << "Computer chose " << n << "-digit number without repeating digits. \n"
  78. << "Your goal is to guess it. After each try computer shows 2 numbers: \n"
  79. << "Digits : <how many digits you guessed> \n"
  80. << "Positions: <how many of them are in correct place> \n"
  81. << "Good luck! \n"
  82. << "-------------------------------------------------------------------------\n";
  83. }
  84.  
  85. string getUserNumber(const vector<string>& numbers) {
  86. for (;;) {
  87. cout << "your guess: ";
  88. string line;
  89. getline(cin, line);
  90. istringstream sinp(line);
  91. int t;
  92. char c;
  93. if (sinp >> t && !(sinp >> c)) {
  94. string res = to_string(t);
  95. if (find(begin(numbers), end(numbers), res) != end(numbers)) {
  96. return res;
  97. }
  98. }
  99. cout << "Incorrect number. Try again..." << endl;
  100. }
  101. }
  102.  
  103. void analyze(const string& compNumber, const string& userNumber, int& dig, int& pos) {
  104. dig = 0;
  105. pos = 0;
  106. for (int i = 0; i < int(userNumber.size()); i++) {
  107. for (int j = 0; j < int(compNumber.size()); j++) {
  108. if (userNumber[i] == compNumber[j]) {
  109. ++dig;
  110. pos += i== j;
  111. }
  112. }
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement