Please help “Master Mind” Project

I am new to c++ and had a professor switch. The new professor assigned this project and we cannot figure it out.


For this project, you will write a C++ program that imitates a popular peg-game called Master Mind. The computer will secretly and randomly select four “pegs” from a pile of six different colors that we abbreviate as follow:
B blue G green O Orange
P purple R red Y yellow
Colors can be repeated so that BYRO, BBGG, and YPPP are valid choices of four peg colors.
The goal of the player is to guess the colors and positions of the four pegs selected by the computer in as few moves as possible and not more than 15 moves. The computer will ask the player (the person running the program) to type four colors (peg colors) that are a guess as to which pegs have been selected by the computer. The computer will then give clues as to which pegs are correct according to the following rules:
1. If an input peg has a correct color but it is in the wrong place, then then the computer will print out an asterisk “*” for each such peg.
2. If an input peg has the correct color and it is in the right place, then the computer will print out a dollar sign “$” for each such peg.
3. No clue is given if a peg of the wrong color is chosen.
To prevent the clues from being too obvious, you should first print out all the possible asterisks and then all the possible dollar signs. In this way, there will not be any one-to-one correspondence of clues with pegs.
The output will consist of successive single lines with each line containing the following information:
1. An integer with width two spaces will be printed out first, indicating how many times someone has tried to guess the correct pegs.
2. A string of four characters echoing what was recently typed by the player.
3. A string of clues consisting of up to four asterisks and/or dollar signs.
When a player has four dollar signs, he or she has won the game. At this point, the program should congratulate the player and ask if further play is desired.
If the computer has chosen the pegs “BYRO”, then here is what the first few output lines of the game might looks like:
YGRP
1 YGRP *$
GYRP
2 GYRP $$
OYRB
3 OYRB **$$
BYRO
4 BYRO $$$$
Congratulations, you have won the game!
Would you like to play the game again (y/n)? n
Thanks for playing Master Mind!
Note how all the dollar signs come after all the asterisks. The unnumbered lines are typed by the player and the numbered lines are printed by the computer.
If the player does not come up with the correct answer in 15 guesses, then the computer should print the following message:
Sorry, you have lost the game!
Would you like to play the game again (y/n)? n
Thanks for playing Master Mind!
The program must have the property that the assignment of peg colors must be random and potentially different each time the game is played; you cannot initialize a fixed assignment of colors of pegs (i.e., at the start of the program you cannot assign a specific character constant to each peg). You can do this by using rand(), srand(), and time() library functions as discussed in the class.
Your program design must have the following properties.
1. Your program must be “modular” which means that it must be divided into multiple functions (at least one function besides function main()) in a manner that reflects the semantics of the program (i.e. put together into a function that naturally belongs together).
2. Each of your functions must have at least one local variable that is used within the function (in some way that is useful within the program) and there must be at least one global variable.
3. You must use arrays and for statements when appropriate.
4. You need to pass at least one non-array variable by value to a function, at least one array by reference to a function. The function that you pass a parameter to should use that parameter within the function body in some meaningful manner.
5. At least one function must have an int return value that is used by the statements that calls the function.


This is what i have so far.



#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

char B, P, G, R, O, Y;

void randompick();//Initilizes the function to randomly pick colors
void Introduction();

char function(char guess[char g],char correct[char real]);

int main()
{
char response;
char user[4];
int turn=0;
do
{

Introduction();

randompick();
for(turn=0;turn<15;turn++
{
cout<<"\n"<<"Current guess number: "<<"\n";
cout<<"Pick the colors you chose: ";
for(int i=0; i<4; i++)
{
cin>>user[i];
}
cout<<turn<<" "<<user[0]<<user[1]<<user[2]<<user[3]<<" ";
}
function(user[i],colors[act]);



cout<<"Would you like to play the game again (y/n)?"<<"\n";
cin>>response;
}
while(response=='y');
return 0;
}




void Introduction()
{
cout<<"Welcome to Master Mind."<<"\n"<<"The objective of the game is to guess the colors of the pegs in the order that they are set, and in under 15 moves."<<"\n"<<" If you chose correctly you will see '$'."<<"\n"<<"If you have the correct choice but the wrong place a '*' will be shown."<<"\n"<<"All letters entered have to be in upper case"<<"the choices are: B,P,G,R,O, and Y"<<"\n";
}




void randompick()
{
char colors[4];
srand(time(0));
int randomint = (rand()%6)+1;

for(int act=0; act<4; act++)
{
randomint=(rand()%6)+1;
switch(randomint)
{
case 1:
colors[act]='B';
break;
case 2:
colors[act]='P';
break;
case 3:
colors[act]='G';
break;
case 4:
colors[act]='R';
break;
case 5:
colors[act]='O';
break;
case 6:
colors[act]='Y';
break;
}
}
}

int function(char guess[char g],char correct[char real])
{
int compmatch
int partialmatch
char array[4];
for(int pick; pick<4; pick++)
{
if (guess[g]=correct[real])
{
compmatch++;
}
if (guess[g]=correct[real+1]||guess[g]=correct[real+2]||guess[g]=correct[real-1])
{
partialmatch++;
}
}

if(compmatch=4)
{
array[0]=='$';
array[1]=='$';
array[2]=='$';
array[3]=='$';
}
if(partialmatch=4)
{
array[0]=='*';
array[1]=='*';
array[2]=='*';
array[3]=='*';
}
}
Last edited on
Here is an update


#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
char colors[4];
char B, P, G, R, O, Y;

void randompick();//Initilizes the function to randomly pick colors
void Introduction();

char function(char guess[g],char correct[real]);

int main()
{
char response;
char user[4];
int turn=0;
char i;
do
{

Introduction();

randompick();
for(turn=0;turn<15;turn++)
{
cout<<"\n"<<"Current guess number: "<<"\n";
cout<<"Pick the colors you chose: ";
for(int i=0; i<4; i++)
{
cin>>user[i];
}
cout<<turn<<" "<<user[0]<<user[1]<<user[2]<<user[3]<<" ";
}
function(user[i],colors[char act]);



cout<<"Would you like to play the game again (y/n)?"<<"\n";
cin>>response;
}
while(response=='y');
return 0;
}




void Introduction()
{
cout<<"Welcome to Master Mind."<<"\n"<<"The objective of the game is to guess the colors of the pegs in the order that they are set, and in under 15 moves."<<"\n"<<" If you chose correctly you will see '$'."<<"\n"<<"If you have the correct choice but the wrong place a '*' will be shown."<<"\n"<<"All letters entered have to be in upper case"<<"the choices are: B,P,G,R,O, and Y"<<"\n";
}




void randompick()
{
char colors[4];
srand(time(0));
int randomint = (rand()%6)+1;

for(int act=0; act<4; act++)
{
randomint=(rand()%6)+1;
switch(randomint)
{
case 1:
colors[act]='B';
break;
case 2:
colors[act]='P';
break;
case 3:
colors[act]='G';
break;
case 4:
colors[act]='R';
break;
case 5:
colors[act]='O';
break;
case 6:
colors[act]='Y';
break;
}
}
}

char function(char guess[g],char correct[real])
{
int compmatch;
int partialmatch;
char array[4];
char x;
for(int pick; pick<4; pick++)
{
if (guess[g]=correct[real])
{
compmatch++;
}
if (guess[g]=correct[real+1]||guess[g]=correct[real+2]||guess[g]=correct[real-1])
{
partialmatch++;
}
}

if(partialmatch=4)
{
array[0]=='*';
array[1]=='*';
array[2]=='*';
array[3]=='*';
}
if(partialmatch=3)
{
array[0]=='*';
array[1]=='*';
array[2]=='*';
}
if(partialmatch=2)
{
array[0]=='*';
array[1]=='*';
}
if(partialmatch=2)
{
array[0]=='*';
}
if(compmatch=4)
{
array[0]=='$';
array[1]=='$';
array[2]=='$';
array[3]=='$';
}
if(compmatch=3)
{
array[1]=='$';
array[2]=='$';
array[3]=='$';
}
if(compmatch=2)
{
array[2]=='$';
array[3]=='$';
}
if(compmatch=2)
{
array[3]=='$';
}
cout<<array[x];
}
Topic archived. No new replies allowed.