Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <set>
  5. #include <algorithm>
  6. #include <ctime>
  7.  
  8. using namespace std;
  9.  
  10. void fillGuesses(vector<string>& guesses);
  11. vector<string> filter(const vector<string>& guesses, int n, int m);
  12. void countBullsCows(const string& guess, const string& elm, int& tn, int& tm);
  13.  
  14.  
  15. int main(int argc, char const *argv[]) {
  16.  
  17. srand(time(0));
  18. vector<string> guesses;
  19. fillGuesses(guesses);
  20. random_shuffle(guesses.begin(), guesses.end());
  21.  
  22. while (true) {
  23.  
  24. string comp_guess = guesses.front();
  25. cout << "My guess: " << comp_guess << endl;
  26.  
  27. cout << "n = ";
  28. int n; cin >> n;
  29. cout << "m = ";
  30. int m; cin >> m;
  31.  
  32. if (n == 4 and m == 4) {
  33. cout << "AI won!" << endl;
  34. break;
  35. }
  36.  
  37. guesses = filter(guesses, n, m);
  38. random_shuffle(guesses.begin(), guesses.end());
  39. }
  40.  
  41.  
  42. return 0;
  43. }
  44.  
  45.  
  46. void fillGuesses(vector<string>& guesses) {
  47.  
  48. for(int i = 1023; i <= 9876; i++) {
  49.  
  50. string number = to_string(i);
  51. set<char> s(number.begin(), number.end());
  52.  
  53. if (s.size() == 4) {
  54. // cout << "number = " << number << endl;
  55. guesses.push_back(number);
  56. }
  57. }
  58. }
  59.  
  60. vector<string> filter(const vector<string>& guesses, int n, int m) {
  61.  
  62. vector<string> new_guesses;
  63.  
  64. string guess = guesses.front();
  65.  
  66. for(const auto& elm : guesses) {
  67.  
  68. int tn;
  69. int tm;
  70.  
  71. countBullsCows(guess, elm, tn, tm);
  72.  
  73. if (n == tn and m == tm) {
  74. new_guesses.push_back(elm);
  75. }
  76. }
  77.  
  78. return new_guesses;
  79. }
  80.  
  81. void countBullsCows(const string& guess, const string& elm, int& tn, int& tm) {
  82.  
  83. tn = 0;
  84. tm = 0;
  85.  
  86. for (int i = 0; i < guess.size(); i++) {
  87. for (int j = 0; j < elm.size(); j++) {
  88.  
  89. if (guess[i] == elm[j]) {
  90. tn++;
  91.  
  92. if (i == j) {
  93. tm++;
  94. }
  95. }
  96. }
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement