Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <set>
- #include <algorithm>
- #include <ctime>
- using namespace std;
- void fillGuesses(vector<string>& guesses);
- vector<string> filter(const vector<string>& guesses, int n, int m);
- void countBullsCows(const string& guess, const string& elm, int& tn, int& tm);
- int main(int argc, char const *argv[]) {
- srand(time(0));
- vector<string> guesses;
- fillGuesses(guesses);
- random_shuffle(guesses.begin(), guesses.end());
- while (true) {
- string comp_guess = guesses.front();
- cout << "My guess: " << comp_guess << endl;
- cout << "n = ";
- int n; cin >> n;
- cout << "m = ";
- int m; cin >> m;
- if (n == 4 and m == 4) {
- cout << "AI won!" << endl;
- break;
- }
- guesses = filter(guesses, n, m);
- random_shuffle(guesses.begin(), guesses.end());
- }
- return 0;
- }
- void fillGuesses(vector<string>& guesses) {
- for(int i = 1023; i <= 9876; i++) {
- string number = to_string(i);
- set<char> s(number.begin(), number.end());
- if (s.size() == 4) {
- // cout << "number = " << number << endl;
- guesses.push_back(number);
- }
- }
- }
- vector<string> filter(const vector<string>& guesses, int n, int m) {
- vector<string> new_guesses;
- string guess = guesses.front();
- for(const auto& elm : guesses) {
- int tn;
- int tm;
- countBullsCows(guess, elm, tn, tm);
- if (n == tn and m == tm) {
- new_guesses.push_back(elm);
- }
- }
- return new_guesses;
- }
- void countBullsCows(const string& guess, const string& elm, int& tn, int& tm) {
- tn = 0;
- tm = 0;
- for (int i = 0; i < guess.size(); i++) {
- for (int j = 0; j < elm.size(); j++) {
- if (guess[i] == elm[j]) {
- tn++;
- if (i == j) {
- tm++;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement