Question: I am supposed to write a C++ program for the game Mastermind. It is supposed to be implemented in the playGame() function. There are several
I am supposed to write a C++ program for the game Mastermind. It is supposed to be implemented in the playGame() function.
There are several steps that are needed to implement: First ask the player to enter a difficulty (1-10) which indicates how long the pattern will be and the range of numbers to guess. For example, if the difficulty is 4, the length of the pattern will be 4 and the range of numbers will be 1-4, inclusively. Next create a random pattern that the user will guess. For each of the 10 turns, print the current turn and ask the user to guess the pattern. Calculate how many of the guesses are correct and in the correct position. Additionally, calculate how many of the guesses are correct, but not in the correct position. Print these statistics to the screen. If the pattern is guessed perfectly, indicate that the user has succeeded. If the pattern is not guessed within 10 turns indicate that the player has lost.
This is what I have but its not working...when i run it, even if I put Level of Difficulty at 2 and guess all variation of 1-2 it is not correct -- Can someone help me with what is wrong/what to change?
#include
using namespace std;
void playGame();
int main() { playGame(); return 0; }
///Implement this function void playGame() { //ask user to input difficulty level int difficulty; do{ cout<<"Enter difficulty (1-10): "; cin>>difficulty; }while(difficulty<1 || difficulty>10);
//Initialize main array for random number pattern int array[difficulty]; srand(time(0));
for(int i=0; i //Print user turn bool success=false; for(int k=1;k<=10;k++) { cout<<" Turn "< //Initialize array to store user guess int guess[difficulty]; //Initialize array for correct guess at given position int correct[difficulty]; for(int i=0;i //check if user has guessed correct number at given position if(guess[i]==array[i]) { correct[i] = 1; }else{ correct[i] = 0; } } //calculate 'Correct guess at correct position' and 'Correct guess not at correct position' int correctP=0; int notCorrectP=0; for(int i=0;i //if 'Correct guess at correct position' equals n, then correct pattern is guessed by user if(correctP==difficulty) { cout<<" Congratulations, You Win!!!"; success=true; break; }else{ cout<<" Correct guess in the correct position: "<< correctP; cout<<" Correct guess in the wrong position: "<< notCorrectP << endl; } } //10 chances are over. Did not guess the correct pattern if(!success) { cout<<"Game Over - Did not guess correct pattern"; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
