c - 输入猜测时刽子手游戏不起作用,但随机单词形式列表起作用
问题描述
我正在尝试为Hangman游戏编写逻辑。我希望能够输入一个单词并从列表中获取一个随机单词。
我选择随机单词的部分效果很好,没有问题。当我输入一个单词时,问题就开始了。无论我是否猜对了正确的字符,它仍然算作一次罢工。为什么会发生这种情况的解释是什么?
int main()
{
int option=5, randomword;
bool loop = true;
// here you choose if you want a random word or to enter one by yourself.
while (loop)
{
printf("choose an option:\n0 to enter a word.\n1 for the computer to choose a word for you.\n");
scanf("%d", &option);
// start the loop over again if the value that got scan is not 0 or 1;
if (option == 0 || option == 1)loop = false;
}
char EnterdWord[128];
// if you choose to enter a word it will scan it and enter it to a string. I am using fgets so you can enter space
if (option == 0)
{
printf("enter a word:\n");
getchar();
fgets(EnterdWord , 128, stdin);
}else // if you want a random word, this will make a random number, so later it will choose a random word from a list.
{
srand(time(NULL));
randomword = rand() % 20 ;
}
// random words list
char RanWord[][20] = {"dog", "cat", "sky", "orange", "apple", "movie", "code", "number" ,"ship" ,"google" ,"sushi" , "shower",
"super hero", "job interview" ,"computer" ,"video games" ,"pizza" ,"cell phone", "charger", "bottle"};
char *TempWord = malloc(1024 * sizeof(char));
// remove the \n from the scan in fgets.
if (*EnterdWord && EnterdWord[strlen(EnterdWord)-1] == '\n')EnterdWord[strlen(EnterdWord)-1] = 0;
// give to the string TempWord the word based on the option u choose
if (option == 0) strcpy(TempWord, EnterdWord);
if (option == 1) strcpy(TempWord, RanWord[randomword]);
//this is the string that you are going to scan your right guess.
char *NiceWord = malloc(1024 * sizeof(char));
if (NiceWord == NULL) return 0;
// copy temp word to nice word
strcpy(NiceWord, TempWord);
// finding the length of the word for the loop below (I know I can use while != \0)
int lengh = strlen(TempWord);
int i=0;
// this part is for aesthetics. I could have just changed all the characters to space.
while(i < lengh)
{
if (TempWord[i] != ' ')NiceWord[i]='_';
if (TempWord[i] == ' ')NiceWord[i]=' ';
i++;
}
loop = true;
char guess;
// bool verb to check for strikes.
bool CheckStrike = true;
int y, strikes=0;
// print so you can see how many characters the word is.
printf("\n%s\n", NiceWord);
while (loop == true)
{
y=0;
//scan your guess.
printf("enter your guess:\n");
getchar();
scanf("%c", &guess);
// reset the strike checker in case you where right for the last guess
CheckStrike = true;
// check if your guess is correct or not.
while(TempWord[y] != '\0')
{
if ( TempWord[y] == guess)
{
NiceWord[y] = guess;
CheckStrike = false;
}
y++;
}
// if your guess was false, add one strike
if (CheckStrike == true)strikes++;
printf("you have %d strikes left\n", 10-strikes);
printf("%s\n", NiceWord);
// check if you lost
if (strikes == 10)
{
printf("the word was: %s\nyou lost :( ", TempWord);
loop = false;
}
// check if you won
if (strcmp (NiceWord, TempWord) == 0)
{
printf("\n\n\n\nu won :) with %d strikes left!\n\n\n\n\n", 10-strikes);
loop = false;
}
}
return 0;
}
解决方案
利用:
printf("enter your guess:\n");
if (option == 1)
getchar();
scanf("%c", &guess);
if (option == 0)
getchar();
这解决了问题。
推荐阅读
- asp.net-core - 从客户端清空多项选择
- sql - 关于 CockroachDB 上表创建错误的说明
- javascript - 当其 id 具有特殊字符时,jqGrid 寻呼机不显示
- ruby-on-rails - 局部形式的当地人-rails 5
- java - 如何将数据与文件一起发送?
- c# - 将文件从一个 API 端点流式传输到另一个 API 端点
- c# - 检查每个单选按钮组是否选择/选中了一个单选按钮
- python - 为 ruamel.yaml 中的所有字段添加一组新属性
- javascript - 无法从类名 js-menu 中读取值,它说无法读取属性类名
- android - 如何在使用 kotlin 关闭活动之前获取 textView 的信息?