scrabble solver
github ↗the project
i built this python scrabble solver for a school project. it searches a dictionary of 279,496 words and shows the five highest-scoring moves it finds. the tricky part was getting a move to fit the board in both directions: every new tile can change a word across and a word down.
while looking on google, i found andrew appel and guy jacobson's 1988 paper, the world's fastest scrabble program, and decided to try building a solver using its approach. i expected the paper to be difficult to read, so i was surprised by how much i understood. it introduced me to tries and recursive backtracking.
finding moves that fit
instead of checking the whole dictionary against every square, the solver starts around empty squares next to existing tiles, called anchors. on an empty board it uses the centre square. for each anchor, it considers possible starting squares to its left, limited by the board edge and the number of rack tiles available.
cross-checking was the part that fried my brain. a tile placed in a horizontal word can form a vertical word too. before searching, the program scans above and below each empty square and stores the letters that would make a valid crossing word. if there are no vertical neighbours, the set contains every letter. i had to be careful about the order of letters above and below the square; the scan above the square runs backwards, so i reverse those letters before checking the dictionary.
when a word ended up in the wrong place
one of the harder bugs was that the solver found words but put them in invalid positions. on an earlier version of the board above, it reported BOCAGE even though the placement overlapped existing letters. after an attempted fix, the debug output showed column numbers from -8 down to -13. python allows negative list indices, so an incorrect coordinate could point back into the board instead of immediately failing.
the search built part of a word before the anchor, but the position used to place the finished word didn't consistently account for where that part started. i changed the search to consider starting squares explicitly and keep the coordinates of newly placed tiles. it checks board boundaries while searching and swaps coordinates back when recording vertical moves. keeping the new tiles separate also helps the scoring code avoid reusing premiums under existing tiles.
building and scoring a word
i load the word list into a trie. each node has a python dictionary mapping letters to child nodes and a boolean marking whether a word ends there. during the search, if there is no child for the next letter, that branch stops. the paper also describes compressing a trie into a directed acyclic word graph, or dawg; i kept the trie and didn't implement the compression.
from a starting square, the solver works to the right through the trie. a letter placed on an empty square must be available on the rack and allowed by that square’s cross-check. the search removes the tile, explores the continuation, then puts it back before trying another letter. a blank can stand for a needed letter and scores zero. when it reaches a tile already on the board, it follows that letter without taking one from the rack. vertical moves use the same search on a transposed board.
the solver scores the main word and any crossing words, applies letter and word multipliers to new tiles, and adds 50 points for using all seven rack tiles. it ranks by immediate score; it doesn't evaluate the remaining rack or future turns.
checking the result
i checked positions in the interface, including selecting a suggestion and resetting the board and rack. the program validates existing board words and tile counts before solving. the screenshot uses a position based on OdKUq.png with two tiles removed so a full rack fits the tile distribution. selecting the top result placed seven highlighted tiles and cleared the rack; the two S tiles came from blanks and scored zero.
the benchmark times nine solves on fixed-seed random positions with zero, two, or four moves on the board, including blank-tile racks. on one run on this computer, the average was 0.1665 seconds, the median was 0.0780 seconds, and the slowest was 0.3688 seconds. the timer excludes dictionary loading, position generation, and the interface. this is a small speed measurement, not a comparison against the paper's timings or proof that every generated move is legal; the benchmark positions themselves are built with the same solver.