CS50:措辞:Todo#7在函数外重用返回的变量

cgvd09ve  于 2023-08-03  发布在  其他
关注(0)|答案(1)|浏览(75)

TODO #7.打印游戏结果。我想重用变量score,但它声明它是未声明的,并且不确定如何不必在main函数中输入这部分。
当我运行下面的代码时,我得到以下错误:使用未声明的标识符'guess'
if(choice)
问题集的链接可获得更多信息:https://cs50.harvard.edu/x/2023/psets/2/wordle50/
我正在努力使用它的功能之外的变量得分。choice(由程序选择的单词,基于用户选择的单词大小,由用户猜测)和guess(用户对选择的单词的猜测)。在过去的几个星期里,我看了函数的短片和讲座片段大概15遍,贯穿了多个习题集,但仍然没有完全掌握它
如何在其功能之外重用猜测和选择?这就是为什么我最终把所有东西都放在main函数中。
如果有人能给我指出这方面的学习资源,我将不胜感激。
我已经尝试了下面的代码,并尝试重新初始化所有的变量,但似乎不可行,我可能不理解的概念。
我尝试尽量减少复制的代码数量,但这并没有给社区提供所需的信息,但我不能创建一个最小的可重复的例子,也许我将能够通过更多的实践来做到这一点,但这里是完整的代码,请不要生气。
这里的问题出现在以下引用行之后的8行中:
报价

// Print the game's result

// TODO #7

字符串
取消引用

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

// each of our text files contains 1000 words
#define LISTSIZE 1000

// values for colors and score (EXACT == right letter, right place; CLOSE == right letter, wrong place; WRONG == wrong letter)
#define EXACT 2
#define CLOSE 1
#define WRONG 0

// ANSI color codes for boxed in letters
#define GREEN   "\e[38;2;255;255;255;1m\e[48;2;106;170;100;1m"
#define YELLOW  "\e[38;2;255;255;255;1m\e[48;2;201;180;88;1m"
#define RED     "\e[38;2;255;255;255;1m\e[48;2;220;20;60;1m"
#define RESET   "\e[0;39m"

// user-defined function prototypes
string get_guess(int wordsize);
int check_word(string guess, int wordsize, int status[], string choice);
void print_word(string guess, int wordsize, int status[]);

int main(int argc, string argv[])
{
    // ensure proper usage
    // TODO #1
    if (argc != 2)
    {
        printf("Usage: ./wordle wordsize\n");
        return 1;
    }

    int wordsize = 0;
    wordsize = atoi(argv[1]);

    // ensure argv[1] is either 5, 6, 7, or 8 and store that value in wordsize instead
    // TODO #2
    if (wordsize < 4 || wordsize > 9)
    {
        printf("Error: wordsize must be either 5, 6, 7, or 8\n");
        return 1;
    }

    // open correct file, each file has exactly LISTSIZE words
    char wl_filename[6];
    sprintf(wl_filename, "%i.txt", wordsize);
    FILE *wordlist = fopen(wl_filename, "r");
    if (wordlist == NULL)
    {
        printf("Error opening file %s.\n", wl_filename);
        return 1;
    }

    // load word file into an array of size LISTSIZE
    char options[LISTSIZE][wordsize + 1];

    for (int i = 0; i < LISTSIZE; i++)
    {
        fscanf(wordlist, "%s", options[i]);
    }

    // pseudorandomly select a word for this game
    srand(time(NULL));
    string choice = options[rand() % LISTSIZE];

    // allow one more guess than the length of the word
    int guesses = wordsize + 1;
    bool won = false;

    // print greeting, using ANSI color codes to demonstrate
    printf(GREEN"This is WORDLE50"RESET"\n");
    printf("You have %i tries to guess the %i-letter word I'm thinking of\n", guesses, wordsize);

    // main game loop, one iteration for each guess
    for (int i = 0; i < guesses; i++)
    {
        // obtain user's guess
        string guess = get_guess(wordsize);

        // array to hold guess status, initially set to zero
        int status[wordsize];

        // set all elements of status array initially to 0, aka WRONG
        // TODO #4
        for(int status_index = 0 ; status_index < wordsize ; status_index++)
        {
            status[status_index] = 0;
        }

        // Calculate score for the guess
        int score = check_word(guess, wordsize, status, choice);

        printf("Guess %i: ", i + 1);

        // Print the guess
        print_word(guess, wordsize, status);

        // if they guessed it exactly right, set terminate loop
        if (score == EXACT * wordsize)
        {
            won = true;
            break;
        }

    }

    // Print the game's result
    // TODO #7

    if (guess == choice)
    {
        printf("You won!\n");
    }
    else
    {
        printf("choice: %s\n", choice);
    }

    // that's all folks!
    return 0;
}

string get_guess(int wordsize)
{
    string guess;
    int guess_int;
    // ensure users actually provide a guess that is the correct length
    // TODO #3
    do
    {
        guess = get_string("Input a %i-letter word: ", wordsize);
    }
    while ((guess_int = strlen(guess)) != wordsize);
    return guess;
}

int check_word(string guess, int wordsize, int status[], string choice)
{
    int score = 0;

    // compare guess to choice and score points as appropriate, storing points in status
    // TODO #5

    // HINTS
    // iterate over each letter of the guess
        for (int guess_index = 0 ; guess_index < wordsize ; guess_index++)
        {

        // iterate over each letter of the choice
            for (int choice_index = 0 ; choice_index < wordsize ; choice_index++)
            {

            // compare the current guess letter to the current choice letter
                // if they're the same position in the word, score EXACT points (green) and break so you don't compare that letter further
                if (guess[guess_index] == choice[guess_index])
                {
                    status[guess_index] = EXACT;
                    score = score + status[guess_index];
                    break;
                }
                // if it's in the word, but not the right spot, score CLOSE point (yellow)
                else if (guess[guess_index] == choice[choice_index])
                {
                    status[guess_index] = CLOSE;
                    score = score + status[guess_index];
                }
            }
        }
        // keep track of the total score by adding each individual letter's score from above

    return score;
}

void print_word(string guess, int wordsize, int status[])
{
    // print word character-for-character with correct color coding, then reset terminal font to normal
    // TODO #6
    for (int i = 0 ; i < wordsize ; i++)
    {
        if (status[i] == 2)
        {
            printf(GREEN"%c"RESET, guess[i]);
        }
        else if (status[i] == 1)
        {
            printf(YELLOW"%c"RESET, guess[i]);
        }
        else if (status[i] == 0)
        {
            printf(RED"%c"RESET, guess [i]);
        }
    }
    printf("\n");
    return;
}

tv6aics1

tv6aics11#

我也遇到了同样的问题。
因为我不能在todo 7中使用'score'或'guess',因为它们都是局部变量,所以我开始寻找全局变量。然后我找到了bool 'won',它在main中声明,并在main中的最后一个循环中获得最终结果。
下面是我为todo 7编写的最终代码:
// TODO #7

if (won)
{
    printf("You won!\n");
}
else
{
    printf("Sorry, the word is: %s\n", choice);
}
// that's all folks!

字符串

相关问题