hello can you please help me with this in C++ I already asked this twice but it\
ID: 3752703 • Letter: H
Question
hello can you please help me with this in C++ I already asked this twice but it's wrong codes. would really appreciate it, please note that there are two stages, and needs to be done differently, thank you!
Prog 2: Mastermind Wite a program similar to Mastermind, except you will use 3 digits instead of colors. Running the program should look like the following Program: 2 MasterMind The program selects 3 distinct random digits 0. 9. On each turn you guess 3 digits. The program indicates how many are correct. You have 10 moves to guess 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: 012 2. Your guess: 345 3. Your guess: 637 4. Your guess: 783 5. Your guess: 793 You entered: 812e You entered: 345 You entered: 637 You entered: 783 2 You entered: 793 *CongratulationstExplanation / Answer
In this program, it is assumed that the user will first set the values before randomizing them (randomizing them means shuffling the positions, and not creating a new random number. This is because in the question the use of random function is not allowed.)
STAGE 1:
#include<iostream>
#include<cstdlib>
using namespace std;
int main() {
int a = 1, b = 2, c = 3; // 3 random digits, Initialised with random values.
//It is assumed that the user will set the values before randomizing them
char choice;
cout<< "Program: 2 Mastermind ";
cout<< "The program selects 3 distinct random digits 0..9. ";
cout<< "On each turn you guess 3 digits. the program indicates how amny are correct. You have 10 moves to guess the number Good luck! ";
cout<< " Press 's' to set the three digits, or 'r' to randomize them: ";
cin>>choice;
int temp;
if(choice == 'r') {
int a = rand()%3; //generates 0,1,2
switch(a) {
case 0:
temp = a;
a = b;
b = c;
c = temp;
break;
case 1:
temp = b;
b = c;
c = a;
a = temp;
break;
case 2:
temp = c;
c = b;
b = a;
a = temp;
break;
} // This switch randomly swaps the values of a, b, c. You could add more cases if you wish to.
}
else {
cout<< "Set the three digits:";
cout<< " Digit 1: ";
cin>>a;
cout<< " Digits 2: ";
cin>>b;
cout<< " Digit 3: ";
cin>>c;
}
cout<< "Values to be guess are: "<<a<<b<<c;
cout<< " Exiting...";
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
STAGE 2:
#include<iostream>
#include<cstdlib>
using namespace std;
int main() {
int a = 1, b = 2, c = 3; // 3 random digits, Initialised with random values.
//It is assumed that the user will set the values before randomizing them
char choice;
cout<< "Program: 2 Mastermind ";
cout<< "The program selects 3 distinct random digits 0..9. ";
cout<< "On each turn you guess 3 digits. the program indicates how amny are correct. You have 10 moves to guess the number Good luck! ";
cout<< " Press 's' to set the three digits, or 'r' to randomize them: ";
cin>>choice;
int temp;
if(choice == 'r') {
int a = rand()%3; //generates 0,1,2
switch(a) {
case 0:
temp = a;
a = b;
b = c;
c = temp;
break;
case 1:
temp = b;
b = c;
c = a;
a = temp;
break;
case 2:
temp = c;
c = b;
b = a;
a = temp;
break;
} // This switch randomly swaps the values of a, b, c. You could add more cases if you wish to.
}
else {
cout<< "Set the three digits:";
cout<< " Digit 1: ";
cin>>a;
cout<< " Digits 2: ";
cin>>b;
cout<< " Digit 3: ";
cin>>c;
}
cout<< " Input of 000 displays the hidden digits, Input of 999 exits the program. ";
cout<< " In Place Out of Place ";
cout<< " -------- ------------ ";
int n, flag=0;
int i =0;
for (i=1; i<=10; i++) {
cout<<i<<". Your guess: ";
cin>> n;
if(n == 0) {
cout<<a<<b<<c;
break;
}
else if(n == 999) {
flag = 1;
break;
}
else {
int z = n%10;
int y = (n%100)/10;
int x = n/100;
//extracted the three digits of input
int in=0, out=0;
if(x == a)
in++;
if(y == b)
in++;
if(z == c)
in++;
if(x == b || x == c)
out++;
if(y == a || y == c)
out++;
if(z == a || z== b)
out++;
cout<< " You entered: "<<n<<" "<<in<<" "<<out<<" ";
if(in == 3) {
cout<< "*** Congratulations! *** ";
break;
}
}
}
if(i==11)
cout<<"Better luck next time.";
cout<< " Exiting program...";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.