C语言 用户输入和Win条件的问题

lc8prwob  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(135)

我一直在做一个简单的C语言的刽子手游戏,我遇到了几个问题,我正在努力解决。游戏似乎有用户输入和获胜条件的问题,我面临的问题是,当用户输入不在单词和扣除的机会,我想限制用户输入一个字符的时间在这里是我写的

#include <stdio.h>
#include <time.h>
#include "./random.h"
#include <stdbool.h>
#include <windows.h>
#define ARRAY_SIZE sizeof(words) / sizeof(words[0])
//------------------------------------------------------------
int main()
{
    srand(time(NULL));
    char *words[] = {
        "hello",
        "superman"};
    char *random_word = random(ARRAY_SIZE, words);
    char userinput;
    bool gameover = false;
    int length = 0;
    int index;
    int chances = 5;
    char *empty_list = (char *)malloc(sizeof(char) * 100);
    char letter;
    //-----------------------------------------------------------
    while (random_word[length] != '\0')
    {
        length++;
    }
    for (index = 0; index < length; index++)
    {
        empty_list[index] = '_';
    }
    empty_list[length] = '\0';

    printf("\n%s\n", empty_list);

    while (!gameover)
    {
        printf("Guess the word: ");
        scanf("%c*c", &userinput);
        for (index = 0; index < length; index++)
        {
            letter = random_word[index];
            if (letter == userinput)
            {
                empty_list[index] = letter;
                printf("\n%s\n", empty_list);
                Beep(720, 300);
            }
            if (userinput != random_word[index])
            {
                chances = chances - 1;
                printf("\nYou have %d chances left", chances);
            }
            if (empty_list[index] == '_' || chances == 0)
            {
                gameover = false;
            }
            else
            {
                gameover = true;
            }
        }
    }
    if (gameover)
        printf("YOU WON ");
}

字符串

ubbxdtey

ubbxdtey1#

尝试你的代码,基本上在“for”循环测试中有一些问题,当减少机会计数器并确定游戏是否结束时。在终端中测试它提供了以下结果。

craig@Vera:~/C_Programs/Console/Hangman/bin/Release$ ./Hangman 

_____
Guess the word: d

You have 4 chances left
You have 3 chances left
You have 2 chances left
You have 1 chances left
You have 0 chances leftGuess the word: d

You have -1 chances left
You have -2 chances left
You have -3 chances left
You have -4 chances left
You have -5 chances leftGuess the word: 
You have -6 chances left
You have -7 chances left
You have -8 chances left
You have -9 chances left
You have -10 chances leftGuess the word: d

- - -

You have -57 chances left
You have -58 chances left
You have -59 chances left
You have -60 chances left
You have -61 chances leftGuess the word: 
You have -62 chances left
You have -63 chances left
You have -64 chances left
You have -65 chances left
hello
YOU WON craig@Vera:~/C_Programs/Console/Hangman/bin/Release$

字符串
如图所示,计数器被允许变为负数,程序将允许输入字母,直到找到该单词,然后,该人被确定为获胜。
两个主要的问题是,计数器可以递减每次字符猜测是错误的,“游戏结束”标志被设置为“假”,直到所有正确的字母被猜测。

if (empty_list[index] == '_' || chances == 0)


下面是你的代码的重构版本。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdbool.h>

#define ARRAY_SIZE sizeof(words) / sizeof(words[0])

//------------------------------------------------------------
int main()
{
    srand(time(NULL));
    char *words[] = {"hello", "superman", "dart", "flask"};
    int x = rand() % ARRAY_SIZE;                /* Using standard rand() function       */
    char random_word[20];
    strcpy(random_word, words[x]);              /* Use standard string copy function    */
    char userinput;
    bool gameover = false, found, underscore;
    int length = 0;
    int index;
    int chances = 5;
    char empty_list[100];
    char letter;

    //-----------------------------------------------------------

    length = strlen(random_word);               /* Use standard string length function  */

    for (index = 0; index < length; index++)
    {
        empty_list[index] = '_';
    }

    empty_list[length] = '\0';

    printf("%s\n", empty_list);

    while (!gameover)
    {
        printf("Guess the word: ");
        scanf(" %c", &userinput);
        found = false;
        underscore = false;
        for (index = 0; index < length; index++)
        {
            letter = random_word[index];
            if (letter == userinput)
            {
                empty_list[index] = letter;
                printf("Beep..:\n");            /* Test run in a console    */
                found = true;
                //Beep(720, 300);               /* Test run in a console    */
            }

        }

        printf("Current: %s\n", empty_list);    /* Test run in a console    */

        if (found == false)
        {
            chances--;                                      /* Decrement the chance counter */
            printf("You have %d chances left\n", chances);
        }

        for (index = 0; index < length; index++)
        {
            if ((empty_list[index] == '_' && chances == 0)) /* Game is over if there are still underscores and the chance count is zero */
            {
                gameover = true;
                break;
            }
        }

        for (index = 0; index < length; index++)            /* Check if any underscores remain in the empty list    */
            if (empty_list[index] == '_')
            {
                underscore = true;
                break;
            }

        if (!underscore)                                    /* If no more underscore characters are in the empty list - the game is won */
        {
            gameover = true;
            break;
        }
    }
    if (gameover && (chances > 0))
        printf("YOU WON\n");
    else
        printf("Sorry, you lost.  The word was %s\n", random_word);
}


我很抱歉包含了完整的代码,因为重构后的代码分散在整个程序中。

  • 首先,由于我是在我的Linux虚拟机上测试这段代码的,所以删除了所有与Windows相关的引用。
  • 另外,包括“stdlib. h”文件允许使用标准随机数函数(rand())。
  • 为了使用常用的字符串功能,包含了“string.h”引用。
  • 正如重构代码中添加的注解所指出的,标准字符串函数用于复制随机单词,并确定各种“for”循环处理的字符串长度。
  • 添加了额外的布尔变量来确定是否以及何时所有下划线都被正确猜测的字符替换,以及是否在随机单词中找到了猜测的字符。
  • “if”测试块被重新定位在初始“for”循环之外,用于确定找到的字符和/或递减机会计数。
  • 如果没有找到一个猜测的角色,并且机会计数已经递减到零,则“gameover”标志被设置为true,并且“while”循环被退出-玩家已经输了。
  • 此外,如果对“empty_list”数组的检查不再包含任何下划线字符,则“gameover”标志被设置为true,并退出“while”循环-玩家获胜。

在这些重构之后,我们对程序进行了几次测试运行。

craig@Vera:~/C_Programs/Console/Hangman/bin/Release$ ./Hangman 
_____
Guess the word: d
Current: _____
You have 4 chances left
Guess the word: d
Current: _____
You have 3 chances left
Guess the word: d
Current: _____
You have 2 chances left
Guess the word: d
Current: _____
You have 1 chances left
Guess the word: d
Current: _____
You have 0 chances left
Sorry, you lost.  The word was hello
craig@Vera:~/C_Programs/Console/Hangman/bin/Release$ ./Hangman 
____
Guess the word: d
Beep..:
Current: d___
Guess the word: a
Beep..:
Current: da__
Guess the word: r
Beep..:
Current: dar_
Guess the word: t
Beep..:
Current: dart
YOU WON


仅供参考,对于测试,程序只是打印单词“哔”,因为我没有访问该库和函数。
从中得到的启示是,要认识到可用于处理“string. h”字符数组的功能,并小心测试逻辑,特别是“and”or“or”logic(“&&”“||您可能希望深入研究一些额外的“C”文献,因为它与字符数组和布尔函数有关。

相关问题