Monday, December 12, 2011

Program to Calculate the Scrabble Score of a Word

Problem:
In most word games, each letter in a word is scored according to its point value, which is inversely proportional to its frequency in English words. In ScrabbleTM, the points are allocated as follows:

For example, the Scrabble word "FARM" is worth 9 points: 4 for the F, 1 each for the A and the R, and 3 for the M. Write a ConsoleProgram that reads in words and prints out their score in Scrabble, not counting any of the other bonuses that occur in the game. You should ignore any characters other than uppercase letters in computing the score. In particular, lowercase letters are assumed to represent blank tiles, which can stand for any letter but which have a score of 0. (Robers ch 9, problem 5)

What it looks like:


/* File: ScrabbleCalc
 * -----------------
 * This program takes in an arbitrary word from the end-user and calculates its value according to the points alloted
 * to each letter by the game Scrabble. Characters must be upper-case; lowercase letters are ignored.
 * 
 * The program makes use of the private method 'scabbleScore' that takes in the input string and iterates through
 * each charcter. The method also establishes the integer 'score,' an instance variable that keeps track of the
 * accumulating points for the word. In each loop, it asses character via a switch statement and adds the appropriate 
 * points value for the character to 'score.'
 */
import acm.program.*;
public class ScrabbleCalc extends ConsoleProgram {
    public void run() {
       String word = readLine("Enter Scrabble word (all in uppercase) and we'll calculate your score: ");
       print(scrabbleScore(word));
    }
    private int scrabbleScore(String scrabbleWord) {
        int score = 0;
        for (int i = 0; i < scrabbleWord.length(); i++){
            char calculatedLetter = scrabbleWord.charAt(i);
            switch (calculatedLetter) {
                case 'A':
                case 'E':
                case 'I':
                case 'L':
                case 'N':
                case 'O':
                case 'R':
                case 'S':
                case 'T':
                case 'U': //Jesus this is fugly
                    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;
    }
}

What made it tricky:

Whenever I tell other programmers I'm learning programming with Java, they express much exasperation about the language, and I begin to see why. The statement that I had wanted to express was,  "If the letter at increment "i" is A, E, I, L, etc, then add 1 to the instance variable "score." I was looking for the switch statement to support an "or" statement (ie "case 'A' || 'E' || 'I' || 'L' ") but alas, the syntax isn't that neat. I had to list all the separate cases individually, though fortunately I didn't have to do the action block repeatedly for cases with identical points! I don't exactly know how you'd handle this in Python or Ruby but I expect it would be more succinct.
I think if building a Scrabble calculator in the "real world," you'd want to use a hash table  to easily turn the letter:points match into key:value pairs. We haven't gotten to hashes and arrays yet, so the tools for this problem were either a) switch statements or b) cascading "if" statements.

4 comments:

  1. Sweet, "if letter in" is supremely useful, geez. I feel like people who learned programming on Java and then discover Python must feel like teenagers who leave home at age 18 and find out that everyone else's parents let them have dessert before dinner.

    I guess "groups" is your hash then? I don't see when "letters" and "letter_score" are defined/initialized...maybe Python lets you do that eh? You do initialize word_score but not letter_score if I'm not mistaken.

    ReplyDelete
  2. This is great. Did you consider a dictionary?

    ReplyDelete
  3. I am regular reader, how are you everybody? This piece of writing posted at
    this web page is in fact fastidious.

    Here is my blog Psn Code Generator

    ReplyDelete