[CS50-SCRABBLE]将代表每个字母表的数字数组与每个字母的分数数组链接起来,类似于拼字游戏

问题描述 投票:0回答:2
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};
string word = get_string("Player 1: ");
int score[26];
int sum=0;
int i=0;
while (word[i]!='\0')//string to alphanum
{
    if (isupper(word[i]))
    {
        word[i]=(int)word[i];
        score[i]=word[i]-65;

    }
    else if (islower(word[i]))
    {
        word[i]=(int)word[i];
        score[i]=word[i]-97;

    }



    printf("%d\t",score[i]);

    i++;
}
printf("\n");
for (int j=0;score[j]!='\0';j++)
{
    score[j]=POINTS[score[j]];
    printf("%d \t",score[j]);
}

该代码适用于除 A 之外的所有值,由于某种原因它停止工作, 我首先将输入字符数组(word[])转换为整数数组,然后尝试将分数数组(代表字母位置==> A-0,B-1,C-2,..,Z-25)链接到POINTS 数组,表示每个字母的拼字游戏值 (Z-10,A-1)

enter image description here

c cs50
2个回答
0
投票

一个问题是,对符号表值(如

'A' + 1 == 'B'
等)进行算术运算实际上没有明确定义或保证可以工作,除了符号
'0'
'9'
之外,其中这样的保证 is 由 C 做出标准。

这意味着您不能有一个包含26个分数的查找分数表,并根据字符值减去

'A'
(或65)来计算该表中的索引。如果 CS-50 另有说法,那就不好了。

不过幸运的是,如今主流计算机拥有大量内存,因此我们可以创建一个像这样的查找表:

const unsigned char letter_score [256] =
{
  ['A'] = 1, ['a'] = 1,
  ['B'] = 3, ['b'] = 3,
  ...
};

这占用了 256 个字节而不是 26 个字节,但这没什么大不了的。我们通过所谓的“指定初始化器”(

['A'] = 
)设置表的每个索引 - 这是创建自记录代码的简单方法。数组初始化中留下的项目保证设置为零,在这种情况下很好。

正如您所注意到的,如果我们将

isupper
toupper
设置为相同的值,这也消除了
'A'
/
'a'
等运行时检查。这是为了提高执行速度而进行的优化,但以 ROM 内存为代价。

用途:

size_t length = strlen(word);
int score = 0;
for(size_t i=0; i<length; i++)
{
  score += letter_score[ word[i] ];
}

如您所见,

score
不需要是一个数组,因为我们实际上不需要知道/保留字母数字n给出x点的信息。我们只想知道总分。


0
投票

感谢所有的回复,我让它工作了,你建议的和我改变的事情是删除分数数组并计算总和,修复了代码并摆脱了我的所有困惑,它也代码相当紧凑

int compute_score(string word)
{
    int sum=0;
    int i=0;
    while (word[i]!='\0')//string to alphanum
    {
        if (isupper (word[i]))
        {
            word[i]=(int)word[i];
            sum+=POINTS[word[i]-65];
        }
        else if (islower (word[i]))
        {
            word[i]=(int)word[i];
            sum+=POINTS[word[i]-97];
        }
        i++;
    }
    return sum;
}

这是最终的代码

如果有人想要完整的代码,请剧透那些试图自己解决 cs50 的人

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

// 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)
{
    // Get input words from both players
    string word1 = get_string("Player 1: ");
    string word2 = get_string("Player 2: ");

    // Score both words
    int score1 = compute_score(word1);
    int score2 = compute_score(word2);

    // TODO: Print the winner
    if (score1>score2)
    {
        printf("Player 1 Wins! by %d points\n",score1);
    }
    else if (score1<score2)
    {
        printf("Player 2 Wins! by %d points\n",score2);
    }
    else
    {
        printf("Tie!\n");
    }
}

int compute_score(string word)
{
    int sum=0;
    int i=0;
    while (word[i]!='\0')//string to alphanum
    {
        if (isupper (word[i]))
        {
            word[i]=(int)word[i];
            sum+=POINTS[word[i]-65];
        }
        else if (islower (word[i]))
        {
            word[i]=(int)word[i];
            sum+=POINTS[word[i]-97];
        }
        i++;
    }
    return sum;
}
© www.soinside.com 2019 - 2024. All rights reserved.