CS50 拼字游戏 |代码仅在 "QUESTION!" 个字处出错

CS50 Scrabble | Code goes wrong only with "QUESTION!" word

编程小伙伴。希望大家都没事

我正在做 CS50 的拼字游戏练习,程序运行良好。

但是...当您键入“问题!”时或“问题?”作为第一个单词,它无法再将“Q”识别为字母,因此将其标为零。

我是不是做错了什么?

我试图在从单词到点的字母分配后立即打印输出,似乎那里有问题,但我不知道是什么。

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

// Points assigned to each letter of the alphabet

int POINTS[] = {1, 1, 3, 3, 3, 3, 2, 2, 1, 1, 4, 4, 2, 2, 4, 4, 1, 1, 8, 8, 5, 5, 1, 1, 3, 3, 1, 1, 1, 1, 3, 3, 10, 10, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 4, 8, 8, 4, 4, 10, 10, 0};
char LETTERS[] = {'A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g', 'H', 'h', 'I', 'i', 'J', 'j', 'K', 'k', 'L', 'l', 'M', 'm', 'N', 'n', 'O', 'o', 'P', 'p', 'Q', 'q', 'R', 'r', 'S', 's', 'T', 't', 'U', 'u', 'V', 'v', 'W', 'w', 'X', 'x', 'Y', 'y', 'Z', 'z', '[=11=]'};

int compute_score(string word);

int main(void)
{
    int lcounter = 0;
    int counter;
    int index1;
    int index2;
    
    // Get input words from both players
    
    string word1 = get_string("Player 1: ");
    string word2 = get_string("Player 2: ");
    
    // FIND LENGTH OF WORD STRINGS
    
    int length_word1 = strlen(word1);
    int length_word2 = strlen(word2);

    // FIND CHARS OF STRINGS IN ARRAY AND ASSIGN VALUE
    
    string cword1 = word1;
    string cword2 = word2;
    
    int word1_counter = length_word1 - 1;
    int word2_counter = length_word2 - 1;
    int w1index[word1_counter];
    int w2index[word2_counter];
    
    for (counter=0; counter < 53; counter ++)
    {
        if (cword1[word1_counter] == LETTERS[counter])
        {
            w1index[word1_counter] = counter;
            word1_counter--;
            counter = -1;
        }
        else if (word1_counter < 0)
        {
            counter = 54;
        }
        else if (counter == 52)
        {
            w1index[word1_counter] = 52;
            word1_counter--;
            counter = -1;
        }
    }
    
    for (counter=0; counter < 53; counter ++)
    {
        if (cword2[word2_counter] == LETTERS[counter])
        {
            w2index[word2_counter] = counter;
            word2_counter--;
            counter = -1;
        }
        else if (word2_counter < 0)
        {
            counter = 54;
        }
        else if (counter == 52)
        {
            w2index[word2_counter] = 52;
            word2_counter--;
            counter = -1;
        }        
    }
    
    // Score both words
    int score1;
    int score2;
    
    for (counter = 0; counter < length_word1; counter ++)
    {
        score1 = score1 + POINTS[w1index[counter]];
    }
    for (counter = 0; counter < length_word2; counter ++)
    {
        score2 = score2 + POINTS[w2index[counter]];
    }
    // TODO: Print the winner
    if (score1 > score2)
    {
        printf("Player 1 wins!");
    }
    else if (score1 < score2)
    {
        printf("Player 2 wins!");
    }
    else if (score1 == score2)
    {
        printf("Tie!");
    }
}

好的,我想我理解了你的算法。

有两个错误。第一个在这里:

int word1_counter = length_word1 - 1;
int word2_counter = length_word2 - 1;
int w1index[word1_counter];
int w2index[word2_counter];

您没有为预期的尺寸预留足够的 space。 因此,第二个循环(填充 w2index 的循环)实际上会在填充 w2index 中的第一个成员时覆盖 w1index 中的最后一个成员。 将这些行更改为:

int word1_counter = length_word1 - 1;
int word2_counter = length_word2 - 1;
int w1index[word1_counter + 1];
int w2index[word2_counter + 1];

另一个错误在这里:

// Score both words
int score1;
int score2;

变量没有初始化。更改为:

// Score both words
int score1 = 0;
int score2 = 0;

不使用变量 lcounterindex1index2。删除它们。

名称 word*_counter 不是很好,因为您实际上将它们用作索引...您可以使用 break 退出循环并打破 down/simplify 代码更多的。但我看到你还在开始,所以很容易过于复杂......不过没关系。这需要时间。美妙之处在于,随着经验的增加,我们总是倾向于让这些事情变得更简单!