
//
// This is example code from Chapter 5.11 "Drill" of
// "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
//
/***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 "..\std_lib_facilities.h"

//------------------------------------------------------------------------------
void createCode(vector<int>& v) ;
void getGuess(vector<char>& v) ;
void score(vector<int> secretCode, vector<char> guess, int& bulls, int& cows);


int main()
try {
    /// Create a secret code  like 1234 with all different digits;
    vector<int> secretCode;
    srand(time(0));
    createCode(secretCode);
    cout << "\nThe secret code is ";
    for(int i = 0; i < secretCode.size();++i)
        cout << secretCode[i];

    /// Game loop
    int bulls{0}, cows{0};
    vector<char> guess(4);
    while(bulls != 4) {

        /// Get a guess from the player
        getGuess(guess);

        cout << "\nYour guess is ";
        for(int i = 0; i < 4; ++i)
            cout << guess[i];

        /// score bulls and cows and report
        score(secretCode, guess, bulls, cows);
        cout << "\nThat's " << cows << " cows, and " << bulls << " bulls;";
        if(bulls != 4) cout << "\nGuess again.";
    } /// end game loop

    /// repeat until guess is correct

    return 0;
}

catch (exception& e) {
    cerr << "error: " << e.what() << '\n';
    keep_window_open();
    return 1;
}
catch (...) {
    cerr << "Oops: unknown exception!\n";
    keep_window_open();
    return 2;
}

//------------------------------------------------------------------------------

void createCode(vector<int>& v) {
    int next;
    bool allDifferent = false;
    v.push_back(rand()%10);
    cout << "\nThe first digit is " << v[0];
    while(v.size()<4) {
        next = rand()%10;
        allDifferent = true;
        for(int i = 0; i < v.size();++i) {
            if(next == v[i])
                allDifferent = false;
        }
        if(allDifferent) {
            v.push_back(next);
            cout << "\nThe next digit is " << v[v.size()-1];
        }
    }
}

void getGuess(vector<char>& v) {
    cout << "\nEnter a 4-digit guess: ";
        for(int i = 0; i < 4; ++i)
            cin >> v[i];
}


void score(vector<int> secretCode, vector<char> guess, int& bulls, int& cows) {
    ///score bulls
    bulls = 0;
    for(int i = 0; i < 4; ++i)
        if(secretCode[i]==int(guess[i])-48) ++bulls;
    ///score cows
}
