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
here is my code:
My problem is that i couldn't get each function to communicate with each other in main() WITH PROPER VARAIBLES. I compiled my code, and most errors occured in main(), but I really don't know how to fix those error. This my first doing functions without any programming background.
I hope you guys/girls will help me "fix the codes" for me and tell me the spots where i got messed up. This code is driving me crazy, but i am willing to learn.
100 thanx to whoever took the time helping me out:bowdown: :bowdown: :bowdown:
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
here is my code:
Code:
#include <iostream.h>
#include <stdlib.h>
//----------------------------------------------------------------------
void GeneratePegColor (int &peg1, int &peg2, int &peg3);
void UserGuess (int guess1, int guess2, int guess3);
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);
//----------------------------------------------------------------------
int main()
{
int GuessTaken = 1;
GeneratePegColor(peg1, peg2, peg3);
do{
cout << "Guess " << GuessTaken;
UserGuess(guess1, guess2, guess3);
cout << "You have " << ReturnNumberOfCorrectPeg () << " peg(s)" << ReturnNumberOfCorrectPegColor(); << " color(s)";
GuessTaken++;
while (CorrectPosition < 3);
cout << "You have broken the the code in " << GuessTaken << " guesses";
return(0);
}
//-----------------------------------------------------------------------
void GeneratePegColor (int &peg1, int &peg2, int &peg3)
/* Generate random peg colors */
{
do{
peg1 = random(5)+1;
peg2 = random(5)+1;
} while (peg1==peg2);
do {
peg3 = random(5)+1;
} while ((peg3==peg1)||(peg3==peg2));
}
//-----------------------------------------------------------------------
void UserGuess (int guess1, int guess2, int guess3)
/* 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): ";
cin >> guess3;
while (guess1==guess2||guess1==guess3||guess3==guess2){
cout << "**WARNING**: Please enter three DIFFERENT colors again."<<endl;
cout << "\tFirst peg (1-5): ";
cin >> guess1;
cout << "\tSecond peg (1-5): ";
cin >> guess2;
cout << "\tThird peg (1-5): ";
cin >> guess3;
}
}
//-----------------------------------------------------------------------
int ReturnNumberOfCorrectPeg (int Peg1, int Peg2, int Peg3,int guess1, int guess2, int guess3, int %CorrectPosition)
// 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, int &CorrectColor++)
// 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);
}
//-----------------------------------------------------------------------
My problem is that i couldn't get each function to communicate with each other in main() WITH PROPER VARAIBLES. I compiled my code, and most errors occured in main(), but I really don't know how to fix those error. This my first doing functions without any programming background.
I hope you guys/girls will help me "fix the codes" for me and tell me the spots where i got messed up. This code is driving me crazy, but i am willing to learn.
100 thanx to whoever took the time helping me out:bowdown: :bowdown: :bowdown: