具有函数的简单对策问题的c问题新
我是c新手,我试着做一个简单的hangedman游戏,我不知道为什么在gcc“输入结束时的预期声明或语句”中doesent work会出错 我是c语言的新手,我会努力学习。 我错过了什么?我的功能不对?有什么建议可以让你学会理性思考吗? 你给我的直升机要提前交给thanx具有函数的简单对策问题的c问题新,c,C,我是c新手,我试着做一个简单的hangedman游戏,我不知道为什么在gcc“输入结束时的预期声明或语句”中doesent work会出错 我是c语言的新手,我会努力学习。 我错过了什么?我的功能不对?有什么建议可以让你学会理性思考吗? 你给我的直升机要提前交给thanx #include <stdio.h> #include <stdlib.h> #include <string.h> //function to find letter in string
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//function to find letter in string
int findletter(char y)
{
char c;
int i;
char secret[] = "outcast";
i = 0;
scanf("%c", &c);
while (c != secret[i] && i < strlen(secret))
i++;
if(c == secret[i])
return (1);
else
return (0);
}
//confirmation letter
int guessed(char a)
{
int z;
char guess [6] = {0};
z = 0;
while(findletter(guess[z]) != 1 && findletter(guess[z]) < 6)
{
z++;
if(findletter(guess[z]) == 1)
return 1;
else
return 0;
//word guessed
int tryguess(char v)
{
int x;
x = 0;
while(findletter(guess[x]) == 0)
{
x++;
if(findletter(guess[x] == 1))
return 1;
else
return 0;
}
}
int main()
{
char secret[] = "outcast";
char letter;
int lives;
char guess [6] = {0};
int i;
lives = 10;
i = 0;
printf("welcome to the hanged man\n");
while(i < 6)
{
if((findletter(secret[i] == 1)))
printf("%c", secret[i]);
else
printf("*\n");
i++;
}
return 0;
}
#包括
#包括
#包括
//函数查找字符串中的字母
int findletter(字符y)
{
字符c;
int i;
char secret[]=“弃儿”;
i=0;
scanf(“%c”、&c);
而(c!=secret[i]&&i
对代码的更正
int guessed(char a)
{
int z;
char guess [6] = {0};
z = 0;
while(findletter(guess[z]) != 1 && findletter(guess[z]) < 6)
{
z++;
if(findletter(guess[z]) == 1)
return 1;
else
return 0;
} // you forgot closing while loop here
} // function closing parenthesis
//word guessed
这将消除你犯括号错误的机会…从语法和逻辑的角度来看,这个程序有很多问题 一般性问题包括:
- 函数
及其猜测
循环未关闭(缺少while
)}
- 有很多未使用的代码(函数和变量)
- 行
将if((findletter(secret[i]==1))
的字符值与secret[i]
进行比较,并将结果传递给1
。但这并不重要,因为您不使用此参数,并在函数中接受用户输入findletter
- 您有硬编码的字符串和长度,这使得您的程序不那么动态,将来更难更改
- 使用
循环作为保护(在未使用的函数while
和tryguess
中),它们总是在第一次迭代时退出gustized
只需检查findletter
是否包含字符secret
,并在第一次出现时返回c
int findletter(char unused) {
char secret[] = "secret",
c;
scanf(" %c", &c);
for (size_t i = 0; i < strlen(secret); i++)
if (secret[i] == c)
return 1;
return 0;
}
可以简单地简化为
return a == b;
除上述问题外,你的程序结构没有多大意义。如果我们的程序工作,你基本上是让玩家猜一个长度未知的单词,一次猜一个字符。他们也可以简单地猜任何字母来显示当前的字母。你可以“解”整个单词
“secret
”只需反复输入“s”
一个非常基本的刽子手程序的结构是:
- 若失去生命,则游戏结束
- 赢得条件,或返回步骤3
#include <stdio.h>
#include <string.h>
size_t update_all(char *to, const char *from, size_t len, char g) {
size_t changed = 0;
for (size_t i = 0; i < len; i++)
if (from[i] == g) {
to[i] = g;
changed++;
}
return changed;
}
void play_hangman(const char *word, unsigned lives) {
size_t word_length = strlen(word),
blanked_length = 0;
char blanked[word_length + 1],
guess = '\0';
for (size_t i = 0; i < word_length; i++)
blanked[i] = '*';
blanked[word_length] = '\0';
while (lives) {
printf("The word: [%s]\n"
"(Lives = %u) Enter a guess: ",
blanked,
lives);
scanf(" %c", &guess);
if (strchr(blanked, guess)) {
printf("[%c]: Already guessed!\n", guess);
continue;
}
size_t found = update_all(blanked, word, word_length, guess);
blanked_length += found;
if (!found) {
printf("[%c]: NOT FOUND!\n", guess);
lives--;
} else
printf("[%c]: FOUND!\n", guess);
if (!lives)
puts("Out of lives! You lose!");
else if (blanked_length == word_length) {
printf("You win! Word is [%s].\n", word);
return;
}
}
}
int main(void) {
play_hangman("secret", 10);
}
#包括
#包括
大小更新全部(字符*到,常量字符*从,大小长度,字符g){
改变的大小=0;
对于(大小i=0;i
请注意,这个程序远不是完美的,因为它不能完全跟踪猜测的字母,因此玩家可以多次猜测同一个错误的字母,每次都会失去一条生命。要解决这个问题,我们需要更多的状态,收集玩家做出的每一次猜测,并使用这些数据,而不是天真的if(strchr(blanked,guess))
它还使用'*'
字符作为哨兵值,这
return a == b;
#include <stdio.h>
#include <string.h>
size_t update_all(char *to, const char *from, size_t len, char g) {
size_t changed = 0;
for (size_t i = 0; i < len; i++)
if (from[i] == g) {
to[i] = g;
changed++;
}
return changed;
}
void play_hangman(const char *word, unsigned lives) {
size_t word_length = strlen(word),
blanked_length = 0;
char blanked[word_length + 1],
guess = '\0';
for (size_t i = 0; i < word_length; i++)
blanked[i] = '*';
blanked[word_length] = '\0';
while (lives) {
printf("The word: [%s]\n"
"(Lives = %u) Enter a guess: ",
blanked,
lives);
scanf(" %c", &guess);
if (strchr(blanked, guess)) {
printf("[%c]: Already guessed!\n", guess);
continue;
}
size_t found = update_all(blanked, word, word_length, guess);
blanked_length += found;
if (!found) {
printf("[%c]: NOT FOUND!\n", guess);
lives--;
} else
printf("[%c]: FOUND!\n", guess);
if (!lives)
puts("Out of lives! You lose!");
else if (blanked_length == word_length) {
printf("You win! Word is [%s].\n", word);
return;
}
}
}
int main(void) {
play_hangman("secret", 10);
}