Introduction
Scrabble is a popular word game that assigns points based on letter tiles used in the words formed. The aim of this article is to help you understand how to calculate the score of a Scrabble word using Java programming language.
Objective
The objective of this program is to calculate the total score of a given Scrabble word based on the values of individual letters. In Scrabble, each letter has a specific point value. The program will accept a word as input and output the total score of that word by summing up the values of its individual letters.
Java Program to Calculate Scrabble Word Score
import java.util.HashMap; import java.util.Map; public class ScrabbleScoreCalculator { public static void main(String[] args) { // Create a map to hold letter values Map<Character, Integer> letterScores = new HashMap<>(); // Assign score values to each letter letterScores.put('A', 1); letterScores.put('B', 3); letterScores.put('C', 3); letterScores.put('D', 2); letterScores.put('E', 1); letterScores.put('F', 4); letterScores.put('G', 2); letterScores.put('H', 4); letterScores.put('I', 1); letterScores.put('J', 8); letterScores.put('K', 5); letterScores.put('L', 1); letterScores.put('M', 3); letterScores.put('N', 1); letterScores.put('O', 1); letterScores.put('P', 3); letterScores.put('Q', 10); letterScores.put('R', 1); letterScores.put('S', 1); letterScores.put('T', 1); letterScores.put('U', 1); letterScores.put('V', 4); letterScores.put('W', 4); letterScores.put('X', 8); letterScores.put('Y', 4); letterScores.put('Z', 10); // Example Scrabble word String word = "JAVA"; int score = calculateScore(word.toUpperCase(), letterScores); System.out.println("The score of the word \"" + word + "\" is: " + score); } // Method to calculate the total score of a word public static int calculateScore(String word, Map<Character, Integer> letterScores) { int totalScore = 0; // Loop through each letter in the word and add the score to totalScore for (int i = 0; i < word.length(); i++) { char letter = word.charAt(i); if (letterScores.containsKey(letter)) { totalScore += letterScores.get(letter); } } return totalScore; } }
Explanation of Program Structure
This program is structured as follows:
- Letter Scores Map: A
HashMap
is used to map each letter in the alphabet to its corresponding Scrabble score. - Input Word: The word to calculate the score for is hardcoded in the program (you can modify this to accept user input). In this case, the word “JAVA” is used as an example.
- calculateScore Method: This method takes the word and the map of letter scores as inputs, loops through the word’s characters, and sums their corresponding scores. The result is then returned.
- Output: The program prints the score of the word to the console.
How to Run the Program
To run this program on your system, follow these steps:
- Make sure you have the Java Development Kit (JDK) installed on your machine. If not, download and install it from the official Oracle website.
- Create a new file named
ScrabbleScoreCalculator.java
and paste the above code into it. - Open a terminal or command prompt and navigate to the directory where the file is saved.
- Compile the program by running the following command:
javac ScrabbleScoreCalculator.java
- Run the compiled program using this command:
java ScrabbleScoreCalculator
- The program will output the score of the Scrabble word.