字符排列格式化
我正在处理一个不正确的字母猜测存储在名为wrongletters
的字符数组中的hang子手游戏。当然,用户以零错误开始游戏,所以wrongletters
数组在声明后保持空。我遇到的问题是,当我尝试显示错误的字母时,由于数组中的所有其他非值元素,字母的间隔距离非常远字符排列格式化
预期:(推测字母:ABCD)
电流:(猜字母:(多余的空格)ABCD)
有什么想法? (我知道游戏不能正常工作尚未):
void gameSequence() // Runs the hangman game loop
{
// Local and Global Variable Declaration and Initialization
char guessLetter = ' ';
guessWord = strToUpper(getNextWord());
string maskedWord(guessWord.size(), '_');
char wrongLetters[26] = {};
int numWrongLetters = sizeof(wrongLetters)/sizeof(wrongLetters[0]);
// Input, Process, and Output
cout << "\nLet's PLAY\n\n";
for (int i = 0; i < maskedWord.length(); i++)
cout << maskedWord[i] << " ";
while (incorrectCount < 6)
{
drawHangman(incorrectCount);
cout << "<<<<<<<<<< MAKE A GUESS >>>>>>>>>>\n\n";
cout << "Guessed Letters: ";
for (int i = 0; i < 26; i++)
cout << wrongLetters[i] << " ";
cout << "\n\nEnter a letter to guess: ";
cin >> guessLetter;
cout << endl;
guessLetter = toupper(guessLetter);
for (int i = 0; i < maskedWord.length(); i++)
cout << maskedWord[i] << " ";
if (guessWord.find(guessLetter) != string::npos)
{
for (int i = 0; i < maskedWord.length(); i++)
{
if (maskedWord[i] == guessLetter)
maskedWord[i] = guessLetter;
}
}
else
{
incorrectCount++;
wrongLetters[incorrectCount] = guessLetter;
bubbleSort(wrongLetters, numWrongLetters);
}
if (incorrectCount == 6)
{
drawHangman(incorrectCount);
cout << "Sorry you lose - the word was: " << guessWord << endl << endl;
}
}
incorrectCount = 0;
}
据我了解阵列wrongletters
包含年初至今guesed错字母了。所以没有一点打印它,尤其是排序所有它。
因此,你应该改变:
for (int i = 0; i < incorrectCount; i++) // incorrectCount replaced 26
cout << wrongLetters[i] << " ";
...
else
{
incorrectCount++;
wrongLetters[incorrectCount] = guessLetter;
bubbleSort(wrongLetters, incorrectCount+1); // incorrectCount replaced numWrongLetters
}
否则,在所有种类的阵列的空间先走之前错误的字母。
我做了你推荐的修改,修正了空格问题,但现在猜对的字母排序不正确。我测试了'bubblesort()'函数,我知道它正常工作,所以这不是问题。我似乎无法弄清楚为什么猜测不能正确排序了...... – KnightValor
@KnightValor也许你应该发送'incorrectCount + 1'到'bubblesort'? –
啊好点。这解决了问题。谢谢你的帮忙。 – KnightValor
因为即使你的字符序列是空的,你要求你的循环在错误的字母[i]后面显示一个空格。在'通过ENDL更换,你将有
一个
乙
Ç
d
OP问题是**之前的空格** –
不要使用数组。使用优先队列。这将使列表保持良好的排序和大小。 – user4581301
@ user4581301这是一个赋值的一部分,它指定我必须使用一个数组,所以不幸的是我不能使用优先级队列。 – KnightValor