o.k., so as i stated in my first post, I'm very new to this, and not very good at it. I have been able to get though the labs up to this point. But I am having some issues with my final lab. One thing I want to make clear is I dont want someone to just do the work for me, I need to be able to understand what it is I'm not getting so I can go on to the next course and not be completely over my head.

This lab is to put together a "Mastermind" game (if you know what that is, this is the first time I've seen it, but I think I understand the idea behind it) To do this, we were giving the code to make the random four letter key, and the right libs. (<ctime>, <cstdlib> and <string> to add to the usually <iostream>) I've been able to get to the point where I can get the input for someone, have it stored (in this case as "size") but I'm fairly lost as to what to do from here. I know I need to make a loop (the user can only have 8 guesses to get it right) and some way to add this guess to a string and tell the user how many of the 4 they got right. I'm just not quite sure how to do this? Can you even add a string to while loop?

I'm sorry if this isn't making much sense, its finals week and I've been studying like mad. If you dont understand, by all means, just ignore me ^_^. If however it does and you dont know how to explain without just doing it for me, maybe you have a link that I could use?

## All 10 Replies

I don't really understand. Here it is what I am understand so far:
+ First, we need to generate 4 random letters word.
+ Player can only guess one letter for 8 times.

For example:
(computer generate this letter hstd)
Guess this word ****
There is no letter o in this word.
You have found letter(s) in this word.
The word: **t*
.......

Am I right?

I don't really understand. Here it is what I am understand so far:
+ First, we need to generate 4 random letters word.
+ Player can only guess one letter for 8 times.

For example:
(computer generate this letter hstd)
Guess this word ****
There is no letter o in this word.
You have found letter(s) in this word.
The word: **t*
.......

Am I right?

well I already have the 4 letter word, and the letters can only be from a to f. Thank you for trying but no, the game is that the program will ask the user to guess four letters from a to f (i.e. "abca") it will then compair this to the key (which the program makes randomly on its own) and if the user guessed a right letter in a right place it will tell them how many (as in. if the key is "abab" and the user guesses "acda" the program will output "1" and then it will start over until the user as input 8 guesses)

Perhaps it would help if I put the code I have so far in here. Please keep in mind its not finished, and aside from the whole keeps running thing its working, I'm just not sure where to take it form here. This is not all my code, some of it was given to us from the teacher (the part that we haven't gone over yet) and I haven't "made it pretty" yet

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

main ()
{

// Varubles
int size = 4;
int maxtries = 8;
int range = 6;
char lowest = 'a';
char highest = lowest + range - 1;
int j = 1;

//from site

srand(time(0));
for (int i = 0; i < size; i++) {
char ch = lowest + rand() % range;
}

// starting output "instructions"
cout << "Welcome to Mastermind\n\n";
cout << "You must guess a random sequence of 4 letters\n";
cout << "from 'a' to 'f' within 8 tries.  After each\n";
cout << "guess, I'll tell you how many letters were right.  To be\n";
cout << "counted, you must guess the right letter in the right position.\n\n\n";

while (j < 11, j++)

cout << "Please enter a guess of 4 letters.\n";
cin >> size;

return 0;
}``````

It would help if you post the code you have, then those who help will have an idea of where to start.

To begin with, have you constructed a loop that ends either when the user has a correct guess or has used up the eight attempts?

How did you get the input from the user? You say "as size", that doesn't sound like you've stored it to a string.

Val

-> Generate 4 letters
-> Start Loop from 0 to 7 (which is 8 times)
----> Get user input
----> Compare how many characters are matched
--------> If 4 characters are matched, end loop.
-> End Loop

``````while ([B]j <= 8[/B]) [b]{[/b]
cout << "Please enter a guess of 4 letters.\n";
cin >> ????;

// After recieve the user's input, you need to
// compare how many letters are matched
// If 4 letters are matched then break;
// break; <-- to end your loop.

[b]j++;[/b]
[b]}[/b]``````

1) You want to loop only 8 times right?
2) You need to open and close brace.
3) You ask your player to input Integer or input 4 letters string?

First of all, your formatting is fairly good, just a tad inconsistent. Be a little more careful with your indentation.

The concept of mastermind is almost identical to the game Lingo if you get the cable station GSN (Game Show Network).

After the user enters his guess, you need to compare that guess character by character with the target "word". Tell that user
1) the number of letters that are correct AND in the correct postition
2) the number of letters that are correct but in the incorrect postition

Example:
Guess acdb
Result:
1 character correctly placed
2 characters incorrectly placed

A is of course in the correct position and CD are correct but in the wrong position.
The way I've done this is three loops.
Loop 1 to check for correct positions
Loop 2&3 are nested to check for the other letters that may be correct.

A is of course in the correct position and CD are correct but in the wrong position.
The way I've done this is three loops.
Loop 1 to check for correct positions
Loop 2&3 are nested to check for the other letters that may be correct.

I really don't know much about the rule of this game. I'm seeking to learn more about it. What's happened in this case:?
Guess: dcdb

0 characters correctly placed
2 or 3 characters incorrectly placed?

*I found what I was doing wrong after staring at it for awhile, but thanks for looking. For those who are wondering, I had to reset the 'count' to 0 in the while loop*

thank you all for all the help. I'm getting alot closer to being done, but I am still running into a problem. The sad thing is, I'm not all together sure what the problem is, but I am thinking it has something to do with the count? Can anyone see what I'm doing wrong here?

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

int main()
{

// Var
int size = 4;
int maxtries = 8;
int range = 6;
char lowest = 'a';
char highest = lowest + range - 1;
int j = 1;
int count = 0;
string guess = "";

//from site. random number

srand(time(0));
for (int i = 0; i < size; i++) {
char ch = lowest + rand() % range;
}

// starting output "instructions"
cout << "\n\n\n            Welcome to Mastermind\n\n";
cout << "   You must guess a random sequence of 4 letters ";
cout << "from 'a' to 'f' within 8 tries.  After each ";
cout << "guess, I'll tell you how many letters were right.  To be ";
cout << "counted, you must guess the right letter in the right position.\n\n\n";

//input
while (j <= 8) {
cout << "\n     Please enter a guess of 4 letters between 'a' and 'f'.\n";
cin >> guess;

{
count ++;
}

{
count ++;
}

{
count ++;
}

{
count ++;
}

cout << "   You got " << count << " right\n";

//Winner
{
cout << "        You won! The correct answer was " << guess << ". \n\n" << "         It took you " << j << " guesses.";

break;
}

// Loser
if (j == 8)
{
cout << "        Sorry, you couldn't guess the answer. \n\n You Lost.\n";

break;
}

j++;
}

return 0;
}``````

oh and I can't show if they guess a right letter but in the wrong place, I probably could figure it out after awhile, but this is something the teacher was very clear about. I can only tell them if the guess the right letter and in the right place. Though that would be more fun.

Rather than test each character with separate IFs, use a FOR loop.

And to test for a winner, just look a COUNT. If it's 4, you won. Simply break out of the loop. After the loop, check COUNT again. If it's 8, you lost, if it's not, you won. Don't print WIN or LOSE inside the loop. The loop should be only for checking the input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.