Coe808 Lab 5: Python Scrabble - Ryerson University
COE808 Lab 5 (2010) February 23, 2010
Scrabble in Python
coe808 Lab 5: Python Scrabble
Objectives
Learn how to use python. Duration: two weeks.
Scrabble basics
Letters are dealt to players, who then construct one or more words out of their letters. Each valid word receives a score, based on the length of the word and the letters in that word. The rules of the game are as follows: Dealing A player is dealt a hand of n letters chosen at random (assume n=7 for now). The player arranges the hand into a set of words using each letter at most once. Some letters may remain unused (these won't be scored). Scoring The score for the hand is the sum of the score for the words. The score for a word is the sum of the points for letters in the word, plus 50 points if all n letters are used on the first go. Letters are scored as in Scrabble; A is worth 1, B is worth 3, C is worth 3, D is worth 2, E is worth 1, and so on. We have defined the dictionary: SCRABBLE_LETTER_VALUES that maps each lowercase letter to its Scrabble letter value. For example, 'weed' would be worth 8 points (4+1+1+2=8), as long as the hand actually has 1 'w', 2 'e's, and 1 'd'. As another example, if n=7 and you get 'waybill' on the first go, it would be worth 65 points (4+1+4+3+1+1+1=15, +50 for the 'bingo' bonus of using all seven letters).
Getting Started
1. Download and save ? game.py: the skeleton you'll fill in for Problems 1-5 game.py ? test_game.py: Unit tests for some of your code (more on this later) test_game.py ? words.txt: the list of valid words (all words acceptable in North American Scrabble up to 10 letters long) words.txt
Make sure to put all of the files in the same directory! 2. Run the code
Page 1 of 9
COE808 Lab 5 (2010) February 23, 2010
Scrabble in Python
Run game.py, without making any modifications to it, in order to ensure that everything is set up correctly. The code we have given you loads a list of valid words from a file and then calls the play_game function. You will implement the functions it needs in order to work.
If everything is okay, after a small delay, you should see the following printed out:
Loading word list from file... 83667 words loaded. play_game not implemented. play_hand not implemented.
If you see an IOError instead (e.g., No such file or directory), you should change the value of the
WORDLIST_FILENAME constant (defined near the top of the file) to the complete pathname for the file words.txt (This will vary based on where you saved the file).
Provided code
The file game.py has a number of already implemented functions you can use while writing up your solution. You can ignore the code between the following comments, though you should read and understand everything else:
# ----------------------------------# Helper code # (you don't need to understand this helper code) ... # (end of helper code) # -----------------------------------
Unit testing
This problem set is structured so that you will write a number of modular functions and then glue them together to form the complete word playing game. Instead of waiting until the entire game is ready, you should test each function you write, individually, before moving on. This approach is known as unit testing, and it will help you debug your code.
We have provided several test functions to get you started. As you make progress on the problem set, run test_game.py as you go.
If your code passes the unit tests you will see a SUCCESS message; otherwise you will see a FAILURE message. These tests aren't exhaustive. You may want to test your code in other ways too.
If you run test_game.py using the provided game.py skeleton, you should see that all the tests fail.
These are the provided test functions:
test_get_word_score()
Test the get_word_score() implementation.
test_update_hand()
Test the update_hand() implementation.
test_is_valid_word()
Test the is_valid_word() implementation.
Page 2 of 9
COE808 Lab 5 (2010) February 23, 2010
Scrabble in Python
I. Word scores
The first step is to implement some code that allows us to calculate the score for a single word.
Problem #1
The function get_word_score should accept a string of lowercase letters as input (a word) and return the integer score for that word, using the game's scoring rules.
Fill in the code for get_word_score in game.py:
def get_word_score(word, n): """ Returns the score for a word. Assumes the word is a valid word. The score for a word is the sum of the points for letters in the word, plus 50 points if all n letters are used on the first go. Letters are scored as in Scrabble; A is worth 1, B is worth 3, C is worth 3, D is worth 2, E is worth 1, and so on. word: string (lowercase letters) n: integer (maximum hand size; i.e., hand size required for additional points) returns: int >= 0
""" # TO DO ... You may assume that the input word is always either a string of lowercase letters, or the empty string "". You will want to use the SCRABBLE_LETTER_VALUES dictionary defined at the top ofgame.py. You should not change its value.
Do not assume that there are always 7 letters in a hand! The parameter n is the number ofletters required for a bonus score (the maximum number of letters in the hand).
Testing:
If this function is implemented properly, and you run test_game.py, you should see that the test_get_word_score() tests pass. Also test your implementation of get_word_score, usingsome reasonable English words.
HINT: To iterate through all the characters in a string, try something like this loop, which prints each letter in word:
for letter in word: print letter
II. Dealing with hands
Representing hands
A hand is the set of letters held by a player during the game. The player is initially dealt a set of random letters. For example, the player could start out with the following hand:
Page 3 of 9
COE808 Lab 5 (2010) February 23, 2010
Scrabble in Python
a, q, l, m, u, i, l A straightforward way to represent a hand in Python is as a list: hand = ['a', 'q', 'l', 'm', 'u', 'i', 'l']
However, we'll represent the hand in a different way, because it simplifies the code we'll need in the update_hand and is_valid_word functions. (In general, there are many ways to represent, in code, various concepts -- some are better suited to certain operations than others).
In our program, a hand will be represented as a dictionary: the keys are (lowercase) letters and the values are the number of times the particular letter is repeated in that hand. For example, the above hand would be represented as:
hand = {'a':1, 'q':1, 'l':2, 'm':1, 'u':1, 'i':1}
Notice how the repeated letter 'l' is represented.
Notice that with a dictionary representation, the usual way to access a value is hand['a'], where 'a'is the key we want to find. However, this only works if the key is in the dictionary; otherwise, we get an error. To avoid this, we can call hand.get('a',0). This is the "safe" way to access a value if we are not sure the key is in the dictionary. It returns the value found if the key is in the dictionary, and 0 otherwise.
Converting words into dictionary representation
One useful function we've defined for you is get_frequency_dict, defined near the top of game.py.
When given a string of letters as an input, it returns a dictionary where the keys are letters and the values are the number of times that letter is represented in the input string. For example:
>> get_frequency_dict("hello") {'h': 1, 'e': 1, 'l': 2, 'o': 1}
As you can see, this is the same kind of dictionary we use to represent hands.
Displaying a hand
Given a hand represented as a dictionary, we want to display it in a user-friendly way.
We have provided the implementation for this in the the display_hand function. Make sure you read through this carefully and understand what it does and how it works.
def display_hand(hand): """ Displays the letters currently in the hand. For example: display_hand({'a':1, 'x':2, 'l':3, 'e':1}) Should print out something like: a x x l l l e The order of the letters is unimportant. hand: dictionary (string -> int) """ for letter in hand.keys():
for j in range(hand[letter]):
Page 4 of 9
COE808 Lab 5 (2010) February 23, 2010
Scrabble in Python
print letter, # the comma ensures everything in the for loop #prints on the same line
print # this just prints an empty line
Generating a random hand
The hand a player is dealt is a set of letters chosen at random. We now need a function that generates this random hand. We have to be careful when randomly picking a hand. We need to ensure that there are enough VOWELS in the hand to allow the player to spell some words.
In our implementation, we use the randrange function from the random module to generate random numbers. Note that we import random at the top of game.py, and use the syntax random.randrange() to call randrange from inside module random. Make sure you read through our implementation of deal_hand carefully, and understand what it does and how it works.
import random def deal_hand(n): """
Returns a random hand containing n lowercase letters. At least n/3 of the letters in the hand should be VOWELS. Hands are represented as dictionaries. The keys are
letters and the values are the number of times the
particular letter is repeated in that hand. n: int >= 0
returns: dictionary (string -> int)
""" hand={} num_vowels = n / 3
for i in range(num_vowels):
x = VOWELS[random.randrange(0,len(VOWELS))] hand[x] = hand.get(x, 0) + 1
for i in range(num_vowels, n): x = CONSONANTS[random.randrange(0,len(CONSANANTS))] hand[x] = hand.get(x, 0) + 1 return hand
Notice the access of values using hand.get(x,0) because we do not know if the key x is in the dictionary, as discussed in the Representing hands section.
Removing letters from a hand
The player starts with a hand, a set of letters. As the player spells out words, letters from this set areused up. For example, the player could start out with the following hand:
Page 5 of 9
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- more than abcs letter knowledge and the development of a
- literacy strategy
- how words cast their spell
- letters to a prisoner amnesty international canada
- welcome to word ladders
- coe808 lab 5 python scrabble ryerson university
- handwriting scholastic
- your 4 year old alberta
- games to teach letters sounds
- letters and sounds phase four
Related searches
- k 5 computer lab lesson plans
- scrabble 5 letter word list
- lab university of applied sciences
- all scrabble 5 letter words
- university of michigan lab locations
- centos 6 5 python 2 7
- university of michigan lab sites
- ap lab 5 cell respiration
- 5 letter scrabble words
- ryerson university data analytics
- ryerson university address
- ryerson university home