我正在写一个刽子手游戏。我的逻辑和我的游戏逻辑都失败了。字符猜测(人们猜测的字母)没有被添加到向量guessArray 的正确内存槽中。假设该词是用户输入的词。
如果guessArray 是原始数组,我认为这会起作用。是否有某种原因这不适用于矢量?
//assume that attempts is num of attempts left
void coutWord(int attempts, std::string word, char guess)
{
std::vector<char> guessArray(word.length());
//this is supposed to add the guess to the correct area of guessArray;
//It's not.
for (int i = 0; i < word.length(); i++) {
if (guess == word[i]) {
guessArray[i] = guess;
std::cout << " " << guessArray[i] << " ";
continue;
}
std::cout << " _ ";
}
std::cout << std::endl << std::endl;
}
编辑:我使用此代码的目标是在同一个 for 循环中找出所有未猜测的空格和猜测的空格。我只需要“记住”以前的猜测,以便得到正确的输出。给定单词=“苹果酱”:
Input: a
a _ _ _ _ _ a _ _ _
Input: p
a p p _ _ _ a _ _ _
等等