C++ guess game w/ vectors

missseducated,

This vector stores the answer. In this example, the answer is "orion". Of course, this vector would be hidden from the player at least until he guesses all letters correctly.
vector <char> answer{'o','r','i','o','n'};

This vector represents hidden characters. This is what the player would see. Each letter is revealed as the player guesses it correctly.
vector <char> vectorTwo{'*','*','*','*','*',};

You are not following the specifications provided for each function. I will explain why at the end.

checkGuess() - This function will return if a match is found in the answer vector passed into it. You will need to pass the user's guess to compare, the correct answer vector. This function might not make sense to you at first, but try to use this as a utility function. If it's still confusing, try doing reveal letter first and see if this still feels necessary. This might also be a good function to check for the user guessing a letter that's already been revealed. Just a thought!

revealLetter() - This void function should be passed the matching guess char, the answer vector, the word vector, and REFERENCE to "revealed" so it can be iterated. This function should look through the answer vector and find the matching element between the user's guess and the answer vector. Copy the answer element data into the word array at that exact position to simulate revealing the letter. Lastly, increment revealed.


For right now, I will give feedback on your current codes.

1. I want to make a note about your int revealed variable. You first initialize it to 0 in the main function and then, your intention is to probably pass that variable as a parameter to the checkGuess function, which supposedly increments it if user guesses a letter correctly. However, you are not passing that variable to the checkGuess function by reference. This means that when you increment revealed in the checkGuess function, this variable will NOT change in the main function, so whenever you call checkGuess from the main, revealed will always be 0.

Just so you know, pass-by-reference looks like this. & is commonly know as the "Address-Of" operator. http://www.cplusplus.com/doc/tutorial/functions/ Read section - Arguments passed by value and by reference.
 
vector <char> checkGuess(char guess,vector <char> answer, int & revealed)


2. You are trying to access vectorTwo in the checkGuess function (Line 67), but you don't pass vectorTwo to this function as a parameter. It seems that you pass std::vector<char> answer, which is supposedly the same answer in the main function, but where is vectorTwo in the checkGuess function?

3. You have the option of passing the vectorTwo vector as a reference and update items in the vectorTwo in the checkGuess function, so you wouldn't need to return vector<char> and instead, turn the function into a void function. But, I guess this really depends on the specifications provided by whoever gave you this assignment.

Your logic for revealing a letter will probably look similar to the below pseudocode.
1
2
3
4
5
6
7
8
9
10
11
12
// answer.size() will return the size of the container, which is 5 in your example
for(int i = 0; i < answer.size(); i++) {
    // The player's guess is in the answer vector.
    if(answer[i] == guess) {
        // We know that vectorTwo's index match that of the answer vector.
        // Set the item in vectorTwo to the actual character value from the same index items from answer.
        vectorTwo[i] = answer[i];

        revealed++;
    }
    // Since the answer can contain more than 1 same letter, continue the loop.
}

Starting Reference on std::vector: http://www.cplusplus.com/reference/vector/vector/?kw=vector

Finally, let's discuss how you are supposed to implement the checkGuess and the revealLetter functions.

checkGuess() is supposed to return boolean (true or false) depending on whether the player's guess is found in the answer or not. However, you are trying to code the revealLetter() logic in this function. This is supposed to be a utility function.

revealLetter() most likely will call checkGuess() to check if player's guess is found in the answer and if so, it will reveal the letter in vectorTwo. The codes that you currently have in the checkGuess() should be in this function instead.

Sorry for the long post, but I hope it helps.

Last edited on
It's a little silly to use vector of char in this case. Sequences of char should generally be represented using string.

vector<char> and string are more similar than some might expect, but there are substantial advantages to using a type designed specifically for your use case.

In general, using types to the fullest extent will make your job easier and lead directly to higher-quality software.
Last edited on
You don't have to edit your original post to answer, you can also post a new reply, It can be interesting for other people to see what you problem was and other people's explanations ;)
Last edited on
Topic archived. No new replies allowed.