Scoring
So we've discussed and analyzed word length as part of our Scrabble Challenge. Now let's discuss scoring the value of the words in the list. The kind folks at OpenHatch kindly give us a dictionary of letters with their values. So the "base" score of a word would be the sum of the value of letters. Let's build a function that calculates the score that utilizes this gift.
def word_score(input_word):
# score the word
# need to account for the blanks
scores = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
"x": 8, "z": 10}
word_score = 0
for letter in input_word:
word_score = word_score + scores[letter]
return word_score
In actual play scoring is more complex. The player has to consider the premium squares, blank tiles and the words that are already in play. These situations are a bit too complex to be calculated in our simple program, but but we can assist the player by returning the base score of their possibilities.
Where to Store the Score?
We could calculate and store the score and words in the SOWPODS list in a large dictionary similar to that of the letters and there scores. But this approach doesn't resolve some issues. We also need a place to store the length of the word. A dictionary can store a key/value pair, but we need the ability to use more than one value to sort by. We could use a list, but instead we are going are going to use a secret weapon included with Python: SQLite.
Loading the word list, word lengths an scores into a database solves a few problems in this challenge. We only have to load the data and the calculated values once, then use the fruits of the work again and again.
#!/usr/bin/env python
# -*- coding: ascii -*-
"""
Load the sowpods word list into a sqlite database table
Note: Rough Prototype
"""
from __future__ import print_function
import string
import sys
import sqlite3 as sqlite
def test_for_db():
# test for existance of sowpods database
pass
def test_for_sowpods():
# test for existence of sowpods text file
pass
def word_score(input_word):
# score the word
# need to account for the blanks
scores = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
"x": 8, "z": 10}
word_score = 0
for letter in input_word:
word_score = word_score + scores[letter]
return word_score
def word_list(input_file):
# create a list of tuples which containing the word, it's length, score and sorted value
sp_list =[]
f = open(input_file, 'r')
for line in f:
sp_word = line.strip().lower()
sp_list.append((sp_word, len(sp_word), ''.join(sorted(sp_word)), word_score(sp_word)))
f.close()
return sp_list
def load_db(data_list):
# create database/connection string/table
conn = sqlite.connect("sowpods.db")
cursor = conn.cursor()
# create a table
tb_create = """CREATE TABLE spwords
(sp_word text, word_len int, word_alpha text, word_score int)
"""
conn.execute(tb_create)
conn.commit()
# Fill the table
conn.executemany("insert into spwords(sp_word, word_len, word_alpha, word_score) values (?,?,?,?)", data_list)
conn.commit()
# Print the table contents
for row in conn.execute("select sp_word, word_len, word_alpha, word_score from spwords"):
print (row)
if conn:
conn.close()
def print_help():
""" Help Docstring"""
pass
def test():
""" Testing Docstring"""
pass
if __name__=='__main__':
# test()
sp_file = "sowpods.txt"
load_db(word_list(sp_file))