Hello I need some help with this code. here is the link https://sites.google.com
ID: 3753375 • Letter: H
Question
Hello I need some help with this code. here is the link https://sites.google.com/site/uiccs141/programs/2-mastermind
i have got some of the program done and just need help with one of the outputs.
this is my program
#include <iostream>
#include <cstdlib>
using namespace std;
// int d1 = 0, d2 = 0, d3 = 0;
char d1, d2, d3;
void generateRandomDigits() {
int d[10] = {0};
int count = 0;
d1 = rand() % 10;
d[d1] = 1;
d2 = rand() % 10;
while(d[d2]) {
d2 = rand() % 10;
}
d[d2] = 1;
d3 = rand() % 10;
while(d[d3]) {
d3 = rand() % 10;
}
}
int main() {
cout << "Program: 2 MasterMind" << endl;
cout << "The program selects 3 distinct random digits 0..9." << endl;
cout << "On each turn you guess 3 digits. The Program indicates" << endl;
cout << "how many are correct. You have 10 moves to guess the number." <<endl;
cout << "Good luck!" << endl;
char choice;
cout << ("Press 's' to set the three digits, or 'r' to randomize them: ") << endl;
cin >> choice;
if(choice == 's') {
cout << "Enter three distinct digits each in the range 0..9 (e.g. 354): " << endl;
int num;
cin >> num;
d3 = num % 10;
num = num/10;
d2 = num % 10;
num = num/10;
d1 = num % 10;
} else {
generateRandomDigits();
}
cout << "Input of 000 displays the hidden digits. Input of 999 exits the program." << endl;
int attempts = 0;
int guess;
cout<< " In place Out of place ";
cout<< " -------- ------------ ";
while(attempts < 10) {
attempts++;
cout << attempts << ". Your guess: " << endl;
cin >> guess;
int x1 = guess/100;
int x2 = (guess % 100)/10;
int x3 = guess % 10;
int inPlace = 0, outOfPlace = 0;
if(x1 == d1) {
inPlace++;
} else if(x1 == d2 || x1 == d3) {
outOfPlace++;
}
if(x2 == d2) {
inPlace++;
} else if(x2 == d1 || x2 == d3) {
outOfPlace++;
}
if(x3 == d3) {
inPlace++;
} else if(x3 == d2 || x3 == d1) {
outOfPlace++;
}
// cout<< " In Place Out of Place ";
// cout<< " -------- ------------ ";
cout<< " You entered: "<< guess <<" "<<inPlace<<" "<<outOfPlace<<" ";
if(inPlace == 3) {
cout << "*** Congratulations! ***" << endl;
cout << "Exiting program..." << endl;
cout << endl;
return 0;
}
}
cout << "Better luck next time." << endl;
cout << "Exiting program..." << endl;
return 0;
}
this is my output on the left in the first image, and the expected output is on the right. I am having problem with the first guess. It is not printing out '012' and is just printing '12' ignoring the 0. can you help me with that?
I also need help with the 2nd part of the program "
Input of 000 displays the hidden digits. Input of 999 exits the program."
in the second image the expected output is on the right.
Can you please edit the program in that way? I would really appreciate it.
ome images POF Contact CLI Desktop afatin22 7 removals 13 additio rsfedSpi Word Cheraclor t. Press 's" to set the three digits, or 'r to randosize the: 1. Press 'sto set the three digits, or 'r'to rand enize thons out of 999 exits the pregran 3. Input of e20 displays the hidden digits. Input of 999 exits the its the prora place out of place 012 rou vou entened 354 Presss to set the Three digits, orr to ranoe thes: Ieeut of 0 dsplays the hidden dlgits. Input of 999 exits the progras nslace out of place In place out of place 1. Your quess you entered: 012Explanation / Answer
I have re - written the code and verified that output matched to given sample.
Please save beow code as mastermind2.cpp or any name as u desired.
/*
* Master mind 2 game for guessing
* the random number
*/
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
// method prototype that used for internal processing
int unplace_count(string, string);
string rand_number();
int place_count(string, string);
int main()
{
// declration of hidden number
string hidden_number = "";
string user_guessed_no;
char set_or_random_choice;
/*
* printing the given static
* information to player
*/
cout << "Program: 2 MasterMind " << endl;
cout << "The program selects 3 distinct random digits 0..9. ";
cout << "On each turn you user_guessed_no 3 digits. The program indicates ";
cout << "how many are correct. You have 10 moves to user_guessed_no the number. Good "
"luck! "
<< endl
<< endl;
/*
* Asking whether user want enter no or
* random number generation
*/
cout << "Press 's' to set the three digits, or 'r' to randomize them: ";
cin >> set_or_random_choice;
/*
*if user choose r then hidden number get generated by random number
*/
if (set_or_random_choice == 'r')
{
hidden_number = rand_number();
}
else
{
//user specific hidden value
cout << "Enter the hidden number : ";
cin.ignore(10,' ');
cin >> hidden_number;
}
cout << endl
<< "Input of 000 displays the hidden digits. Input of 999 exits the "
"program. "
<< endl
<< endl;
/*
* printing the header of aplication
*/
cout << " In place Out of place " << endl;
cout << " ---------- ------------" << endl;
/*
* using the for loop for asking
* the user to user_guessed number of max ten
* times
*/
for (int i = 1; i <= 10; i++)
{
cout << i << ". Your guess: ";
cin >> user_guessed_no;
//if user decided to choose to show the hidden number on
// pressing the 000
if (user_guessed_no == "000") {
cout << " hidden number is " << hidden_number << endl;
continue;
}
/*
* if user decide to give up
* then user_guessed number must
* be 999 to exit
*/
if (user_guessed_no == "999")
{
break;
}
cout << " you entered: " << user_guessed_no;
int inplace_count = place_count(hidden_number, user_guessed_no);
int outplace_count = unplace_count(hidden_number, user_guessed_no);
cout << " " << inplace_count << " " << outplace_count
<< endl;
//if exact count then printing the congratulation
if (inplace_count == 3)
{
cout << "*** Congratulations! ***" << endl;
break;
}
/*
* if user not able to guess the number till
* last chance then printing the message
*/
if (i == 10)
{
cout << endl
<< " Better luck next time." << endl;
}
}
cout << endl
<< "Exiting program..." << endl;
}
string rand_number()
{
string rand_number = "";
// using the while to gen rand number
// of any of digit repeat then while again try to gen
while (1)
{
int n1 = rand() % 9;
int n2 = rand() % 9;
int n3 = rand() % 9;
if (n1 == n2 || n1 == n3 || n2 == n1 || n2 == n3)
continue;
else
{
rand_number += std::to_string(n1);
rand_number += std::to_string(n2);
rand_number += std::to_string(n3);
return rand_number;
}
}
}
/*
* this method will the exact number of place matched
*/
int place_count(string hidden_number, string user_guessed)
{
if (hidden_number == user_guessed)
{
return 3;
}
int count = 0;
if (hidden_number[0] == user_guessed[0])
count++;
if (hidden_number[1] == user_guessed[1])
count++;
if (hidden_number[2] == user_guessed[2])
count++;
return count;
}
/*
* this method will the exact number of place matched
*/
int unplace_count(string hidden_number, string user_guessed)
{
int count = 0;
for (int i = 0; i < hidden_number.length(); i++)
{
for (int j = 0; j < user_guessed.length(); j++)
{
if (j == i)
continue;
if (hidden_number[i] == user_guessed[j])
{
count++;
break;
}
}
}
return count;
}//end
//OUTPUT
run 1:
Program: 2 MasterMind
The program selects 3 distinct random digits 0..9.
On each turn you user_guessed_no 3 digits. The program indicates
how many are correct. You have 10 moves to user_guessed_no the number.
Good luck!
Press 's' to set the three digits, or 'r' to randomize them: r
Input of 000 displays the hidden digits. Input of 999 exits the program.
In place Out of place
---------- ------------
1. Your guess: 210
you entered: 210 0 0
2. Your guess: 345
you entered: 345 0 1
3. Your guess: 512
you entered: 512 0 0
4. Your guess: 453
you entered: 453 0 1
5. Your guess: 000
hidden number is 674
6. Your guess: 678
you entered: 678 2 0
7. Your guess: 671
you entered: 671 2 0
8. Your guess: 674
you entered: 674 3 0
*** Congratulations! ***
Exiting program...
run 2:
Program: 2 MasterMind
The program selects 3 distinct random digits 0..9.
On each turn you user_guessed_no 3 digits. The program indicates
how many are correct. You have 10 moves to user_guessed_no the number.
Good luck!
Press 's' to set the three digits, or 'r' to randomize them: r
Input of 000 displays the hidden digits. Input of 999 exits the program.
In place Out of place
---------- ------------
1. Your guess: 234
you entered: 234 1 0
2. Your guess: 211
you entered: 211 0 0
3. Your guess: 678
you entered: 678 0 2
4. Your guess: 456
you entered: 456 1 0
5. Your guess: 345
you entered: 345 0 1
6. Your guess: 897
you entered: 897 0 1
7. Your guess: 313
you entered: 313 0 1
8. Your guess: 786
you entered: 786 2 0
9. Your guess: 678
you entered: 678 0 2
10. Your guess: 234
you entered: 234 1 0
Better luck next time.
Exiting program...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.