Introduction
Scrabble is a popular word game in which players form words on a game board using letter tiles. Each tile has a specific point value, and the objective of the game is to accumulate the highest score. In this tutorial, we will learn how to write a program in C++ that calculates the score of a given Scrabble word.
Objective
The goal of this program is to take a word as input from the user, calculate its Scrabble score based on letter values, and output the total score. The program will use a predefined point system for the letters of the alphabet as follows:
- A, E, I, O, U, L, N, S, T, R: 1 point
- D, G: 2 points
- B, C, M, P: 3 points
- F, H, V, W, Y: 4 points
- K: 5 points
- J, X: 8 points
- Q, Z: 10 points
C++ Code
#include
#include
#include
using namespace std;
// Function to calculate Scrabble score
int calculateScrabbleScore(const string& word) {
// Point system for each letter
unordered_map<char, int> letterPoints = {
{'A', 1}, {'E', 1}, {'I', 1}, {'O', 1}, {'U', 1}, {'L', 1}, {'N', 1}, {'S', 1}, {'T', 1}, {'R', 1},
{'D', 2}, {'G', 2},
{'B', 3}, {'C', 3}, {'M', 3}, {'P', 3},
{'F', 4}, {'H', 4}, {'V', 4}, {'W', 4}, {'Y', 4},
{'K', 5},
{'J', 8}, {'X', 8},
{'Q', 10}, {'Z', 10}
};
int score = 0;
// Convert word to uppercase for case-insensitive comparison
for (char ch : word) {
ch = toupper(ch); // Convert character to uppercase
if (letterPoints.find(ch) != letterPoints.end()) {
score += letterPoints[ch];
}
}
return score;
}
int main() {
string word;
// Get the word from the user
cout << "Enter a word to calculate its Scrabble score: "; cin >> word;
// Calculate and display the score
int score = calculateScrabbleScore(word);
cout << "The Scrabble score for the word '" << word << "' is: " << score << endl;
return 0;
}
Program Explanation
The program consists of the following parts:
- Header Files: We include
iostreamfor input/output operations,stringto handle string data types, andunordered_mapfor storing letter-point mappings. - calculateScrabbleScore Function: This function takes a word as input, converts each character to uppercase, and then looks up its score in the
unordered_mapwhere we store the letter-to-point mapping. The total score is then calculated and returned. - main Function: It asks the user to input a word, calls the
calculateScrabbleScorefunction, and then prints the result on the screen.
How to Run the Program
- Copy the provided C++ code into a text editor or IDE of your choice (such as Visual Studio, Code::Blocks, or online compilers like repl.it).
- Save the file with a
.cppextension (e.g.,scrabble_score.cpp). - Compile the program using a C++ compiler. For example, if you’re using a command-line interface, type
g++ scrabble_score.cpp -o scrabble_score. - Run the compiled program by typing
./scrabble_score(on Linux/Mac) orscrabble_score.exe(on Windows). - Enter a word when prompted, and the program will display its Scrabble score.

