Primitive realisation of well-known game Scrabble as a problem set of the course "MITx: 6.00.1x Introduction to Computer Science and Programming Using Python" at edx.org.
Compatibility: Python 3.4+.
The game is interactive and intended to be lauched in a console. It reads a set of words from the included words.txt file.
A user has to compose words with letters in current hand (set of letters) to move forward.
The final function playGame() has been implemented via helper functions:
— def updateHand(hand, word): """ Assumes that 'hand' has all the letters in word. In other words, this assumes that however many times a letter appears in 'word', 'hand' has at least as many of that letter in it.
Updates the hand: uses up the letters in the given word
and returns the new hand, without those letters in it.
Has no side effects: does not modify hand.
word: string
hand: dictionary (string -> int)
returns: dictionary (string -> int)
"""
— def isValidWord(word, hand, wordList): """ Returns True if word is in the wordList and is entirely composed of letters in the hand. Otherwise, returns False.
Does not mutate hand or wordList.
word: string
hand: dictionary (string -> int)
wordList: list of lowercase strings
"""
— def calculateHandlen(hand): """ Returns the length (number of letters) in the current hand.
hand: dictionary (string-> int)
returns: integer
"""
— def playHand(hand, wordList, n): """ Allows the user to play the given hand, as follows:
* The hand is displayed.
* The user may input a word or a single period (the string ".")
to indicate they're done playing
* Invalid words are rejected, and a message is displayed asking
the user to choose another word until they enter a valid word or "."
* When a valid word is entered, it uses up letters from the hand.
* After every valid word: the score for that word is displayed,
the remaining letters in the hand are displayed, and the user
is asked to input another word.
* The sum of the word scores is displayed when the hand finishes.
* The hand finishes when there are no more unused letters or the user
inputs a "."
hand: dictionary (string -> int)
wordList: list of lowercase strings
n: integer (HAND_SIZE; i.e., hand size required for additional points)
"""