/* G. Hagopian
12. Implement a little guessing game called (for some obscure reason) “Bulls and Cows.” 
The program has a vector of four different integers in the range 0 to 9 (e.g., 1234 but 
not 1122) and it is the user’s task to discover those numbers by repeated guesses. Say 
the number to be guessed is 1234 and the user guesses 1359; the response should be 
“1 bull and 1 cow” because the user got one digit (1) right and in the right position 
(a bull) and one digit (3) right but in the wrong position (a cow). The guessing continues 
until the user gets four bulls, that is, has the four digits correct and in the correct order.
*/

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <map>
using namespace std;

const int rackSize = 4;


vector<int> createKey();
vector<int> getGuess();
pair<int, int> scoreGuess(vector<int>,vector<int>);


int main() {
	srand((unsigned)time(0));
	//create a random key in vector<int>
	vector<int> key = createKey();
	//show key
	//for (int i : key) cout << i << " ";
	//cin.get();
	//loop (while score is not 4 bulls):
	pair<int, int> score;  //(bulls,cows)
	do {
		//get guess
		vector<int> guess = getGuess();
		//score the guess
		score = scoreGuess(guess,key);
		//show score
		cout << score.first << " bulls and " << score.second << " cows.\n";
	} while (score.second != 4);
	cout << "Hooray!  You guessed it!";
}

bool in(int x, vector<int> v) {
	//return true if x is in v, else false
	for (int i : v) if (i == x) return true;
	return false;
}

vector<int> createKey() {
	//need to be sure you don't have repeated int in the key
	int rnd = rand() % 10;
	vector<int> key(1, rnd );
	do {
		rnd = rand() % 10;
		if (!in(rnd, key)) key.push_back(rnd);
	} while (key.size() < rackSize);
	return key;
}
vector<int> getGuess() {
	vector<int> guess(rackSize);
	cout << "Enter your guess (" << rackSize << " numerals): ";
	for (int i = 0; i < rackSize; ++i) {
		cin >> guess[i];
	}
	return guess;
}
pair<int, int> scoreGuess(vector<int> guess, vector<int> key) {
	pair<int, int> score;
	vector<bool> cows(10);
	for (int i = 0; i < rackSize; ++i) {
		//if (in(guess[i], key) && guess[i] != key[i] &&) {
			//++score.second;
		if (guess[i] == key[i]) {
			++score.first;
			cows[guess[i]] = true;
		}
		else if (in(guess[i], key) && !cows[guess[i]]) {
			++score.second;
			cows[guess[i]] = true;
		}
	}
	return score;
}