Thursday, 20 June 2013

Scrabble Cheater using PYTHON

#enjoy.. Fun with scrabble cheater
# sowpods.txt is the list of words in the official SOWPODS Scrabble word list.


#dictionary that stores the letters as the keys and the values as their scores.
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}

my_file = open("sowpods.txt", "r") #open file in read mode.
li = my_file.readlines()                   # li is the list that stores the of words.
my_file.close()                                #closes sowpods.txt file.  
for i in range(0,len(li)):                  # for loop that strips new line characters 
    li[i] = li[i].strip()                         #from the words

import sys

rack = sys.argv[1]                       # get the Scrabble rack from the command line.
valid_words = []
for word in li:                               
    candidate = True                    
    rack_letters = list(rack)          #rack is stored as a list in racks_letters 
    for letter in word:
        if letter not in rack_letters:  #checking if the rack forms the word.
            candidate = False
        else:   
            rack_letters.remove(letter)
    if candidate == True:                 #if candidate is true calculate score.
        total = 0
        for letter in word:
            total = total + scores[letter]
        valid_words.append([total, word])
        
valid_words.sort()                            #sorts list of valid words that could be
                                                         #formed by the rack
for entry in valid_words:
    score = entry[0]
    word = entry[1]
    print(str(score) + " " + word)       #prints score space word.       
       
            

1 comment:

  1. I m enjoying learning python :)

    Thank you,Jessica! :D

    love
    Shobha

    ReplyDelete