//A simple MasterMind Program #include #include #include using namespace std; //Generate a random number between 0 and N int random(int N) {return rand()%N;} //Generate an n-digit random number //This is a slightly more general version that the one that I used in the //class on Friday int generate_random_number(int n) { //Generate the first digit, which cannot be zero int random_number = 1 + random(9); //Loop over the remaining digits for(unsigned int i=1;i> ndigits; //Generate the random number int answer = generate_random_number(ndigits); //OK, now start looping do { //Read in the guess int guess; cout << "Enter guess " << endl; cin >> guess; //Find out how many digits are correct int ncorrect = compare_digits_of_two_numbers(guess,answer) ; //If there is an error report it if(ncorrect == -1) { cout << "Need a " << ndigits << " digit number" << endl; continue; } //Otherwise say how many digits are correct else { cout << ncorrect << " digits correct" << endl; } //If all correct then exit if(ncorrect == ndigits) {break;} } while(true); //Congratulate the player! cout << "Well done" << endl; }