【发布时间】:2025-12-29 17:45:13
【问题描述】:
我正在写一个刽子手游戏。我的逻辑和我的游戏逻辑都失败了。字符猜测(人们猜测的字母)没有被添加到向量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 _ _ _
等等。
【问题讨论】:
-
无论guessArray 是向量还是原始数组,这似乎都是相当有缺陷的代码。为了能够回答您的问题,您需要说出您在这里尝试做什么。 EG,如果单词是“Triangulate”,猜测是“g”,输出应该是什么?
-
那么我应该将向量作为另一个参数传递吗?
-
根据您的函数,您还需要遍历所有先前的猜测,因为您没有在调用之间保存guessArray。你可能想把它作为另一个参数传入,是的。