Write functions to compute letter and word scores in the Scrabble board game.

Write functions to compute letter and word scores in the Scrabble board game.

Introduction

Scrabble is a board game in which players take turns spelling words for points. Each letter has a point value, word scores are made up of the sum of letter scores, and multipliers can be applied if a letter or word lands on particular spaces on the board (as shown in the figure below).

Different letters are worth different numbers of points in Scrabble. The distribution of these letters is given in this Wikipedia article; we will use the English letter distribution. You will need to write a Python function that, when given a letter, returns the number of points associated with that letter. For example, the letter A is worth 1 point, whereas the letter Z is worth 10.

Points are scored in Scrabble by playing words. The score of a word, absent any multipliers (described below), is the sum of its letters' points. For example, the word APPLE would score 9 points.

A standard Scrabble board

Multipliers

If a word crosses any of the coloured spaces on the game board, letter or word scores can be doubled or even tripled. For the purposes of this assignment, we will only consider word multipliers, which are:

Triple word score

Locations (0,0), (0,7), (0,14), (7,0), (7,7), (7,14), (14,0), (14,7), (14,14)

Double word score

Locations (1,1), (1, 13), (2,2), (2, 12), (3,3), (3, 11), (4,4), (4, 10),
(10,4), (10, 10), (11,3), (11, 11), (12,2), (12, 12), (13,1), (13, 13)

Thus, if the word APPLE is played from left to right at location (0,6), it wiill be worth 27 points (9 × 3) because one of its tiles is on a triple word score tile at (0, 7). As another example, if the word PYTHON is spelled from top to bottom at location (2,0), it should score 42 points (14 × 3) because one of its tiles is on a triple word score tile at (7,0).

example

Requirements

In this assignment, you must implement two Python functions, one to compute the score of a single letter and one to compute the score of a word.

Single letters

Use the points distribution referenced above to implement the following Python function:

def letter_score(letter):
    """
    This function should return the number of points scored by playing
    the letter `letter`.
    """

    pass     # replace this with your own work

For example, letter_score('A') should return 1 andi letter_score('Z') should return 10.

Words

Use the description above to complete the following Python function that calculates a word’s point value.

def word_score(word, row=None, col=None, direction=None):
    """
    Compute the points that will be scored if a player plays this word.

    Parameters:
      word           the word to be played
      row            which row the word will be played at (or None)
      col            which column the word will be played at (or None)
      direction      'LR' for left-to-right, 'TB' for top-to-bottom, or None
    """

    pass     # replace this with your own work

If any of row, col or direction are None, all of them should be; this means that you can ignore the word’s placement. For example, the function call word_score('APPLE') should evaluation to 9. If any of row, col or direction are non-None, however, they should all be. In this case, you should take them all into account and apply any appropriate multipliers. For example, word_score('APPLE', 0, 6, 'LR') should evaluation to 27 (as described above).

Completing the assignment

Implement the two functions above and submit your work in a file called scrabble.py. You may start by copying the stub implementations above into your Python file to help you get started. Then, follow our usual problem-solving approach (problem, pseudocode, Python) for each part of the assignment (letters, words, words with positions). I suggest that you work on one part at a time:

  1. First, implement letter_score()

  2. Next, implement word_score() while ignoring row, col and direction

  3. Finally, take row, col and direction into account within word_score()

As always, assignments in this course must be done individually. You can (and are encouraged to!) work together on exercises, but you must do the assignments yourself. If in doubt, come and talk with me.