Created
June 27, 2012 15:39
-
-
Save hcarreras/3004900 to your computer and use it in GitHub Desktop.
Scrabbel best move
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Dictionary | |
attr_reader :my_words | |
def initialize(rows) | |
@my_words = rows | |
end | |
def valid_words(letters) | |
good_words = Array.new #here is where gonna be the words we can use | |
contains = true | |
#It's a triplex-loop. Only we can use a word if we have each letter in my_letters | |
for w in @my_words && contains | |
p = 0 | |
while p < w.size | |
compare = w[p] | |
have_l = false; | |
for l in letters | |
if l[0] == compare #It's l[0] because tiles has a letter and a puntuation | |
have_l = true | |
end | |
end | |
if(have_l == false) | |
contains = false | |
end | |
p += 1 | |
end | |
if(contains) | |
good_words.insert(good_words.size, w) #add in the bottom | |
end | |
contains = true; | |
end | |
good_words #return | |
end | |
end | |
class Board | |
attr_reader :squares | |
def initialize(rows) | |
@squares = rows.collect { |line| parse_line(line) } | |
end | |
def rows | |
@squares.size | |
end | |
def cols | |
@squares[0].size | |
end | |
def parse_line(row) | |
array_row = Array.new | |
for col in row | |
position = 0 | |
while position < row.size | |
array_row.insert(positon, row[position].to_i) | |
position += 1 | |
end | |
end | |
array_row #return | |
end | |
def points(row, col) | |
@squares[row][col] | |
end | |
def best_puntuation(words, letters) | |
position_x = 0 | |
position_y = 0 | |
best_score = 0 | |
actual_score = 0 | |
for w in words | |
actual_row = 0 | |
while actual_row < rows | |
actual_col = 0 | |
while actual_col + w.size < cols | |
p = 0 | |
while p < w.size | |
#The puntuation of a square is the points of this square * the points of the tile | |
actual_score += letters.puntuation(w[p]) * @squares.points(actual_row, actual_col) | |
p += 1 | |
end | |
if actual_score > best_score | |
best_score = actual_score | |
position_x = actual_row | |
position_y = actual_col | |
word = w | |
end | |
end | |
end | |
end | |
[word, position_x, position_y] #return | |
end | |
end | |
class Tile | |
attr_reader tiles | |
def initialize(rows) | |
@tiles = rows | |
end | |
def puntuation(letter) | |
for tile in @tiles && !found? | |
if(letter == tile[0]) | |
puntuation = tile[1] | |
found = true | |
end | |
end | |
puntuation | |
end | |
end | |
class Scrabbler | |
def initialize(path) | |
path ||= 'INPUT.json' | |
data = YAML.load(File.read(path)) | |
@board = Board.new(data['board']) | |
@tiles = Tile.new(data['tiles']) | |
@words = Dictionary.new(data['dictionary']) | |
end | |
def find_best_move | |
@board.best_puntuation(@words.valid_words(@tiles)) | |
end | |
best_move = Scrabbler.new('INPUT.json').find_best_move | |
put best_move | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment