Scrabble, anyone?

python
game

#1

For those who went through the Python track, you'll remember the scrabble_score problem. We solved it and that was that. But what if we wanted to think it through a little more and begin to dial in on the real game parameters, such as tile count. Would a word be valid under those constraints? That's just one of the criteria. The game has others, especially written in program form.

One can only begin to imagine the variables, structures and algorithms, so let's start with the simple ones and build from there. Since this is a project that I am woe to complete alone, it is put to the floor in hopes others will take an interest and share their ideas.

Because Scrabble is an internationally famous and adapted game, we must assume that this and all code relates to the English language version.

tiles = {"a": 9, "b": 2, "c": 2, "d": 4, "e": 12, "f": 2,
         "g": 3, "h": 2, "i": 9, "j": 1, "k": 1, "l": 4, 
         "m": 2, "n": 6, "o": 8, "p": 2, "q": 1, "r": 6, 
         "s": 4, "t": 6, "u": 4, "v": 2, "w": 2, "x": 1, 
         "y": 2, "z": 1, "_": 2}

def count_tiles():
    return sum([tiles[x] for x in tiles])

print count_tiles()

def count_verify(word):
    frequency = letter_frequency(word)
    for letter in word:
        if frequency[letter] > tiles[letter]:
            return False
    return True
    
def letter_frequency(word):
    letters = { letter: 0 for letter in word }
    for letter in word:        
        letters[letter] = letters[letter] + 1
    return letters

One tiny step. None of the above is boilerplate, so feel free to improve upon it. We have a long way to go.


#2

Imagine if each letter has a dictionary of all the words on the board that it is included in.


#3

Add to that where it is the origin of intersecting words.


#4

The game dictionary would be as ad hoc as the word dictionary, built upon the letter foundation that precedes it. Only letters already introduced would be in its collection (yet to be thought out...).


#5

http://discuss.codecademy.com/t/9-scrabble-score/12751/26

Hi Marciej,

Don't mean to trouble you... I'm trying to come up with a dictionary comprehension for letter frequency and have hit a wall (nothing new). Have you got one in your bag of tools?

This should give you an idea of where I'm at (which is wrong)

def letter_frequency(word):
    letters = {letter: + 1 for letter in word }
    return letters

At this point, it returns 1 for every letter in "krackle" (which is not a word, but that's not important). I'm trying to construct a letter count validator that rejects any word with too many of a given letter. There are only 100 tiles in English Scrabble, including only 1 k. It's impossible to validate if I can't come up with a decent frequency table. The long way is moot. I'm more interested in stretching things into comprehensions, if possible for this. Appreciate your consideration. Thanks.

Cheers!
Roy


#6

tiles = {"a": 9, "b": 2, "c": 2, "d": 4, "e": 12, "f": 2,
         "g": 3, "h": 2, "i": 9, "j": 1, "k": 1, "l": 4, 
         "m": 2, "n": 6, "o": 8, "p": 2, "q": 1, "r": 6, 
         "s": 4, "t": 6, "u": 4, "v": 2, "w": 2, "x": 1, 
         "y": 2, "z": 1, "_": 2}

def count_tiles():
    return sum([tiles[x] for x in tiles])

print count_tiles()     #  100

#7

Something that works as expected, but still not dialed down...

def letter_frequency(word):
    #letters = { letter: + 1 for letter in word }
    letters = { letter: 0 for letter in word }
    for letter in word:        
        letters[letter] = letters[letter] + 1
    return letters

#8

Nice question!

I would probably use something like this:

def letter_frequency(word):
    return {letter: word.count(letter) for letter in set(word)}

I use set to not count a frequency of the same letter multiple times. But this solution loses this nice linear complexity.


The main problem is that in list comprehensions we can not use assignments, only expressions. I am afraid that if you don't want to use built-in methods (like I did) then it might be a better idea to use a loop instead of list comprehension.


You can also use powerful counters:

from collections import Counter
word = "krackle"
Counter(list(word))

9/15 Scrabble
#9

May we add/merge this with the topic in the Corner Bar?


#10

Sure Roy! :smile:


#11

I'm old, it's late. If you or Judy (@albionsrefuge) could handle the logistics, that would be great! TX


#12

I don't have moderator powers, I am not able to merge topics, but I invited Judy to this discussion. In the worst case - we will copy it :smile:

I have to go, I have my ping pong class in 15 minutes. I will try to visit codecademy in the evening.


#13

Keep the serve!

Cheers!


#14

I hope that this is the topic that you wanted it merged with, if not, let me know. :green_heart:


#15

Yes, thank you, @albionsrefuge.