刚开始编程,才几周.try 制作一个Wordle克隆听起来像是一个很好的练习,所以我今天早些时候开始了.顺便说一句,我也真的没有学到关于指针的东西.

在42号线遇到问题:

string result = check(guess[i], key) 

当我try 使用我的check函数时,CLI向我返回以下内容:

不兼容的指针类型正在使用类型为‘字符串*’(也称为‘char**’)的表达式初始化‘字符串’(又名‘char*’)

第二个问题是,我可以得到猜测中每个字母的支票,但它们存储在一个名为letter的数组中,我想将它们组合到一个字符串中,以表示:第一个字母:正确的点,第二个字母:不是在Word中...等等.不确定这是否可能.

我还试图从我的函数传回字母数组,但不确定它是如何工作的.但我想我能弄明白这一点.我也考虑过传回int分,由此我可以推断结果,但不确定哪种方法是最好的.

很抱歉用了呕吐这个词,但我真的想完成这件事,但我不知道go 哪里.

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

string word_list[] = { "blind", "sheet", "crush", "relax", "drain",
    "label", "expel", "thump", "shade", "resin", "alert", "dream",
    "flood", "guard", "adult", "force", "wound", "drain", "cable"
};
string key;

string *check(string guess, string key);

int main(void)
{
    char ready;
    do {
        ready = get_char("Welcome to wordle! Are you ready to play? (y/n) ");
    } while (ready != 110 && ready != 121);

    if (ready == 121) {
        int ran;
        srand(time(NULL));
        ran = rand() % 18;
        key = word_list[ran];
    } else {
        return 1;
    }

    int guess_num = 1;
    string guess[5];

    for (int i = 0; i < 5; i++)
        do {
            guess[i] = get_string("Guess %i:\n", guess_num);
            string result = check(guess[i], key);
            printf("%s\n", result);

            if (strlen(guess[i]) == 5) {
                guess_num++;
            }
        } while (strlen(guess[i]) != 5);
}

string *check(string guess, string keys)
{
    string correct = "in correct spot." ;
    string almost = "in the word, wrong spot.";
    string not = "not in word.";
    string letter[5];

    for (int i = 0; i < 5; i++)
        if (guess[i] == key[])
        {
            letter[i] = correct;
        }
        else if (guess[i] == key[0] || guess[i] == key[1] || guess[i] == key[2] || guess[i] == key[3] || guess[i] == key[4])
        {
            letter[i] = almost;
        }
        else
        {
            letter[i] = not;
        }
    return letter;
}

我try 了多个网站和教程,但没有真正理解,或者解决方案是针对类似的问题,但没有解决我自己的.

推荐答案

存在多种问题:

  • 测试if (guess[i] == key[])是语法错误.你应该写道:

      if (guess[i] == key[i])
    
  • 函数check返回局部数组:这是无效的.函数一返回,数组就无效,因此指向其第一个元素的指针不能返回给调用方. 相反,你应该把数组作为参数(在调用函数中定义),并在check()中更新它. 注意,letter应该是长度为6的char数组,而不是string(它只是char *).

  • 你应该使用字符常量而不是神秘的ASCII码ready != 110 && ready != 121:

     ready != 'n' && ready != 'y'
    
  • word_list数组中实际上有19个单词,不要使用18,而是一个计算结果为数组长度的表达式.

以下是修改后的版本:

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

string word_list[] = {
    "blind", "sheet", "crush", "relax", "drain",
    "label", "expel", "thump", "shade", "resin",
    "alert", "dream", "flood", "guard", "adult",
    "force", "wound", "drain", "cable"
};
string key;

void check(string guess, string key, char letter[]);

int main(void)
{
    char ready;
    do {
        ready = get_char("Welcome to wordle! Are you ready to play? (y/n) ");
    } while (ready != 'n' && ready != 'y');

    if (ready == 'y') {
        srand(time(NULL));
        int ran = rand() % (sizeof word_list / sizeof word_list[0]);
        key = word_list[ran];
    } else {
        return 1;
    }

    int guess_num = 1;
    string guess[5];
    char result[6];

    for (int i = 0; i < 5; i++) {
        do {
            guess[i] = get_string("Guess %i:\n", guess_num);
        } while (strlen(guess[i]) != 5);

        check(guess[i], key, result);
        printf("%s\n", result);
        guess_num++;
        if (!strcmp(guess[i], key)) {
            printf("found!\n");
            return 0;
        }
    }
    printf("not found! word was %s\n", key);
}

void check(string guess, string keys, char letter[6])
{
    char correct = 'O';
    char almost = 'X';
    char not = '.';

    for (int i = 0; i < 5; i++) {
        if (guess[i] == key[i]) {
            letter[i] = correct;
        } else
        if (guess[i] == key[0] || guess[i] == key[1] || guess[i] == key[2]
        ||  guess[i] == key[3] || guess[i] == key[4]) {
            letter[i] = almost;
        } else {
            letter[i] = not;
        }
    }
    letter[5] = '\0';  // set the null terminator
}

C++相关问答推荐

如何在C中通过转换为char * 来访问float的字节表示?

如何确保内存分配在地址附近?

设计处理各种数据类型的方法和数据 struct

你能用自己的地址声明一个C指针吗?

由Go调用E.C.引起的内存快速增长

这是一个合法的C Strdup函数吗?

将 struct 变量赋给自身(通过指针取消引用)是否定义了行为?

链接到底是如何工作的,我在这里到底做错了什么

在C中创建任意类型的只读指针参数

使用Open62541向OPCUA服务器发送读请求时内存泄漏

从BIOS(8086)中读取刻度需要多少?

不带Malloc的链表

当用C打印过多的';\n';时输出不正确

与外部SPI闪存通信时是否应禁用中断?

';malloc():损坏的顶部大小';分配超过20万整数后

x86-64平台上的int_fast8_t大小与int_fast16_t大小

共享内存未授予父进程权限

将char*铸造为空**

C循环条件内的函数

指向返回 struct 成员的指针,安全吗?