c - 刽子手游戏错误
问题描述
所以这几乎可以工作,输入最后一个字符时只是一个错误,由于某种原因它检测为错误字符。我试过在 GDB 中调试它,但不知道为什么会这样
同样在任何人评论这都是 C 代码之前,我们的教授希望我们从一个 C 程序开始,然后将其更改为 C++,因为我们将它构建成一个带有图形等的成熟游戏。
我也知道有更好的方法可以用更多的功能来构建它,但让它工作是现阶段的首要任务
散布着一些注释掉的代码,这只是过去试图让它工作的尝试——随意忽略
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#define MAX_INPUT 1024
void updateAvailableLetters(char *_arrAvailableLetters[0], char _currentLetter);
int main(int argc, char *argv[])
{
if (argc == 0)
{
srand(time(NULL)); //seeds the rand() function with the number of seconds since epoch date so rand() will change
}
else
{
srand((int)argv[1]); //seeds the rand() function with the command line argument
}
const char arrLetters[] = {"abcdefghijklmnopqrstuvwxyz"}; //array of letters to be used
char *arrLettersMem = (char*)malloc(strlen(arrLetters)+1); //memory for strcpy
char *arrAvailableLetters[] = {strcpy(arrLettersMem, arrLetters)}; //array of available letters which will be updated
const char *arrWordList[] = {"hello", "goodbye", "this", "is", "a", "game"}; //array of secret words
int iWordCount = sizeof(arrWordList)/sizeof(arrWordList[0]); //number of words in the array
printf("This is my hangman game. Please guess a letter to avoid the hangman!\n");
while (true) //main game loop
{
const char *pCurrentWord = arrWordList[rand() % iWordCount]; //randomly select a word
int iWordLength = strlen(pCurrentWord); //selected word length
char *pCurrentWordMem = (char*)malloc(sizeof(*pCurrentWord * iWordLength)); //pointer to the dynamically allocated memory
char *pCurrentWordCopy = strcpy(pCurrentWordMem, pCurrentWord); //copy of selected word
char *pBlankWord = (char*) memset(pCurrentWordCopy,'-',iWordLength); //letters found so far
int iLettersGuessed = 0; //number of correct guesses
int iWrongGuessesLeft = 8;
bool inWord = false;
while(iWrongGuessesLeft > 0 && iLettersGuessed != iWordLength)
{
printf("\n");
char c;
while((c = getchar()) != '\n' && c != EOF){
char currentLetter = tolower(c);
inWord = false;
if (strchr(arrLetters, currentLetter) == NULL) //quick validation
{
printf("Please try a letter from a-z!\n");
break;
}
// char compare = *strchr(arrAvailableLetters[0], currentLetter);
// if (strchr(arrAvailableLetters[0], currentLetter) != NULL){
if (strchr(arrAvailableLetters[0], currentLetter) == NULL){ //is the current letter already chosen?
{
printf("You have already chosen %c, guess again!\n", currentLetter);
break;
}
}
//is the currentLetter in the chosen word?
for (int i = 0; i < iWordLength - 1; i++){
if(currentLetter == pCurrentWord[i]){
pBlankWord[i] = currentLetter;
iLettersGuessed++;
updateAvailableLetters(arrAvailableLetters, currentLetter);
printf("*** CORRECT GUESS *** - %c IS in the word!\n", currentLetter);
printf("Guess a letter for the word: %s\n", pBlankWord);
printf("You have %d guesses left, and have guessed %d letters so far.\n", iWrongGuessesLeft, iLettersGuessed);
printf("Available letters are: %s\n", arrAvailableLetters[0]);
inWord = true;
}
}
if (inWord == false){
// for (int j = 0; j < iWordLength - 1; j++){
// if(currentLetter != pCurrentWord[j]){
iWrongGuessesLeft--;
updateAvailableLetters(arrAvailableLetters, currentLetter);
// *strchr(arrAvailableLetters[0], currentLetter) = '.'; //update available letters
printf("!!! WRONG GUESS !!! - %c IS NOT in the word!\n", currentLetter);
printf("Guess a letter for the word: %s\n", pBlankWord);
printf("You have %d wrong guesses left, and have guessed %d letters so far.\n", iWrongGuessesLeft, iLettersGuessed);
printf("Available letters are: %s\n", arrAvailableLetters[0]);
// }
// }
}
}
// if (currentLetter == *pCurrentWord) //is the currentLetter in the currentWord
// {
// *pBlankWord = currentLetter;
// iLettersGuessed++;
// for (int i = 0; i < 26; i++){ //updates arrAvailableLetters with '.'
// if (arrAvailableLetters[0][i] == currentLetter){
// arrAvailableLetters[0][i] = '.';
// break;
// }
// }
// // *strchr(arrAvailableLetters[0], currentLetter) = '.'; //update available letters
// printf("*** CORRECT GUESS *** - %c IS in the word!\n", currentLetter);
// printf("Guess a letter for the word: %s\n", pBlankWord);
// printf("You have %d guesses left, and have guessed %d letters so far.\n", iWrongGuessesLeft, iLettersGuessed);
// printf("Available letters are: %s\n", arrAvailableLetters[0]);
// pCurrentWord++;
// }
// else
// {
// iWrongGuessesLeft--;
// for (int q = 0; q < 26; q++){
// if (arrAvailableLetters[0][q] == currentLetter){
// arrAvailableLetters[0][q] = '.';
// break;
// }
// }
// // *strchr(arrAvailableLetters[0], currentLetter) = '.'; //update available letters
// printf("!!! WRONG GUESS !!! - %c IS NOT in the word!\n", currentLetter);
// printf("Guess a letter for the word: %s\n", pBlankWord);
// printf("You have %d wrong guesses left, and have guessed %d letters so far.\n", iWrongGuessesLeft, iLettersGuessed);
// printf("Available letters are: %s\n", arrAvailableLetters[0]);
// }
// }
}
free(pCurrentWordMem);
free(arrLettersMem);
char buffer[MAX_INPUT];
if (iWrongGuessesLeft == 0)
{
printf("Oh no, you got hanged! Do you want to try again y/n?");
while (fgets(buffer,MAX_INPUT,stdin) != NULL)
{
if (buffer[0] == 'y' || buffer[0] == 'Y')
break;
else if (buffer[0] == 'n' || buffer[0] == 'N')
{
return 0;
}
else
printf("Enter (y/n)\n");
}
}
if (iLettersGuessed == iWordLength)
{
printf("Congratulations, you guessed the word! Do you want to try again y/n?");
while (fgets(buffer,MAX_INPUT,stdin) != NULL)
{
if (buffer[0] == 'y' || buffer[0] == 'Y')
break;
else if (buffer[0] == 'n' || buffer[0] == 'N')
{
return 0;
}
else
printf("Enter (y/n)\n");
}
}
}
}
void updateAvailableLetters(char *_arrAvailableLetters[0], char _currentLetter)
{
for (int i = 0; i < 26; i++){ //updates arrAvailableLetters with '.'
if (_arrAvailableLetters[0][i] == _currentLetter){
_arrAvailableLetters[0][i] = '.';
break;
}
}
}
解决方案
问题似乎是这一行:
for (int i = 0; i < iWordLength - 1; i++){
iWordLength
之前设置为strlen(pCurrentWord)
,并且您想要遍历单词中的所有字母。上面的代码只会迭代到倒数第二个字母。将条件更改为
for (int i = 0; i < iWordLength; i++){
推荐阅读
- qt - 在 QT Creator 中编辑多个 QT 小部件 (GUI)
- sas - 总处理时间 PROC SQL,了解日志
- iis - TargetInvocationException - 对象引用未设置为对象的实例。对于自动映射器
- python - 控制台入口点不适用于 pip install 的 --target 参数
- javascript - Express.js 实时数据流
- java - 如何模拟 GSON 自定义解串器?
- gulp - gulp 构建和部署中的作曲家
- javascript - 调用内部有更多异步函数的异步函数
- office-js - 在任务窗格外使用 Office-JS
- javascript - 如何通过 __vue__ 访问循环项的对象?