This challenge is broken down into two parts:
- Write a script to calculate the highest scrabble score form a list of words in a text file.
- Extend this code to build up a simple Scrabble-like game on the command line.
Write a script to read in a series of words from a text file and output the word with the highest Scrabble score, along with the score value. The input text file has one word per line.
-
Core data values such as
LETTER_SCORES
are included in thedata.py
file, and should be imported into your code to make use of them. -
Make sure the script is executable over the command line and takes the name of the file as the first and only command line argument.
-
A file
dictionary.txt
is included to test upon.
As you write the code, think about how you may be able to breakdown the problem into a series of tasks, and then write a function for each task. e.g. include:
load_word_dictionary(filename)
- Given a file name and returns a list of wordscalc_word_value(word)
- Given a word, return its scrabble scoremax_word_value(word_list)
- Given a list of words, return a tuple of (score, word) for the highest scoring word.
Using what we've coded, we now build a simple Scrabble-like game whereby the user is given a random set of 7 letters, and asked to build the most valuable word.
Users should interact with the script via the command line, with an example session running something like:
Letters drawn: G, A, R, Y, T, E, V
Form a valid word: gary << user input
Word chosen: GARY (value: 8)
Optimal word possible: GARVEY (value: 13)
You scored: 61.5
The final score should be based on:
(score_achieved / maximum_possible) * 100
The final program should perform the following steps:
-
Load in any necessary data structures either previously created or stored in
data.py
-
Draw 7 random letters from the
POUCH
variable. This contains a list of letters with frequencies equal to those in scrabble (increased frequencies of vowels etc.). -
Ask the player to form a word with one or more of the 7 letters of the draw. Hint: Look up the
input()
function -
Validate input for:
- all letters of word are in those that are drawn;
- word is in the given dictionary of allowed words.
-
Calculate the given word value and show it to the player.
-
Calculate the optimal word (= max word value) by checking all permutations of the 7 letters of the draw, cross-checking with the set of valid words. Hint: looking up elements one at a time in a list is going to be slow if the list is large! Consider using one of Pythons other data structures to represent the valid words.
-
Show the player what the optimal word and its value is.
-
Calculate the players score, then display it.