Skip to content

Scrabble score

Intro

Letter Values

You'll need these:

1
2
3
4
5
6
7
8
Letter                           Value
A, E, I, O, U, L, N, R, S, T       1
D, G                               2
B, C, M, P                         3
F, H, V, W, Y                      4
K                                  5
J, X                               8
Q, Z                               10

Examples

"cabbage" should be scored as worth 14 points:

  • 3 points for C
  • 1 point for A, twice
  • 3 points for B, twice
  • 2 points for G
  • 1 point for E

And to total:

  • 3 + 2*1 + 2*3 + 2 + 1
  • = 3 + 2 + 6 + 3
  • = 5 + 9
  • = 14

Task

Given a word, compute the Scrabble score for that word.

The Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    #include "scrabble_score.h"
    #include "algorithm"
    #include "list"
    #include <cctype>
    #include <list>
    #include <string>

    namespace scrabble_score {
    int score(std::string test) {
    int nilai{};
    std::string group[] = {{"AEIOULNRST"}, {"DG"}, {"BCMP"}, {"FHVWY"},
                            {"K"},          {"JX"}, {"QZ"}};
    std::transform(test.begin(), test.end(), test.begin(),
                    [](unsigned char c) { return std::toupper(c); });
    std::string word{};
    for (auto c : test) {
        if (std::isalpha(c))
        word.push_back(c);
    }
    for (auto s : word) {
        group[0].find(s) != std::string::npos ? nilai += 1 : 0;
        group[1].find(s) != std::string::npos ? nilai += 2 : 0;
        group[2].find(s) != std::string::npos ? nilai += 3 : 0;
        group[3].find(s) != std::string::npos ? nilai += 4 : 0;
        group[4].find(s) != std::string::npos ? nilai += 5 : 0;
        group[5].find(s) != std::string::npos ? nilai += 8 : 0;
        group[6].find(s) != std::string::npos ? nilai += 10 : 0;
    }

    return nilai;
    }
    } // namespace scrabble_score 
1
2
3
4
5
6
7
8
#if !defined(SCRABBLE_SCORE_H)
#define SCRABBLE_SCORE_H
#include "string"
namespace scrabble_score {
    int score(std::string);
}  // namespace scrabble_score

#endif // SCRABBLE_SCORE_H 

Last update: February 13, 2021

Comments