拼字点计算器

发布于 2025-01-19 04:16:06 字数 2208 浏览 2 评论 0原文

我正在解决一个问题,要求根据拼字游戏棋盘游戏确定哪个单词具有更高的分值。我的思考过程是:

  1. 在字符串中获取用户输入(即两个单词)
  2. 将用户输入转换为小写
  3. 调用函数,计算每个输入的分数
  4. 输出哪个单词具有更高的分值

这是我到目前为止所得到的:

// Points assigned to each letter of the alphabet
int POINTS[] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};

int compute_score(string word);

int main(void)
{
    string word1 = get_string("Player 1: ");
    string word2 = get_string("Player 2: ");

    //convert each word inputs to lowercase using 'tolower()' function
    for (int m=0; m<strlen(word1); m++)
    {
        word1[m]=tolower(word1[m]);
    }
    for (int n=0; n<strlen(word2); n++)
    {
        word2[n]=tolower(word2[n]);
    }
    // Score both words
    int score1 = compute_score(word1);
    int score2 = compute_score(word2);
    if (score1<score2)
    {
        printf("Player2 wins!");
        printf("\n");
    }
    if (score2<score1)
    {
        printf("Player1 wins!");
        printf("\n");
    }
    if (score1==score2)
    {
        printf("Tie!");
        printf("\n");
    }
    printf("this is the score for word 1: ""%i\n", score1);
    printf("this is the score for word 2: ""%i\n", score2);
}
int compute_score(string word) //uppercase letters are converted in the main();
{
    int wordScore=0;
    //the loop below converts each char in 'word' into int values according to ASCII. Then, it converts each int values mod 97
    for(int i=0; i<=strlen(word); i++)
    {
        word[i]=((int)word[i]);
        word[i]=word[i]%97;
        printf("each letter is now converted to this int value: ""%i\n",word[i]);
    }
    for(int j=0; j<=strlen(word); j++)
    {
        for (int k=0; k<26; k++)
        {
            if(word[j]==k)
            {
                word[j]=POINTS[k];
            }
        }  
    }
    for(int l=0; l<strlen(word); l++)
    {
        if(word[l]<33)//word[l]<33 since special characters will have values from 33~47;
        {
            wordScore+=word[l];
        }
    }
    return wordScore;
}

出于某种原因,一些字母,例如 b,无法正确计算。错误是什么?对于我的一生,我无法弄清楚为什么任何带有 b 的单词都会将字符 b 计算为 2 点。

I am working on a problem that is asking to determine which word has a higher point value according to the Scrabble board game. My thought process is to:

  1. take user inputs (i.e. two words) in strings
  2. convert user inputs to lowercase
  3. call functions that calculate scores for each input
  4. Output which word has a higher point value

Here is what I have so far:

// Points assigned to each letter of the alphabet
int POINTS[] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};

int compute_score(string word);

int main(void)
{
    string word1 = get_string("Player 1: ");
    string word2 = get_string("Player 2: ");

    //convert each word inputs to lowercase using 'tolower()' function
    for (int m=0; m<strlen(word1); m++)
    {
        word1[m]=tolower(word1[m]);
    }
    for (int n=0; n<strlen(word2); n++)
    {
        word2[n]=tolower(word2[n]);
    }
    // Score both words
    int score1 = compute_score(word1);
    int score2 = compute_score(word2);
    if (score1<score2)
    {
        printf("Player2 wins!");
        printf("\n");
    }
    if (score2<score1)
    {
        printf("Player1 wins!");
        printf("\n");
    }
    if (score1==score2)
    {
        printf("Tie!");
        printf("\n");
    }
    printf("this is the score for word 1: ""%i\n", score1);
    printf("this is the score for word 2: ""%i\n", score2);
}
int compute_score(string word) //uppercase letters are converted in the main();
{
    int wordScore=0;
    //the loop below converts each char in 'word' into int values according to ASCII. Then, it converts each int values mod 97
    for(int i=0; i<=strlen(word); i++)
    {
        word[i]=((int)word[i]);
        word[i]=word[i]%97;
        printf("each letter is now converted to this int value: ""%i\n",word[i]);
    }
    for(int j=0; j<=strlen(word); j++)
    {
        for (int k=0; k<26; k++)
        {
            if(word[j]==k)
            {
                word[j]=POINTS[k];
            }
        }  
    }
    for(int l=0; l<strlen(word); l++)
    {
        if(word[l]<33)//word[l]<33 since special characters will have values from 33~47;
        {
            wordScore+=word[l];
        }
    }
    return wordScore;
}

For some reason, some letters, such as b, do not get calculated correctly. What is the error? For the life of me, I can't figure out why any words with b calculate the character b as 2 points.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看透却不说透 2025-01-26 04:16:06

您的计算word [i] = Word [i]%97是伪造的。这忽略了字符编码,并通过Modulo算术创建0到96之间的值。将会发生的奇怪事情之一是字母'a'将过早终止您的字符串,因为'a'%97为零。这意味着您不能依靠strlen

您应该避免修改字符串本身。这样做是没有意义的。计算分数非常简单:

int compute_score(string word)
{
    int wordScore = 0;
    for(int j = 0, len = strlen(word); j < len; j++)
    {
        if (isalpha(word[j])) {
           wordScore += POINTS[tolower(word[j]) - 'a'];
        } 
    }
    return wordScore;
}

这也将较低的分数滚入分数计算中,因此无需将字符串预先转换为小写。

说到这一点,这可能是一个很好的时机,指出您正在使用循环错误。在字符串长度上迭代时,您会继续使用&lt; =。这是不正确的,除非您也想处理字符串的NUL终结器。

关于使用硬编码的整数作为角色值,请停止这样做。改用字符文字(请注意我如何使用文字'a'而不是97)。了解ASCII表和硬编码整数常数并不聪明 - 所有的事情就是使代码不那么便宜且易于阅读。因此,请停止这样做,并将其视为公共服务。

Your calculation word[i] = word[i] % 97 is quite bogus. This ignores the character encoding, and creates a value between 0 and 96 via modulo arithmetic. One of the weird things that will happen is the letter 'a' will prematurely terminate your string, because 'a' % 97 is zero. That means you cannot rely on strlen afterwards.

You should avoid modifying the string itself. It makes no sense to do so. It's quite simple to compute the score as follows:

int compute_score(string word)
{
    int wordScore = 0;
    for(int j = 0, len = strlen(word); j < len; j++)
    {
        if (isalpha(word[j])) {
           wordScore += POINTS[tolower(word[j]) - 'a'];
        } 
    }
    return wordScore;
}

This also rolls the lowercasing into the score calculation so there is no need to pre-convert your strings to lowercase.

Speaking of that, this is probably a good time to point out that you're using loops incorrectly. You keep using <= when iterating over a string length. That is incorrect, unless you're also wanting to process the string's NUL terminator.

Regarding using hard-coded integers for character values, please stop doing that. Use character literals instead (notice how I use the literal 'a' instead of 97 ). There's nothing clever about knowing the ASCII table and hardcoding integer constants -- all that does is make the code less portable and less readable. So please stop doing that and consider it a public service.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文