Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有函数的简单对策问题的c问题新_C - Fatal编程技术网

具有函数的简单对策问题的c问题新

具有函数的简单对策问题的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

我是c新手,我试着做一个简单的hangedman游戏,我不知道为什么在gcc“输入结束时的预期声明或语句”中doesent work会出错 我是c语言的新手,我会努力学习。 我错过了什么?我的功能不对?有什么建议可以让你学会理性思考吗? 你给我的直升机要提前交给thanx

#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”

一个非常基本的刽子手程序的结构是:

  • 选择要猜测的单词。选择生命数
  • 创建空白版本的word以跟踪进度。显示此空白版本,它指示播放机的长度
  • 让玩家猜一个字母。跳过已经猜到的字母
  • 更新空白版本中字母出现在单词中的所有位置。
    • 若失去生命,则游戏结束
  • 检查空白版本中字符的变化量是否与单词的长度匹配。
    • 赢得条件,或返回步骤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);
    }