刚刚接触编码,才几周时间。尝试制作一个 Wordle 克隆听起来是一个很好的做法,所以我今天早些时候开始了。顺便说一句,我还真的没有学到任何关于指针的知识。
在第 42 行遇到问题:
string result = check(guess[i], key)
当我尝试使用
check
函数时,CLI 将以下内容返回给我:
不兼容的指针类型使用“string *”类型的表达式(又名“char **”)初始化“string”(又名“char *”)
问题二是我可以检查猜测的每个字母,但它们存储在一个名为
letter
的数组中,我想将它们组合在一个字符串中:第一个字母:正确的位置,第二个字母:没有在文字中...等等。不确定这是否可能。
还尝试从我的函数传回字母数组,但不确定它是如何工作的。但我想我能弄清楚这一点。我还考虑过传回
int
,从中我可以推断出结果,但不确定哪种方式是最好的。
抱歉用了“呕吐”这个词,但我确实想完成这个任务,但不知道该去哪里。
#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;
}
我尝试过多个网站和教程,但没有真正理解,或者解决方案是针对类似问题但不解决我自己的问题。
存在多个问题:
测试
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, string 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[]) {
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
}