简体   繁体   English

如何计算单词的拼字游戏分数

[英]How to calculate Scrabble Score of a word

I'm trying to write a program to calculate the score of a word, based on the game Scrabble 我正在尝试根据游戏Scrabble编写一个程序来计算单词的分数 在此处输入图片说明

The scores are based off the image above. 分数基于上图。

I've currently coded a function, my ideal goal is to use this and get the user to input a word to calculate the score. 我目前已经编码了一个函数,我的理想目标是使用它并让用户输入一个单词来计算分数。

int scrabbleScore(String Word) {
        int score = 0;
        for (int i = 0; i < Word.length(); i++){
            char calculatedLetter = Word.at(i);
            switch (calculatedLetter) {
                case 'A':
                case 'E':
                case 'I':
                case 'L':
                case 'N':
                case 'O':
                case 'R':
                case 'S':
                case 'T':
                case 'U':
                    score +=1; break;
                case 'D':
                case 'G':
                    score +=2; break;
                case 'B':
                case 'C':
                case 'M':
                case 'P':
                    score +=3; break;
                case 'F':
                case 'H':
                case 'V':
                case 'W':
                case 'Y':
                    score +=4; break;
                case 'K':
                    score +=5; break;
                case 'J':
                case 'X':
                    score +=8; break;
                case 'Q':
                case 'Z':
                    score +=10; break;
                default: break;
            }
        }
        return score;

Why is this giving me a score of 0 for any word? 为什么这给我每个单词0分?

You could make it a bit shorter and prepare for one day multi-language with few modifications. 您可以将其缩短一点,并且只需很少的修改就可以准备一天的多语言版本。

int scrabbleScore(string Word)
{
    int score = 0;
    char EnglishScoreTable[26] = { 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 };
    for (auto Letter : Word)
    {
        if (Letter >= 'A' && Letter <= 'Z')
        {
            score += EnglishScoreTable[Letter - 'A'];
        }
        else
        {
            // error in input 
        }
    }
    return score;
}
std::string word;
std::cin>>word;
std::cout<<"Your Score :" << scrabbleScore(word) ;

Also, 也,

int scrabbleScore(String Word)
                  ^ this should be string from std namespace,

Change imp to this: 将imp更改为此:

private static int scrabbleScore(String Word) {
    int score = 0;
    String upperWord = Word.toUpperCase();
    for (int i = 0; i < upperWord.length(); i++){
        char calculatedLetter = upperWord.charAt(i);
        switch (calculatedLetter) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM