Peter Norvig

29 December 2015

Refactoring a Crossword Game Program¶

In my CS 212 class on Udacity, the most complex lesson involved a crossword game program (for games such as Scrabble® and Words with Friends®). The program was developed incrementally. First I asked "what words can be made with a rack of seven letters?", then I asked "how can you place words onto a single row?", and finally, I, with the help of the students, developed a program to find the highest scoring play anywhere on the board. This approach made for a good sequence of exercises, each building on the previous one. But the code ended up being overly complicated—it accumlated technical debt—because it kept around ideas from each iteration.

In this notebook I will refactor the program to pay off the debt.

Our program uses these concepts:

Dictionary : A set of all legal words.

: A set of all legal words. Word : a string of letters. Words in the dictionary are all uppercase.

: a string of letters. Words in the dictionary are all uppercase. Tile : a letter (or a blank) that can be played on the board to form words.

: a letter (or a blank) that can be played on the board to form words. Blank : a tile with no letter on it; the player who places it on the board gets to choose which letter it will represent.

: a tile with no letter on it; the player who places it on the board gets to choose which letter it will represent. Rack : a collection of up to seven tiles that a player may use to make words.

: a collection of up to seven tiles that a player may use to make words. Board : a grid of squares onto which players play tiles to make words.

: a grid of squares onto which players play tiles to make words. Square : a location on the board; a square can hold one tile. (The variable s will stand for a square number, and sq for the contents of a square.)

: a location on the board; a square can hold one tile. (The variable will stand for a square number, and for the contents of a square.) Bonus : some squares give you bonus scores: double or triple letter or word scores.

: some squares give you bonus scores: double or triple letter or word scores. Play : a play consists of placing some tiles on the board to form a continuous string of letters in one direction (across or down), such that only valid words are formed, and such that one of the letters is placed on an anchor square.

: a play consists of placing some tiles on the board to form a continuous string of letters in one direction (across or down), such that only valid words are formed, and such that one of the letters is placed on an anchor square. Anchor square : Every play must place a letter on an anchor square: either the center start square or a square that is adjacent to a tile previously played on the board.

: Every play must place a letter on an anchor square: either the center start square or a square that is adjacent to a tile previously played on the board. Direction: Every play must be in either the ACROSS or DOWN direction. (The variable dir stands for a direction.)

Every play must be in either the or direction. (The variable stands for a direction.) Cross word : a word formed in the other direction from a play. For example, a play forms a word in the across direction, and in doing so, places a letter that extends a word in the down direction. This new extended cross word must be in the dictionary.

: a word formed in the other direction from a play. For example, a play forms a word in the across direction, and in doing so, places a letter that extends a word in the down direction. This new extended cross word must be in the dictionary. Score : the points awarded for a play, consisting of the sum of the word scores for each word made (the main word and possibly any cross words), plus a bingo bonus if all seven letters are used.

: the points awarded for a play, consisting of the sum of the word scores for each word made (the main word and possibly any cross words), plus a bingo bonus if all seven letters are used. Word score : Each word scores the sum of the letter scores for each tile (either placed by the player or already on the board but part of the word) times the word bonus score. The word bonus score starts at 1, and is multiplied by 2 for each double word square and 3 for each triple word square covered by a tile on this play.

: Each word scores the sum of the letter scores for each tile (either placed by the player or already on the board but part of the word) times the word bonus score. The word bonus score starts at 1, and is multiplied by 2 for each double word square and 3 for each triple word square covered by a tile on this play. Letter score : The letter score is the value on the letter tile (for example, 1 for A and 10 for Q ) times the letter bonus score. The letter bonus is 2 when a tile is first placed on a double letter square (or the center star) and 3 when first placed on a triple letter square; it is 1 for a tile already on the board, or for a new tile played on a non-letter-bonus square. The letter score for a blank tile is always zero.

: The letter score is the value on the letter tile (for example, 1 for and 10 for ) times the letter bonus score. The letter bonus is 2 when a tile is first placed on a double letter square (or the center star) and 3 when first placed on a triple letter square; it is 1 for a tile already on the board, or for a new tile played on a non-letter-bonus square. The letter score for a blank tile is always zero. Bingo : a bonus gained by using all seven tiles in one play. In Words with Friends® the bingo bonus is 35; in Scrabble® it is 50.

: a bonus gained by using all seven tiles in one play. In Words with Friends® the bingo bonus is 35; in Scrabble® it is 50. Game : players take turns making plays until one player has no more tiles. After making a play, the player's rack is replenished with tiles until the player has 7 tiles or until the bag of tiles is empty.

: players take turns making plays until one player has no more tiles. After making a play, the player's rack is replenished with tiles until the player has 7 tiles or until the bag of tiles is empty. Prefix: a string of zero or more letters that starts some word in the dictionary. Not a concept that has to do with the rules of the game; it will be important in our algorithm that finds valid plays.

This notebook uses these imports: