Thursday, September 19, 2013

Open Hatch's Scrabble Challenge: Let's Score and More

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))
  
  





Sunday, September 15, 2013

DFW Pythoneers 2nd Saturday Teaching Meeting, September 14, 2013

We had 11 Pythoneers show up for DFW Pythoneers 2nd Saturday Teaching Meeting. A special shout out to Jose and Jim from KForce Technology for setting us up with a great conference room and refreshments.

John Zurawski covered the news and interesting projects occurring in the Python world. He also covered his latest entry in the Ludum Dare contest and went into about his challenges with Python application installers. (John also loaned his Apple Mini DisplayPort to VGA Adapter to me which allowed me to present on the projector. Thanks a million, John!)

I made a presentation on Python Challenges and covered another step in solving the OpenHatch Scrabble Challenge.

If you missed the meeting due to the time change, please be aware that the scheduling may vary based on the venue, so double check the time and location for each individual meetup. KForce offered us great venue in a very central location, so it's probable that we will met here in the future and 1:00 PM is the closing time on Saturday. If you have any firm alternative sites please suggest them to either John, Kevin, Jeff or myself. We need room for 25 people, WiFi, a projector and restrooms.