Guest_imported
New member
- Jan 1, 1970
- 0
Here is something neat that i wanna to try to make:
intro:
The game of Mastermid is played as follow: One player (the code maker) chooses a secret arrangement of colored pegs
and the other player (the code breaker) tried to guess it. The code breakers put forth an arrangement of colored
pegs, and the code maker reports two number:
1. The number of pegs that are correct color and in the correct postion.
2. The number of pegs that are the correct color regardless of whether they are in the correct postion
Task:
Write a program that plays the part of the code maker in this game. The code will consist of an arrangement of 3
"pegs" each with a "color" from 1 to 5. The program output may look similar to:
Guess 1:
First peg: 3
Second peg: 4
Third peg: 2
You have 1 correct peg(s) and 2 correct color(s)
Guess 2:
First peg: 3
Second peg: 4
Third peg: 5
You have 1 correct peg(s) and 3 correct color(s)
Guess 3:
First peg: 3
Second peg: 5
Third peg: 4
You have 3 correct peg(s) and 3 correct color(s)
You have borken the code in 3 guesses
**Note**: For simpilicity, do not allow either guess or the code to contain duplicates. You should write a function
that checks for duplicate pegs and use it to prevent duplicates.
Okay, here is the code that I have made contain. I suspect that there are many logic error, but I don't know how
to fix with little help of the compiler error msgs.
2 Biggest Problem I need help:
1. I need help with my logic and coding. It doesn't seem right to the compiler
2. As stated before:
"For simpilicity, do not allow either guess or the code to contain duplicates. You should write a function that checks for duplicate pegs and use it to prevent duplicates."
So, i made two prototypes in the code called DopeTest1 and DopeTest2 to check if there is no repeat in generating the random color and in user's guess. But I don't know the LOGIC behind how to perform this task. I really need some good detail helps. I have no idea on how to write the code for those two prototype.
Anyone can fix the code and tell me where are the problems for me?? Also do the code or tell me the logic behind checking no repeating in generating and guessing the color??? (eg. peg1 and peg2 should all equal to 1)
I really want to learn this program. This is a big step for me <--a newbie in C++. This is my first time doing function in C++ without any other programming background. Can someone who is pro at this take the time to help out? Thank you! Thank you!
intro:
The game of Mastermid is played as follow: One player (the code maker) chooses a secret arrangement of colored pegs
and the other player (the code breaker) tried to guess it. The code breakers put forth an arrangement of colored
pegs, and the code maker reports two number:
1. The number of pegs that are correct color and in the correct postion.
2. The number of pegs that are the correct color regardless of whether they are in the correct postion
Task:
Write a program that plays the part of the code maker in this game. The code will consist of an arrangement of 3
"pegs" each with a "color" from 1 to 5. The program output may look similar to:
Guess 1:
First peg: 3
Second peg: 4
Third peg: 2
You have 1 correct peg(s) and 2 correct color(s)
Guess 2:
First peg: 3
Second peg: 4
Third peg: 5
You have 1 correct peg(s) and 3 correct color(s)
Guess 3:
First peg: 3
Second peg: 5
Third peg: 4
You have 3 correct peg(s) and 3 correct color(s)
You have borken the code in 3 guesses
**Note**: For simpilicity, do not allow either guess or the code to contain duplicates. You should write a function
that checks for duplicate pegs and use it to prevent duplicates.
Okay, here is the code that I have made contain. I suspect that there are many logic error, but I don't know how
to fix with little help of the compiler error msgs.
Code:
#include <iostream.h>
#include <stdlib.h>
//---------------------------------------------------------------------
void GeneratePegColor (int &peg1, int &peg2, int &peg3);
void UserGuess (int guess1, int guess2, int guess3, int GuessesLeft);
int ReturnNumberOfCorrectPeg (int Peg1, int Peg2, int Peg3,int guess1, int guess2, int guess3);
int ReturnNumberOfCorrectPegColor (int Peg1, int Peg2, int Peg3,int guess1, int guess2, int guess3);
bool DopeTest1 (int var1,int var2, int var3);
bool DopeTest2 (int var1,int var2, int var3);
//---------------------------------------------------------------------
int main()
{
int GuessTaken = 1;
do{
cout << "Guess " << GuessTaken;
GeneratePegColor();
UserGuess();
cout << "You have "<< ReturnNumberOfCorrectPeg()<< "peg(s) and "<< ReturnNumberOfCorrectPegColor()<< "correct
Color(s)";
GuessTaken++;
return(0);
}
//----------------------------------------------------------------------
void GeneratePegColor (int &peg1, int &peg2, int &peg3)
/* Generate random peg colors */
{
peg1 = random(5)+1;
peg2 = random(5)+1;
Peg3 = random(5)+1;
}
//----------------------------------------------------------------------
void UserGuess (int guess1, int guess2, int guess3, int GuessesTook)
/* Display number of Guess taken, and ask the user to enter the guesses */
{
cout << "\tFirst peg (1-5): ";
cin >> guess1;
cout << "\tSecond peg (1-5): ";
cin >> guess2;
cout << "\tThird peg (1-5): ";
cout << guess3;
//----------------------------------------------------------------------
int ReturnNumberOfCorrectPeg (int Peg1, int Peg2, int Peg3,int guess1, int guess2, int guess3)
// Decide number of correct position
{
int CorrectPosition = 0;
if (guess1==peg1){
CorrectPosition++;
}
else if (guess2==peg2){
CorrectPosition++;
}
else if (guess3==peg3){
CorrectPosition++;
}
return (CorrectPosition);
//-----------------------------------------------------------------------
int ReturnNumberOfCorrectPegColor (int peg1, int peg2, int peg3,int guess1, int guess2, int guess3)
// Decide number of correct peg color regardless of wheter they are in correct position
{
int CorrectColor = 0;
if ((guess1==peg1)||(guess1==peg2)||(guess1==peg3)){
CorrectColor++;
}
else if ((guess2==peg1)||(guess2==peg2)||(guess2==peg3)){
CorrectColor++;
}
else if ((guess3==peg1)||(guess3==peg2)||(guess3==peg3)){
CorrectColor++;
}
return(CorrectColor);
}
//-----------------------------------------------------------------------
2 Biggest Problem I need help:
1. I need help with my logic and coding. It doesn't seem right to the compiler
2. As stated before:
"For simpilicity, do not allow either guess or the code to contain duplicates. You should write a function that checks for duplicate pegs and use it to prevent duplicates."
So, i made two prototypes in the code called DopeTest1 and DopeTest2 to check if there is no repeat in generating the random color and in user's guess. But I don't know the LOGIC behind how to perform this task. I really need some good detail helps. I have no idea on how to write the code for those two prototype.
Anyone can fix the code and tell me where are the problems for me?? Also do the code or tell me the logic behind checking no repeating in generating and guessing the color??? (eg. peg1 and peg2 should all equal to 1)
I really want to learn this program. This is a big step for me <--a newbie in C++. This is my first time doing function in C++ without any other programming background. Can someone who is pro at this take the time to help out? Thank you! Thank you!