Jacob_DiDiodato icon

Scrabble Word Solver

Jacob_DiDiodato | PRO | 02/24/17 12:03:11 AM UTC | 0 ⭐ | 237 👁️ | Never ⏰ | []
Python |

4.59 KB

|

None

|

0 👍

/

0 👎

'''
#   Project Name: wordPuzzle.py
#    Author Name: Jacob DiDiodato
#Project Version: 1.0
#      Last Edit: Thursday February 23rd 2017 @ 7:47 PM
#This program is best used on Python 2.7, if not there is certainly some unexpected behaviour that could happen
#This program, when given a set of letters (comma seperated) and a length of word to produce, will produce all words with those combinations using the provided cartesian product method from itertools
#You are free to use this software however you like, however, if you are using it please reference my name and the original page you found this program on (probably this: http://pastebin.com/sEgjjMzQ)
#The dictionary used for this program can be found here: http://www.filedropper.com/scrabbledictionary
#In order to use the dictionary, simply add it the same folder you are running this program in.
#It seems that most unicode characters work for character sets within this program. For example, if you had a French dictionary and wanted to use the accent letters, you could!
'''
 
import sys, itertools, os
 
rows = 22
 
posInWords = 0
 
#Read the text from scrabbleDictionary.txt and splits it into an array
plainText = open("scrabbleDictionary.txt", 'r')
scrabbleDictionary = plainText.read().split('\n')
plainText.close()
 
dict = dict.fromkeys(scrabbleDictionary)
 
#Doesn't do anything right now
scrabbleValues = {'e': 1, 'a': 1, 'i': 1, 'o': 1, 'n': 1, 'r': 1, 't': 1, 'l': 1, 's': 1, 'u': 1, 'd': 2, 'g': 2, ' ': 2, '-': 2, '_': 2, 'b': 3, 'c': 3, 'm': 3, 'p': 3, 'f': 4, 'h': 4, 'v': 4, 'w': 4, 'y': 4, 'k': 5, 'j': 8, 'x': 8, 'q': 10, 'z': 10} 
 
def checkStrings(listOfStrings):
    realWords = []
    
    #Check through all the words that we were given
    for i in range(len(listOfStrings)):
        #Remove the misc. stuff from the string
        listOfStrings[i] = str(listOfStrings[i]).lstrip('(').rstrip(')').split(", ")
        #Then for each sub-list...
        for j in range(len(listOfStrings[i])):
            #Make some more lists
            listOfStrings[i][j] = str(listOfStrings[i][j]).strip("'")
        #If we find the word in our "dictionary" then we add it to the list of real words
        if ("".join(listOfStrings[i]) in dict):
            realWords.append("".join(listOfStrings[i]))         
    return realWords
 
def makeStrings(listOfChars, lengthOfWord):
    return checkStrings(list(itertools.product(listOfChars, repeat=lengthOfWord)))
    
def main():
    #Crashes when you start to generate LOTS of words because Python only handles like 2GB of memory
    #Proper execution: python filename a,b,c,d,e 4
    #               OR python filename alphabet 3
 
    #check if the user has entered 2 CMD arguments
    try:
        sys.argv[1]
        sys.argv[2]
    except IndexError:
        print "You are missing either the list of chars or length of word to create."
    else:
        #If the second CMD argument isn't a number, inform the user
        try:
            int(sys.argv[2])
        except:
            print "The second CMD argument must be the the length of word you wish to create."
        else:
            #If they enter the special alphabet command then make the input the entire alphabet
            if (sys.argv[1] == "alphabet"):
                sys.argv[1] = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"
            return makeStrings(sys.argv[1].split(","),int(sys.argv[2]))
 
try:
    #Try to grab a sorted list of the words
    words = sorted(list(set(main())))
except TypeError:
    raise SystemExit
else:
    totalPages = len(words)//rows
 
    if (len(words) == 0):
        print "there are no words from these characters"
    else:
        while True:
            #Print the words depending on what page I'm on
            for i in range(posInWords*rows, (posInWords+1)*rows):
                try:
                    print words[i]
                except IndexError:
                    print
                    
            #Some basic user commands
            print "You are looking at page:", posInWords + 1, "of the words, type down to go to the next page and up to go to the previous page"
            command = raw_input("")
            if (command == "down"):
                if (not(posInWords >= totalPages)):
                    posInWords += 1
            elif (command == "up"):
                if (not(posInWords - 1 < 0)):
                    posInWords -= 1
            elif (command == "quit"):
                raise SystemExit
            elif (command == "top"):
                posInWords = 0
            elif (command == "bottom"):
                posInWords = totalPages
            elif (command == "find"):
                print "\n"*rows
                print "type the word you wish to find"
                wordToFind = raw_input("")
                for i in range(0, len(words)):
                    if (wordToFind == words[i]):
                        print "your word is on page", str((i//rows) + 1), "and at position", str((i%rows) + 1), "of 22 from the top"
                        posInWords = i // rows
                        break
                else:
                    print "your word isn't found"
            elif (command == "count"):
                print len(words)

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎